Box2D Forums

It is currently Mon May 20, 2013 3:31 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Aug 09, 2012 5:40 am 
Offline

Joined: Thu Aug 09, 2012 5:36 am
Posts: 4
Hello friends,
I have a issue regarding the collision of two bodies in box2d. For collision detection i am using ray wenderlich tutorial.
In my game i have two sprite body 1) Player 2) Enemy.
Here in game enemy body follow the player and when they collied enemy body will destroy but here in my case they collied perfectly and also detect collision point but my enemy body can not destroy.
I show you my update method code
Code:
-(void)update:(ccTime)dt
{
    world->Step(dt, 10, 1);
    //  CGPoint scaledVelocity = ccpMult(leftJoystick.velocity, 140);
    for(b2Body *b=world->GetBodyList();b;b=b->GetNext())
    {
        if(b->GetUserData()!=NULL)
        {
            CCSprite *balldata=(CCSprite *)b->GetUserData();
            balldata.position = ccp(b->GetPosition().x * PTM_RATIO,
                                    b->GetPosition().y * PTM_RATIO);
        }
    }
    playerPos=playerBody->GetPosition();
    enemyPos=enemyBody->GetPosition();
   // This code is for follow the enemy sprite...
    float dx=(playerPos.x-enemyPos.x)*PTM_RATIO;
    float dy=(playerPos.y-enemyPos.y)*PTM_RATIO;
    float d=sqrtf(dx*dx + dy*dy);
    float v=1;
    if(d>10)
    {
        enemyBody->SetTransform(b2Vec2(enemyPos.x+dx/d *v *dt,enemyPos.y+dy/d *v *dt), enemyBody->GetAngle());
    }
    std::vector<b2Body *>toDestroy;
    std::vector<MyContact>::iterator pos;
    for(pos = contactListener->_contacts.begin();
        pos != contactListener->_contacts.end(); ++pos) {
        MyContact contact = *pos;
        b2Body *bodyA = contact.fixtureA->GetBody();
        b2Body *bodyB = contact.fixtureB->GetBody();
        if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
        {
            CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
            CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
         // Playersprite Tag is 2      Enemy tag is 1

            if (spriteA.tag == 1 && spriteB.tag == 2)
            {
                // SpriteA = PlayerSprite
                // SpriteB = EnemySprite
           
            }
            else if (spriteA.tag == 2 && spriteB.tag == 1)
            {
                //toDestroy.push_back(bodyB);
                NSLog(@"Enemy Touch");
                NSString *t=[NSString stringWithFormat:@"Game Over"];
                label.string=t;
                [self removeChild:spriteB cleanup:YES];
                world->DestroyBody(bodyB);
            }
        }
    }
}


Please help me
Thanks


Top
 Profile  
 
PostPosted: Mon Aug 13, 2012 11:59 am 
Offline

Joined: Tue Jul 31, 2012 7:46 am
Posts: 11
I am not really sure what is happening, but

a) No, a body cannot be destroyed while it is being collided, because the world is processing data involving the body, so if the body is destroyed it will cause trouble.

b) To solve the problem cause by (a) look at this tutorial by the irresistible force:

https://www.iforce2d.net/b2dtut/removing-bodies

It shows you how to remove bodies and some of the other tutorials will tell you how to detect collisions and other things like that.

Finally

c) This is in Games and Applications, and I am not sure why. A place like general discussion would probably be better.

Hope that helped.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group