How to make an object in box2d have constant velocity even after collision?

| | August 4, 2015

I have iOS game where I have a bouncing character. Everything is being handled by box2d. The problem is when the character hits a wall of another character it’s velocity changes, but I don’t want this. I need the velocity to stay constant even after collisions.

I was thinking the only way to do this was to keep setting the velocity after each collision unless there’s another way to do this that I’m not aware of?

One Response to “How to make an object in box2d have constant velocity even after collision?”

  1. If you want to maintain the velocity of a body (and have it ignore collisions), just set the body type using the b2body::setType function:

    mybody.setType(b2_kinematicBody);
    

    Alternatively, the b2BodyType can be assigned to the b2BodyDef before the body is created using the b2BodyDef::type public attribute:

    mybodydef.type = b2_kinematicBody;
    

    All kinematic bodies will move with constant velocity, completely ignoring collisions.

Leave a Reply