4
votes

I'm creating a top-down 2d tile-based game and having some troubles with setting up box2d to work in a specific way.

Basically, what i need is to have all box2d bodies to follow certain set of rules: 1) they do not overlap 2) they do not push each other 3) they still collide and provide collision contact lists

I have set gravity to 0, and move my bodies only with setLinearVelocity, because i need them to move at constant speed, and without any inertia effect.

It looks like i need a static body but one that can be moved manually.

I thought that i could use kinematic bodies, but turned out that they overlap with each other and don't have contact lists.

Is there a way to make a solid bodies that can collide, but can't push each other?

2

2 Answers

1
votes

One way to do this is create a kinematic body for each dynamic body as a 'shadow' body, with exactly the same fixtures. After very time step, set the position, angle, linear velocity and angular velocity of the kinematic body to match that of the dynamic body it is shadowing.

The original dynamic fixture should be set to not collide with its own shadow fixture, and not to collide with the original fixtures for bodies that have a shadow body.

When two sets of bodies like this collide, their shadow bodies will prevent the opposing dynamic body from pushing them, but they can still collide normally with other fixtures in the world.

Keep in mind that Box2D tries really hard to replicate the physics we see in the real world, and what you are asking for here is not realistic at all. You will probably see some VERY weird behavior, especially if you try to pile these bodies up together, even though they follow your rules. However, for a top-down scenario where piling of bodies is not needed, it might be ok.

0
votes

Try to set the masses to infinity - this should prevent them from being moved by other objects while still being able to set their velocity manually. If that doesn't work, try setting the mass to 0.