Is there a way to determine how much force is being applied to a body? In my game, I'd like a brick to shatter if it is hit with enough force or crumble if it is under enough weight.
Thanks,
Mike
I tried to remove a body after it had a certain amount of force applied to it. The idea is that if that amount of force is applied, then the object is destroyed and needs to be removed from the scenes. Here is the code I used.
@Override
public void result(ContactResult point) {
if (point.normalImpulse > 800.0f){
//The brick has been hit hard enough to be destroyed
Body body = point.shape2.getBody();
World world = body.getWorld();
body.destroyShape(point.shape2);
world.destroyBody(body);
}
}
I traced the code and made sure this is executing. And it is. But, the bodies are not being removed from the scene.
Am I doing something wrong here?
Thanks,
Mike
Mike,
You cannot destroy bodies directly when a collision is detected by the ContactListener. You have to mark the body for destruction and handle it before the next time step.
Nathan
Awesome! That did the trick!
Thanks,
Mike
The c++ version asserts if you try to do this so you can tell right away you're doing something wrong. I guess the java equivalent to that be throwing an exception?
java uses assertions as well, you just have to enable them with -ea