Search code examples
cocos2d-iphonebox2dcollision-detection

Adding a 'warp' feature to cocos2d / box2d game


I am having trouble getting a "warp" feature to work correctly in a cocos2d / box2d game.

I have two items for my main sprite to collide with - and if the sprite collides / enters one, then it exits the other - like a warp or teleport feature.

The problem I have is that if my sprite collides with "A", I then move it's position to "B" and it should exit "B" and keep going with the same direction and speed that it had before the "A" collision.

But as soon as the sprite hits "B", it is seen as a collision with "B" and it jumps back to "A"!

So if my sprite hits either warp feature, it ends up stuck in an endless loop where it just continually jumps from A to B and back again!!

I have tried a lot of temporary fixes, such as adding, eg, (x+10) to the position, so that it clears the "B" collision when it exits B, but that is a messy solution.

Hoping someone might have another bright idea!!


Solution

  • How about a BOOL to keep track of your collision state?

    So when your object collides with A, set

    _isColliding = YES 
    

    Once you're free of collisions, set

    _isColliding = NO
    

    Any collisions that happen while _isColliding is still set to YES would be ignored.