Let's say I had a ground fixture, a player box, and a kinematic block moving up and down. If the player stands under the block, weird things happen. I want to simply play an event where the player dies. How can I detect when the player box is crushed by the block moving down on it?
Asked
Active
Viewed 254 times
4
-
You can access all the contacts an object has, and calculate the resulting sum of forces acting on it – ThorinII Jul 19 '14 at 07:02
1 Answers
1
I use this trick in libGDX:
- Add a "hidden" box, slightly smaller than your "crusher" box.
- Align the center of the "hidden" box with the center of "crusher" box (this includes movement).
- Place custom data in the "hidden" body using Body#setUserData(), indicating that is a 'destroyer box'. You can access this UserData from inside the ContactListener.
- Detect any colission between the "player" box and the "hidden" box marked as 'destroyer' in with your UserData, so you can call a 'destruction' method.
It's common for Box2D to leave a dynamic body locked and overlapping kinematic bodies when the dynamic one can't move in any other direction. In this case, the dynamic body can collide with the "inner" box, otherwise impossible to reach.
This could vary according on your restitution and friction values.

Juan M. Molina
- 26
- 4