2
votes

I have a node which is placed under my screen and works as a platform. As soon as another node touches the node a boolean is set to NO. When I define my node with the SKPhysicsBody properties for the collision the node ignores the affectedByGravity property.

My code:

+ (void)addNewNodeTo:(SKNode *)parentNode
{
    //Correct image size
    SKSpriteNode *desertBottom = [SKSpriteNode node];
    desertBottom = [[SKSpriteNode alloc] initWithImageNamed:@"Giraffe.png"];

    desertBottom.position = CGPointMake(0, -200);
    desertBottom.zPosition = 2;


    desertBottom.physicsBody.collisionBitMask = lionType;
    desertBottom.physicsBody.categoryBitMask = terrainType;
    desertBottom.physicsBody.contactTestBitMask = lionType;


    desertBottom.zPosition = 2;
    desertBottom.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(desertBottom.size.width, desertBottom.size.height)];
    desertBottom.physicsBody.dynamic = YES;
    desertBottom.physicsBody.affectedByGravity = NO;


    [parentNode addChild:desertBottom];
}

My collision methods:

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

    if (collision == (lionType | terrainType)) {
        self.lionNode.lionIsJumping = NO;
        NSLog(@"%i", self.lionNode.lionIsJumping);
    }
}

- (void)didEndContact:(SKPhysicsContact *)contact
{
    uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

    if (collision == (lionType | terrainType)) {
        self.lionNode.lionIsJumping = YES;
        NSLog(@"%i", self.lionNode.lionIsJumping);
    }
}
3
Can you add the code where the bodies are colliding ? and if the platform is just stationary, try to set the dynamic property to NO aswell. It might solve the issue! - Harsh
I have already tried to set the dynamic property to NO but when it is NO the collision method won't fire anymore - David P

3 Answers

2
votes

A physics body has a velocity. Gravity changes velocity over time. If the body is already traveling at a certain velocity due to gravity, and you disable gravity, it will continue to move according to its current velocity but no longer gain additional speed from gravity.

My guess is that you expect the body to stop when disabling gravity. If that's what you want you can do this manually by setting the y component of velocity to zero:

SKPhysicsBody* body = desertBottom.physicsBody;
body.velocity = CGVectorMake(body.velocity.x, 0.0);
2
votes

If the platform should not move after a collision you have to set the dynamic property to false.

Maybe you have an issue with you bit masks?

Have you tried to log outside your if statement of the didBeginContact method?

1
votes

I have created a new class with subclass of SKSpriteNode for the bottom and added it to my GameScene.