9

How can you check if 2 bodies(with 1 Fixture both) collide(overlaps)?

I know about the ContactListener that fires a method when they start colliding and when they stop. But is there a way to check it in any given moment? Like:

if(body1.overlaps(body2))...

Additional details, one of them is sensor. this is in libgdx.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Daahrien
  • 10,190
  • 6
  • 39
  • 71
  • related: https://stackoverflow.com/questions/11062252/how-to-detect-collision-but-do-not-collide-in-box2d Completely inside another body: https://gamedev.stackexchange.com/questions/27320/in-box2d-how-can-i-check-for-bodies-within-a-circle – Ciro Santilli OurBigBook.com Dec 25 '17 at 11:22

3 Answers3

5

You can apply setContactlistner to your world object like

world.setContactListener(new ContactListener() {

            @Override
        public void beginContact(Contact contact) {

         if(contact.getfixtureA.getBody().getUserData()=="body1"&&
               contact.getfixtureB.getBody().getUserData()=="body2")
            Colliding = true;
            System.out.println("Contact detected");
        }

        @Override
        public void endContact(Contact contact) {
            Colliding = false;
            System.out.println("Contact removed");
        }

        @Override
        public void postSolve(Contact arg0, ContactImpulse arg1) {
            // TODO Auto-generated method stub
        }

        @Override
        public void preSolve(Contact arg0, Manifold arg1) {
            // TODO Auto-generated method stub
        }
    });

The beginContact() method will always call whenever any body will overlap or touch another body.You can also get the information about the body by contact object like contact.getFixtureA().getBody().getUserData(); if you want to do something with them.And when they separate from each other EndContact() method will be called.

Hope This helps.

Jagdeep Singh
  • 1,200
  • 1
  • 16
  • 34
  • 1
    You didn't understand/read my question, I know about the ContactListener. – Daahrien Jun 27 '13 at 23:16
  • As I said you can get the userdata of the bodies which is being colliding, so with the help of that user data of both the bodies you can take a Boolean or something and check it in the render if that Boolean is true then do your work.And false it in the endContact() method. – Jagdeep Singh Jun 28 '13 at 04:16
  • I don't know if this is a bug at current but endContact doesn't always fire. – Oliver Dixon Dec 26 '14 at 07:36
2

Just check if the contact you are looking for is in the contact list:

for (ContactEdge ce = body1.getContactList(); ce != null; ce = ce.next)
{
     if (ce.other == body2 && ce.contact.isTouching())
     {
         // Do what you want here

         break;
     }
}
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • 1
    Do not forget check, if contact is touching: ce->contact->IsTouching(). Contacts creating when AABB are overlap. – Pavel Jun 26 '13 at 10:25
  • 1
    Although I did label libgdx, I forgot to tell. It is in libgdx and there is no getContactList nor other method in body u.u – Daahrien Jun 26 '13 at 10:30
-4

You can create the variable collision: When equals 0, collision equals false; When equals 1, collision equals true;

So:

if(body1.overlaps(body2)==true)
{collision=1}
else
{collision=0}