4
\$\begingroup\$

I'm currently developing my own game but I'm facing a little problem. I am trying to figure out how to get the position of a fixture (collision) between two entities.

For example this:

enter image description here

(the grey rectangle is the player and the greens is an entity (not moving)).

With this code :

        Array<Contact> contacts = levelDatas.world.getContactList();
        try {
            for(Contact contact : contacts) {
                if(contact.getFixtureA().equals(playerBody.getFixtureList().get(0))) {
                    levelDatas.bodyIsWall(contact.getFixtureB().getBody());
                }
                if(contact.getFixtureB().equals(playerBody.getFixtureList().get(0))) {
                    levelDatas.bodyIsWall(contact.getFixtureA().getBody());
                }
            }
        } catch(GdxRuntimeException exp) {
        }

I get all the collision between a player and something else. But my problem is that I want to know where the collision is happening (on the left? on the right? bottom? top?). We can see that if there is a collision between 2 entities, a little point is draw. Anyone have a tip to give me the position of the collision?

Thanks

\$\endgroup\$
3
\$\begingroup\$

When two fixtures collide, there are most of times multiples collisions between them, you can see those collisions with :

for (int i = 0; i < contact.getWorldManifold().getNumberOfContactPoints(); i++) {
   contact.getWorldManifold().getPoints()[i].x;
   contact.getWorldManifold().getPoints()[i].y;
}

1st you check all collsion number, then you will get the X and Y position of the given 'i' index.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.