Can any one explain me how to destroy box2d body when collide i have tried but my application crashed.First i have checked al collisions then add all the bodies in array who i want to destroy.I am trying to learning this tutorial
My all the bodies are falling i want these bodies should destroy when these bodies will collide my actor monkey but when it collide it destroy but my aplication crashed.I have googled and from google i got the application crash reasons we should not destroy body in step funtion but i am removing body in the last of tick method.
could any one help me or provide me code aur check my code why i am getting this prblem or how can i destroy box2d bodies.
This is my code what i am doing. Please could any one check my code and tell me what is i am doing wrong for removing bodies.
The code is for multiple box2d objects falling on my actor monkey it should be destroy when it will fall on the monkey.It is destroing but my application crahes.
public synchronized void tick(float delta) {
synchronized (_world) {
_world.step(delta, 8, 3);
//_world.clearForces();
//addNewSprite();
}
CCAnimation danceAnimation = CCAnimation.animation("dance", 1.0f);
// Iterate over the bodies in the physics world
Iterator<Body> it = _world.getBodies();
while(it.hasNext()) {
Body b = it.next();
Object userData = b.getUserData();
if (userData != null && userData instanceof CCSprite) {
//Synchronize the Sprites position and rotation with the corresponding body
CCSprite sprite = (CCSprite)userData;
if(sprite.getTag()==1)
{
//b.applyLinearImpulse(force, pos);
sprite.setPosition(b.getPosition().x * PTM_RATIO, b.getPosition().y * PTM_RATIO);
sprite.setRotation(-1.0f * ccMacros.CC_RADIANS_TO_DEGREES(b.getAngle()));
}
else
{
//Apply the directional impulse
float impulse = monkey_body.getMass()*direction*WALK_FACTOR;
Vector2 force = new Vector2(impulse, 0);
b.applyLinearImpulse(force, b.getWorldCenter());
sprite.setPosition(b.getPosition().x * PTM_RATIO, b.getPosition().y * PTM_RATIO);
animDelay -= 1.0f/60.0f;
if(animDelay <= 0)
{
animDelay = ANIM_SPEED;
animPhase++;
if(animPhase > 2)
{
animPhase = 1;
}
}
if(direction < 0 )
{
isLeft=1;
}
else
{
isLeft=0;
}
if(isLeft==1)
{
dir = "left";
}
else
{
dir = "right";
}
float standingLimit = (float) 0.1f;
float vX = monkey_body.getLinearVelocity().x;
if((vX > -standingLimit)&& (vX < standingLimit))
{
// Log.v("sasd", "standing");
}
else
{
}
}
}
}
for (Iterator<MyContact> it1 = _contactListener.mContacts.iterator(); it1.hasNext();) {
MyContact contact = it1.next();
Body bodyA = contact.fixtureA.getBody();
Body bodyB = contact.fixtureB.getBody();
// See if there's any user data attached to the Box2D body
// There should be, since we set it in addBoxBodyForSprite
if (bodyA.getUserData() != null && bodyB.getUserData() != null) {
CCSprite spriteA = (CCSprite) bodyA.getUserData();
CCSprite spriteB = (CCSprite) bodyB.getUserData();
// Is sprite A a cat and sprite B a car? If so, push the cat
// on a list to be destroyed...
if (spriteA.getTag() == 1 && spriteB.getTag() == 2) {
//Log.v("dsfds", "dsfsd"+bodyA);
//_world.destroyBody(bodyA);
// removeChild(spriteA, true);
toDestroy.add(bodyA);
}
// Is sprite A a car and sprite B a cat? If so, push the cat
// on a list to be destroyed...
else if (spriteA.getTag() == 2 && spriteB.getTag() == 1) {
//Log.v("dsfds", "dsfsd"+bodyB);
toDestroy.add(bodyB);
}
}
}
// Loop through all of the box2d bodies we want to destroy...
for (Iterator<Body> it1 = toDestroy.iterator(); it1.hasNext();) {
Body body = it1.next();
// See if there's any user data attached to the Box2D body
// There should be, since we set it in addBoxBodyForSprite
if (body.getUserData() != null) {
// We know that the user data is a sprite since we set
// it that way, so cast it...
CCSprite sprite = (CCSprite) body.getUserData();
// Remove the sprite from the scene
_world.destroyBody(body);
removeChild(sprite, true);
}
// Destroy the Box2D body as well
// _contactListener.mContacts.remove(0);
}
}
}
Sorry for my english.
Thanks in advance.