Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing an object when collision happens

I’m still new to Java and Android programming and I am having so much trouble Removing an object when collision happens. I looked around the web and found that I should never handle removing BOX2D bodies during collision detection (a contact listener) and I should add my objects to an arraylist and set a variable in the User Data section of the body to delete or not and handle the removing action in an update handler. So I did this: First I define two ArrayLists one for the faces and one for the bodies:

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

Then when I create a face and connect that face to its body I add them to their ArrayLists like this:

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);

now I add a contact listener and an update handler in the onloadscene like this:

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 detect which two bodies collided in the contact listener by checking a variable from the user data section of the body, get their numbers in the array list and finally use the update handler to remove these bodies.

The questions are: Am I using the arraylist correctly? and in the collision listener how to retrieve the object that collided from the array list? How to add a variable to the User Data (the code please). I tried removing a body in this update handler but it still throws me a NullPointerException, so what is the right way to add an update handler and where should I add it? Any other advices to do this would be great. Thanks in advance.

like image 827
Ayham Avatar asked Sep 06 '11 15:09

Ayham


People also ask

How do you remove an object from a collision?

To destroy an object on collision within the using ty software, you have to use some form of the void OnCollisionEnter method. For 2D games you need the void OnCollisionEnter2D() method and the void OnCollisionEnter() method for 3D games.

How do you make an object disappear after collision unity?

After that in order to make the object disappear you can disable its renderer by using objectToDisappear. GetComponent<Renderer>(). enabled = false; inside your collision detection method.

How do I get some objects to ignore collision with a specific object?

IgnoreLayerCollision which is used to ignore collisions on layers. Just put all the Objects you want to ignore in a layer then invoke the function to ignore layers on them. Both layers do not have to be the-same. You can ignore collision on Objects from different layers.


1 Answers

Typically you would look at the user data for the two things that collided to decide if something should be deleted, and put those to be deleted in the list. Then after the time step, go through the list and delete them, and clear the list ready for the next time 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
}

So while you do need to have a list available to use, you don't need to put all the bodies into it when they are created.

The user data can be a class you make yourself, so you can make it contain whatever you like. Since you have the list to tell you which bodies are flagged for deletion, you don't need to have a flag for this in the user data. Besides, it would be inefficient to loop through every body in the world after every time step to check a flag in the user data.

like image 66
iforce2d Avatar answered Oct 04 '22 22:10

iforce2d