Collision deletion

I'm still new to Java and Android programming and I have so many problems. Deleting an object in a collision. I inspected the web interface and found that I should not handle the removal of BOX2D bodies during conflict detection (contact listener), and I should add my objects to arraylist and set a variable in the User Data section of the body to remove or not process the action deletion in the update handler. So I did this: First, I define two ArrayLists, one for faces and one for bodies:

ArrayList<Sprite> myFaces = new ArrayList<Sprite>(); ArrayList<Body> myBodies = new ArrayList<Body>(); 

Then, when I create a face and connect it to his body, I add them to my ArrayLists as follows:

 face = new AnimatedSprite(pX, pY, pWidth, pHeight, this.mBoxFaceTextureRegion); Body BoxBody = PhysicsFactory.createBoxBody(mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef); mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, BoxBody, true, true)); myFaces.add(face); myBodies.add(BoxBody); 

I now add a contact listener and update handler in onloadscene as follows:

 this.mPhysicsWorld.setContactListener(new ContactListener() { private AnimatedSprite face2; @Override public void beginContact(final Contact pContact) { } @Override public void endContact(final Contact pContact) { } @Override public void preSolve(Contact contact,Manifold oldManifold) { } @Override public void postSolve(Contact contact,ContactImpulse impulse) { } }); scene.registerUpdateHandler(new IUpdateHandler() { @Override public void reset() { } @Override public void onUpdate(final float pSecondsElapsed) { } }); 

My plan is to find out which two bodies collided in the contact listener by checking the variable from the body user data section, get their numbers in the list of arrays, and finally use the update handler to delete these bodies.

Questions: Am I using arraylist correctly? and in the conflict listener, how to retrieve an object that collided with an array? How to add a variable to user data (code, please). I tried to remove the body in this update handler, but it still throws me a NullPointerException, so this is the right way to add an update handler and where to add it? Any other tips for this would be great. Thanks in advance.

+6
source share
1 answer

Usually you look at user data for two things that have come across to decide if something needs to be deleted and put them in a list. Then after the time step, scroll through the list and delete them, and clear the list ready for the next step.

 {//game loop do world step //contacts occur in here, some bodies may be put in the list make sure list contents are unique go thru list and delete contents clear the list } 

Therefore, when you need a list that is available for use, you do not need to insert all the bodies into it when they are created.

User data can be a class that you make yourself, so you can make it contain everything you like. Since you have a list to indicate which organs are marked for deletion, you do not need to have a flag for this in user data. In addition, it would be inefficient to go through every body in the world after each time step in order to check the flag in user data.

+4
source

Source: https://habr.com/ru/post/896723/


All Articles