Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-27-2011, 01:34 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 8
FullMetul is on a distinguished road
Default Box2D Collisions Problem

Hey guys, newbie programmer here and I've been banging my head against the wall for the past six hours trying to figure this out so hopefully someone knows how to get around this. I've been trying to code a physics engine in box2D for a game I'm developing and it's been going fine until I've had to start working with collisions. But it's not the collisions thats the problem, I've implemented the contact listener and programmed the update method appropriately and when my two object's collide they do exactly what I want. The "player" hits the object and it immediately explodes, de-spawning and shooting the player backwards.

The problem is I don't want it to happen immediately. I want the item to wait a little before blowing up, giving the player a chance to get away. Like a bomb that will turn red if the user gets to close and after something like three seconds of the player standing near it, it will explode. I can't for the life of me figure out how to do this, I've tried NStimers, scheduling different methods, performselector with delay, and even sequences to stall it at first but each one of these either crash the app or cause it to hang. I think the problem might be that the collision detection itself is in the gamelogic update code. It also doesn't help that NStimers and the a performselector code can only take objects as parameters

Here's basically what it looks like with the immediate explosion:

-(void) update: (ccTime)delta
{
// The number of iterations influence the accuracy of the physics simulation. With higher values the
// body's velocity and position are more accurately tracked but at the cost of speed.
// Usually for games only 1 position iteration is necessary to achieve good results.
float timeStep = 0.03f;
int32 velocityIterations = 8;
int32 positionIterations = 1;
world->Step(timeStep, velocityIterations, positionIterations);

// for each body, get its assigned sprite and update the sprite's position
for (b2Body* body = world->GetBodyList(); body != nil; body = body->GetNext())
{

CCSprite* sprite = (CCSprite*)body->GetUserData();
if (sprite != NULL)
{
// update the sprite's position to where their physics bodies are
sprite.position = [self toPixels:body->GetPosition()];
float angle = body->GetAngle();
sprite.rotation = CC_RADIANS_TO_DEGREES(angle) * -1;
}
b2Vec2 slow = body->GetLinearVelocity();
slow.x = slow.x * -.0025;
slow.y = slow.y * -.0025;
body->ApplyLinearImpulse(slow, body->GetPosition());
}
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();
b2Vec2 nails = bodyA->GetPosition();
b2Vec2 nails2 = bodyB->GetPosition();
CGPoint nail = ccpMult(CGPointMake(nails.x,nails.y), 32);
if(fabs(nails.x - nails2.x) < 1 && fabs(nails.y - nails2.y) <1){
if (std::find(toDestroy.begin(), toDestroy.end(), bodyB)
== toDestroy.end()) {
toDestroy.push_back(bodyB);
}
}
}


std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
b2Vec2 nails = body->GetPosition();
CGPoint nail = ccpMult(CGPointMake(nails.x,nails.y), 32);
if (body->GetUserData() != NULL) {

CCSprite *sprite = (CCSprite *) body->GetUserData();
sprite.opacity = (255 - 100);
[self removeChild:sprite cleanup:YES];
[self launchBomb:nail:NO]; //actual explosion
[self explode:nail]; // particle effect
}
world->DestroyBody(body);
}
}

If there was some way to change that last part so I could have the collision and start a process to build up to an explosion that would be amazing because I'm so confused now.
FullMetul is offline   Reply With Quote
Old 02-27-2011, 09:03 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 8
FullMetul is on a distinguished road
Default

I think I've figured out how to get the timed animation in and I changed to code up a little:
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();
b2Vec2 nails = bodyA->GetPosition();
b2Vec2 nails2 = bodyB->GetPosition();
CCSprite *sprite = (CCSprite *) bodyB->GetUserData();
CGPoint nail = ccpMult(CGPointMake(nails.x,nails.y), 32);
if(pos == contactListener->_contacts.begin() && contact.fixtureA->GetDensity() != 0 && contact.fixtureB->GetDensity() != 0 ) {
sprite.opacity = sprite.opacity - 10;
if(fabs(nails.x - nails2.x) < 1.0 && fabs(nails.y - nails2.y) < 1.0 && sprite.opacity < 100){
if (std::find(toDestroy.begin(), toDestroy.end(), bodyB)
== toDestroy.end()) {

toDestroy.push_back(bodyB);
}
}
}
}

The problem is that the code locks up when contact begins with the screen boundaries (It's a space game so sometimes the items fly back and hit the boundary of the screen). It detects the collision but never calls the destroy method (as I don't want the wall to be destroyed). So now I'm stuck because I've got the collision method working but as soon as an object hits a wall it stops ALL collision detection. I'm assuming because the for loop gets stuck because contact never ends.

Does anyone have an idea of how I can have the contact listener ignore the screen boundaries? I've tried setting the boundries to inactive but then players and objects can move offscreen! Anyone have any ideas?
FullMetul is offline   Reply With Quote
Reply

Bookmarks

Tags
box2d, collisions, timers

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 422
7 members and 415 guests
chemistry, ChrisYates, hussain1982, Retouchable, skrew88, SLIC, xzoonxoom
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0