0
votes

I have this problem: I have node A and node B. What i want is when collision between them happens (node A collides with the top of B), node B won't be pushing node A a bit up. Because as it is now, when A collides with B, its being pushed back a bit, and collision instantly ends(didEndContact is called). So when im like colliding with that B, collision instantly ends, then starts again, then ends, then starts again.... What i want is that if A collided with B, A will lay on this B. I noticed i could achieve that by setting A.dynamic = NO, but i need to apply velocity to A, and if its not dynamic, velocity wont work.

This is the code:

typedef NS_OPTIONS(uint32_t, CollisionCategory)
{
    CollisionCategoryPlayer             =  1 << 0,
    CollisionCategoryDiggable           =  1 << 1,
};

-(void)didMoveToView:(SKView *)view
{
    self.playerNode = [SKSpriteNode spriteNodeWithImageNamed:@"player"];
    self.playerNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.playerNode.size];
    self.playerNode.physicsBody.categoryBitMask = CollisionCategoryPlayer;
    self.playerNode.physicsBody.collisionBitMask = CollisionCategoryDiggable;
    self.playerNode.physicsBody.contactTestBitMask = CollisionCategoryDiggable;
    self.playerNode.position = CGPointMake(160, 520);
    self.playerNode.name = @"player";
    self.playerNode.physicsBody.allowsRotation = NO;
    self.playerNode.physicsBody.friction = 0;
    [self addChild:self.playerNode];

    SKSpriteNode* spriteNode = [SKSpriteNode spriteNodeWithTexture:textToUse];
    spriteNode.name = keyFromCoordinate(coord);
    spriteNode.position = CGPointMake(160, 400);
    spriteNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode.size];
    spriteNode.physicsBody.dynamic = NO;
    spriteNode.physicsBody.categoryBitMask = CollisionCategoryDiggable;
    spriteNode.physicsBody.collisionBitMask = CollisionCategoryPlayer;
    spriteNode.physicsBody.contactTestBitMask = CollisionCategoryPlayer;
    spriteNode.physicsBody.resting = YES;
    [self addChild:spriteNode];

}

-(void)update:(NSTimeInterval)currentTime
{
    self.playerNode.physicsBody.velocity = CGVectorMake(0, -200);
}

And again the problem is that didBeginContact is called, then didEndContact, then didBegin and so on, what i want is - did begin is only called once.

1
Not near my Mac but try re-setting the collision bitmask for node a/b when it collides. - Scott
@Scott nothing changed :( - t0a0
@Scott i actually found the solution just now. Setting the restitution for both physics bodies to 0 worked. - t0a0

1 Answers

0
votes

Setting the restitution = 0 for both physicsBodies helped