如果physicsBody.dynamic属性为NO,则两个物理实体不联系

时间:2013-11-01 05:57:41

标签: ios objective-c collision-detection game-physics sprite-kit

有两个物理机构: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;
}

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;
}

他们都将physicsBody.dynamic属性设置为NO

问题是当子弹击中飞机时,SKScene没有调用didBeginContact方法。但是,如果我为physicsBody.dynamicYES指定AirplaneNode属性为BulletNode,则didBeginContact正在触发。

有没有办法解决这个问题?

PS:我真的不需要我的节点是动态的,因为它会导致不必要的行为:飞机在受到损坏时轻微移动,子弹在飞行时有时会旋转。

1 个答案:

答案 0 :(得分:16)

非动态(静态)物体永远不会发生碰撞,它们并不意味着首先改变它们的位置。

如果您不希望它们受到冲突的影响,请将collisionBitMask设置为0。请参阅SKPhysicsBody reference