Search code examples
box2dgame-physicsbox2dweb

Box2D - collect a coin


I'm fairly new to Box2D and I'm trying to make a simple game where a character jumps and picks a coin. I was able to create a contact listener and delete the coin when the contact was detected. The problem is that character and coin collided. I was able to filter out their contact but than I'm not able to determine if the character hitted the coin. I'm sure that there must be some relatively easy way to do it since there are collectible items in almost every game, but I just didn't find the way to achieve that.

Thanks for answer.


Solution

  • You are right, there is such way. Simply set isSensor to true in b2FixtureDef of the coins. It will disable the coin interation with the physic bodies, but contacts will continue creating.

    More info about sensors in Box2D you can find there.