Preventing movement on the X axis (Or another approach?)

Hi all,
Here's the situation I'm trying to work around: I have a player body, and many boxes spread around the world.
I want all bodies to be affected by gravity, I want all the boxes to collide with each other (i.e: to stack one on top of the other), but I do not want them to be affected by collisions with the player body. I have tried tons of different approaches, including setting their density to a very high number (and the player to a very low one), but nothing seems to work really well.
Another possible solution in this specific case would be to prevent their movement on the X axis altogether (as box-player collisions are on the horizontal plane most of the times). No matter what I try, the end result is quite jittery and feels very hacky.

This seems like something I keep getting problems with every time I work with Box2d... Is there a "right way" to handle these situations?
I'm using 2.1a btw.

Thanks! 🙂
There is filtering for what you want. Look at SetFilterData.
Hi Boris,
First of all, thanks for a great library 🙂
I'm not sure filtering is what I'm looking for here. I don't want the player to go through the boxes (which I believe is what will happen if I filter them).
I want them to collide, but have the boxes act as static in these collisions so that the player doesn't affect their position at all.
However, I don't want the boxes to actually be static, and still be affected by gravity.
bumping 🙂
i assume you could probably work something out in the preSolve event. I think 2.1 lets you modify the collision data after the collision is detect but before it is solved in the preSolve event. so far i have only seen implementations of 1 way platforms, by contact.SetEnabled(false); disabling collisions under depending on the situation of the collision.

looking into b2Contact.as
i see we can get fixtureA fixtureB and a manifold.
in b2Manifold i don't see anything specific to static bodies.
and according to the comments b2Manifold is sensitive. so it would probably be best to look elsewhere.

i wonder what would happen if you set the fixture to static in PreSolve and back to dynamic on the PostSolve. (or if you even can change it during a world step.)

most of the changes that are set as possibilities in the b2Contact.as seem to have flags. so even the SetSensor is not actually modifying a shape mid calculation. just setting it for that contact.


not sure if this gets you closer.
few ideas on Another Approach

for the time being you may be satisfied making it really high density (as long as it lands on a static object) so that it will react normal to gravity but not be budged by anything that runs into it.

or perhaps even Kenomatic if you just want it strictly control its movements with no alterations from contacts.
Hmm.. I'll give the contact listener approach a try.
Setting the density to a very high number is flaky at best. the outcome is odd, and even with a very high number there's a lot of 1 pixel movements.
Any other ideas? Can someone suggest how I can modify the box2d code to get this behavior? I don't mind if it's a hack if that's the only solution.

Thanks 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=4654