trouble getting collision point

137 views Asked by At

Hey I'm trying to get the point of collision and then create a joint at that point for two nodes. I'm not sure what my results mean or how to get what I want.

I don't have access to the contact points which I see in other posts. The CCContactSet from the collision only gives me number of points and normal.

I output the normal during collision begin and its (1.0, -0.0) for left wall, (-0.0, 1.0) for bottom wall, (-1.0, 0.0) for right wall, (-0.0, -1.0) for top wall. I don't understand them basically. I know they are only referring to the second hullPiece though because no matter how I rotate or position the booster during the collision, the results stay the same. They only change if I rotate the hullPiece.

So how do I get the contact points to create the joint? Am I supposed to use the normal for that, and if so how?

newSprite = CCSprite(imageNamed: "Booster.png")
newSprite.position = CGPoint(x: 200, y: 200)
newSprite.scale = 4.0
let spritePhysics = CCPhysicsBody(rect: newSprite.textureRect, cornerRadius: 0)
spritePhysics.collisionType = "booster"
spritePhysics.sensor = true
newSprite.physicsBody = spritePhysics
editorPhysics.addChild(newSprite)

newSprite2 = CCSprite(imageNamed: "HullPiece.png")
newSprite2.position = CGPoint(x: 200, y: 200)
newSprite2.scale = 4.0
let spritePhysics2 = CCPhysicsBody(rect: newSprite2.textureRect, cornerRadius: 0)
spritePhysics2.collisionType = "hull"
spritePhysics2.sensor = true
newSprite2.physicsBody = spritePhysics2
editorPhysics.addChild(newSprite2)

func ccPhysicsCollisionBegin(pair: CCPhysicsCollisionPair!, booster: CCNode!, hull: CCNode!) -> ObjCBool
{
    NSLog("contact point\(pair.contacts.count) \(pair.contacts.normal)")
    return true
}
1

There are 1 answers

2
Ben-G On BEST ANSWER

You get access to the contact points through the contacts property of the CCPhysicsCollisionPair.

Here's what the contacts struct looks like:

typedef struct CCContactSet {
    /// The number of contact points in the set.
    /// The count will always be 1 or 2.
    int count;

    /// The normal of the contact points.
    CGPoint normal;

    /// The array of contact points.
    struct {
        /// The absolute position of the contact on the surface of each shape.
        CGPoint pointA, pointB;

        /// Penetration distance of the two shapes.
        /// The value will always be negative.
        CGFloat distance;
    } points[2];
} CCContactSet;

Through the points array you can access the exact collision position for each involved shape.