Two physics bodies do not contact if physicsBody.dynamic property is NO
Asked Answered
M

1

7

There are two physics bodies: an AirplaneNode:

- (id)initAirplaneNode {
    self = [super initWithImageNamed:@"airplane.png"];
    if (self) {
        self.name = @"player";
        self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];
        self.physicsBody.dynamic = NO;
        self.physicsBody.affectedByGravity = NO;
        self.physicsBody.categoryBitMask = AIRPLANE_CATEGORY;
        self.physicsBody.contactTestBitMask = BULLET_CATEGORY;
    }
    return self;
}

and a BulletNode:

- (id)initBulletNode {    
    self = [super initWithImageNamed:@"bullet.png"];
    if (self) {
        self.name = @"bullet";
        self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];
        self.physicsBody.dynamic = NO;
        self.physicsBody.usesPreciseCollisionDetection = YES;
        self.physicsBody.categoryBitMask = BULLET_CATEGORY;
        self.physicsBody.contactTestBitMask = AIRPLANE_CATEGORY;
    }
    return self;
}

Both of them have physicsBody.dynamic property set to NO.

The problem is when a bullet hits an airplane, my SKScene doesn't call didBeginContact method. However, if I specify physicsBody.dynamic property to YES for either AirplaneNode or BulletNode, didBeginContact is firing.

Is there a way to fix that?

PS: I really don't need my nodes to be dynamic, because it causes an unwanted behaviour: an airplane slightly moves when getting damaged and a bullet sometimes rotates when flying.

Miseno answered 1/11, 2013 at 5:57 Comment(1)
one needs to be dynamic, then it should work. i.e. your bullet can be dynamic, and you and move it with an action and it will have collision detection. Prob need to set that its not affect by gravity.Gaius
T
17

Non-dynamic (static) bodies never collide, they aren't meant to change their position in the first place.

Instead set their collisionBitMask to 0 if you don't want them to be affected by collisions. Refer to the SKPhysicsBody reference.

Trickle answered 1/11, 2013 at 10:11 Comment(3)
dynamic = YES and collisionBitMask = 0 did the trick. Thanks!Miseno
hi andrey make the physics body dynamic to yes the object will be fall to ground and set the collisionBitMask = 0 also how to achieve that plz help me outSicard
@LearnCocos2D this is incorrect. Non-dynamic physic bodies do collide. the error here is just that setting a not collissionBitMask to zero, let the default value (full mask) which tell spriteKit to handle the collision and then for doesn't trigger the SKPhysicsContactDelegate delegate...Fatality

© 2022 - 2024 — McMap. All rights reserved.