ContactListener
.ContactListener
is an interface that can be implemented in its class for future use in the game world.beginContact
, endContact
, preSolve
, postSolve
. public class MyContactListener implements ContactListener{ @Override public void endContact(Contact contact) { } @Override public void beginContact(Contact contact) { } @Override public void preSolve (Contact contact, Manifold oldManifold){ } @Override public void postSolve (Contact contact, ContactImpulse impulse){ } }
world.setContactListener(new MyContactListener());
beginContact
endContact
preSolve
preSolve
will look like this: @Override public void preSolve (Contact contact, Manifold oldManifold){ WorldManifold manifold = contact.getWorldManifold(); for(int j = 0; j < manifold.getNumberOfContactPoints(); j++){ if(contact.getFixtureA().getUserData() != null && contact.getFixtureA().getUserData().equals("p")) contact.setEnabled(false); if(contact.getFixtureB().getUserData() != null && contact.getFixtureB().getUserData().equals("p")) contact.setEnabled(false); } }
contact.getFixtureA().getUserData().equals("p")
used to identify an object. I recall that when creating a platform, the platform.getFixtureList().get(0).setUserData("p")
method is used.postSolve
java.lang.NullPointerException
at com.badlogic.gdx.physics.box2d.World.contactFilter
@Override public void postSolve (Contact contact, ContactImpulse impulse){ Body body = null; if(contact.getFixtureA() != null && contact.getFixtureA().getUserData() != null && contact.getFixtureA().getUserData().equals("b")) body = contact.getFixtureA().getBody(); if(contact.getFixtureB() != null && contact.getFixtureB().getUserData() != null && contact.getFixtureB().getUserData().equals("b")) body = contact.getFixtureB().getBody(); if(body != null){ body.setActive(false); world.destroyBody(body); } }
getFixtureList().get(0).setUserData("b")
is specified will be destroyed. I wrote above that during a normal deletion there would be an error. But, if you make the object inactive body.setActive(false)
before deletion, there will be no error.Source: https://habr.com/ru/post/162079/
All Articles