Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 284
Andrey Gordeev Avatar asked Nov 01 '13 05:11

Andrey Gordeev


1 Answers

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.

like image 71
LearnCocos2D Avatar answered Nov 02 '22 23:11

LearnCocos2D