JBox2D - Find Collision Coordinates

I am writing a Java program using JBox2D. I need to find the exact point of collision between the two textures, if and when they collide.

I have code to determine if a collision has occurred, and can obviously just call the collision object identifier to determine which textures are colliding.

What I cannot understand is how to capture the actual coordinates of the collision itself. I read the documentation, but it is very complicated and does not address this problem directly.

Here is my code:

import org.jbox2d.callbacks.ContactImpulse; import org.jbox2d.callbacks.ContactListener; import org.jbox2d.collision.Manifold; import org.jbox2d.common.Vec2; import org.jbox2d.dynamics.Fixture; import org.jbox2d.dynamics.contacts.Contact; public class MyContactListener implements ContactListener{ //When they start to collide public void beginContact(Contact c) { System.out.println("CONTACT"); Fixture fa = c.getFixtureA(); Fixture fb = c.getFixtureB(); Vec2 posA = fa.getBody().getPosition(); Vec2 posB = fb.getBody().getPosition(); } public void endContact(Contact c) { } public void preSolve(Contact c, Manifold m) {} public void postSolve(Contact c, ContactImpulse ci){} } 
+7
java collision physics box2d jbox2d
source share
1 answer

To find out where the collision occurred, you need to know that sometimes there is not only one collision point, but also many points.

extracted from manual

(Image extracted from this manual )

As indicated in this guide,

Box2D has functions for calculating contact points for overlapping shapes. [...] These points [...] group them into a structure of diversity. [...]

Normally, you do not need to directly calculate the contact collectors, however, you will probably use the result obtained in the simulation. [...] If you need this data, it is usually best to use the WorldManifold structure [...].

You can access it in the Contact c class:

 public void beginContact(Contact c) { System.out.println("CONTACT"); WorldManifold worldmanifold; worldmanifold = c.getWorldManifold(); for(Vec2 point : worldmanifold.points){ System.out.println("Contact at : [" + point.x + ", " + point.y "]"); } } 

It is important . I have never used this library (JBox2D), however I am familiar with it (since libGDX apparently uses a similar one (Box2D)). Also, I don't know if JBox2D is Box2D (C ++) for Java, and if JBox2D and Box2D (libGDX alone) are connected at all. Perhaps some methods may change ( point.x may be point.getX() ).

You can check this site , but this is for C ++ (I use their answer to answer you).

+1
source share

All Articles