Box2D Forums

It is currently Fri May 24, 2013 4:28 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Collision Detection Code
PostPosted: Wed Jun 06, 2012 4:05 pm 
Offline

Joined: Fri Mar 09, 2012 1:43 pm
Posts: 69
I have this collision code so far:

Code:
//Rover-Meteor Collision
    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();
           
            // Sprite A = ball, Sprite B = Block
            if ([spriteA isKindOfClass:[Rover class]]) {
                NSLog(@"A-Rover && B-Meteor");
                if (std::find(toDestroy.begin(), toDestroy.end(), bodyB) == toDestroy.end()) {
                    //DONT DESTROY body, push it into an array? and it will be cleaned up later
                    toDestroy.push_back(bodyA);
                    toDestroy.push_back(rover.wheelLBody);
                    toDestroy.push_back(rover.wheelRBody);
                }
            }
            // Sprite B = block, Sprite A = ball
            else if ([spriteB isKindOfClass:[BMeteor class]]) {
                NSLog(@"A-Meteor && B-Rover");
                if (std::find(toDestroy.begin(), toDestroy.end(), bodyA) == toDestroy.end()) {
                    toDestroy.push_back(bodyA);
                }
            }     
        }       
    }
    // Clean up array of bodies to destroy?
    std::vector<b2Body *>::iterator pos2;
    for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
        b2Body *body = *pos2;     
        if (body->GetUserData() != NULL) {
            NSLog(@"is vector iterator being called....YES");
            CCSprite *sprite = (CCSprite *) body->GetUserData();
            [sceneSpriteBatchNode removeChild:sprite cleanup:YES];
        }
        isRoverAlive = FALSE;
        world->DestroyBody(body);
       
    }
   
    if (toDestroy.size() > 0) {
        PLAYSOUNDEFFECT(CAR_HIT);
    }


It works fine. However, Im thinking of adding another collision detection test, for the collision between meteor & ground because I would like to make the meteor explode when it hits the ground.

Do I simply add a new if/else test inside this code? Or do I need to create a new code block? I don't understand all the non-ObjC code so Im not to clear on the method or function names and their structure in the code block above.


Top
 Profile  
 
PostPosted: Wed Jun 06, 2012 8:57 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Yes, you can add another test in there. This part of Box2D usage can quickly get messy, I don't think there is a standard way to make it nice and tidy :)


Top
 Profile  
 
PostPosted: Thu Jun 07, 2012 5:04 am 
Offline

Joined: Fri Mar 09, 2012 1:43 pm
Posts: 69
Thanks,

Could I bother you to explain what those lines mean?

Specifically the lines with std::?


Top
 Profile  
 
PostPosted: Thu Jun 07, 2012 5:38 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
No :) You can read about those here:
http://www.cplusplus.com/doc/tutorial%20%20/templates/
http://www.cplusplus.com/doc/tutorial%20/namespaces/
http://www.sgi.com/tech/stl/


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 3 guests


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