# How detect collision between two bodies?

I'm using LibGDX with Box2D and I've two bodies. I just need to know that they have collided.

So, I just need a boolean output.

Example:

if (bodyA.collision(bodyB) == true) {
// Do something
}


How can I do this?

If it is too complicated, can I detect if two PolygonShape intersect?

The strategy in Box2d is to subclass the ContactListener class and define the appropriate methods. For example, you could create a subclass called MyContactListener, you would define the method MyContactListener:beginContact. Then, in your initialization, assign an instance of your MyContactListener class to your b2World using the function b2World:setContactListener. Once this is set up, the code inside your beginContact function will be called each and every time two bodies collide! (provided you do not have any contact filters set up)
In the beginContact function, you are given access to the contact object, from which you can query the fixtures involved in the collision, and subsequently the bodys involved.
What box2d is doing, and what you should strive to do as well, is to create triggers/events/callbacks so that when something interesting happens (e.g. a collision) the game will change what it needs to right then and there. It's the difference between the kids in the back seat asking 23,139 times: "Are we there yet?" and patiently waiting for their parent to say "We've arrived!" just 1 time.