Box2D Forums

It is currently Tue May 21, 2013 7:09 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed May 30, 2012 2:02 pm 
Offline

Joined: Fri Mar 09, 2012 1:43 pm
Posts: 69
I am testing this in the update method:

Code:
if (isBodyCollidingWithObjectType(geyserSensorBody,kCartType)) {
        //RUN CODE
        NSLog(@"Meteor collided with rover...push rover");
    }


But Im getting a crash like this:
Attachment:
Screen Shot 2012-05-30 at 2.57.32 PM.png
Screen Shot 2012-05-30 at 2.57.32 PM.png [ 96.92 KiB | Viewed 575 times ]


The Rover-Cart type is created like this:

Code:
- (id)initWithWorld:(b2World *)theWorld atLocation:(CGPoint)location {
    if ((self = [super init])) {
        world = theWorld;
        [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"rover.png"]];
        gameObjectType = kCartType;
        characterHealth = 100.0f;
        [self createBodyAtLocation:location];
    }
   
    //add wheels
    [self createWheels];
   
    return self;
}


Whereas the geyser & geyserSensorBody is create like this:

Code:
-(void)createGeyser{
   
    //Geyser
    geyser = [CCParticleSystemQuad particleWithFile:@"geyser1.plist"];
    geyser.position = ccp(100,20);
    [self addChild:geyser z:10];

    //Sensor for geyser
    CGSize winSize = [CCDirector sharedDirector].winSize;
    float32 sensorWidth = winSize.width * 0.03;
    float32 sensorHeight = winSize.height * 0.05;
    //float32 sensorOffset = winSize.width * 0.15;
       
    b2BodyDef bodyDef;
    bodyDef.type = b2_staticBody;
    bodyDef.position.Set(geyser.position.x, geyser.position.y);
    geyserSensorBody = world->CreateBody(&bodyDef);
       
    b2PolygonShape shape;
    shape.SetAsBox(sensorWidth/2/PTM_RATIO, sensorHeight/2/PTM_RATIO);
       
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &shape;
    fixtureDef.isSensor = true;
    fixtureDef.density = 0.0;
       
    geyserSensorBody->CreateFixture(&fixtureDef);
    CCLOG(@"geyserSensor created");   

}


Im not sure I understand what the problem is...


Top
 Profile  
 
PostPosted: Wed May 30, 2012 4:22 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
It looks like you are trying to use a b2Body pointer that has not yet been initialized, or has been deleted.
If the problem is specific to cocos2d you might get more help on those forums.


Top
 Profile  
 
PostPosted: Wed May 30, 2012 7:06 pm 
Offline

Joined: Fri Mar 09, 2012 1:43 pm
Posts: 69
Could you explain to me how you got to that conclusion from what i posted? BTW, you were right. :)


Top
 Profile  
 
PostPosted: Thu May 31, 2012 6:15 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
Sure. The error type is EXC_BAD_ACCESS. This means you tried to access memory that you cannot access. This is not specific to Box2D. Consider these examples:

This will crash because the pointer is random data when you try to access it:
Code:
b2Body* body;
b2Vec2 pos = body->GetPosition(); //crash


This will crash because the pointer does not point to a valid b2Body:
Code:
b2Body* body = NULL;
b2Vec2 pos = body->GetPosition(); //crash


This is ok because the pointer points to a valid b2Body:
Code:
b2Body* body = world->CreateBody(&bodyDef);
b2Vec2 pos = body->GetPosition(); //ok


This will crash because the pointer once again does not point to a valid b2Body:
Code:
b2Body* body = world->CreateBody(&bodyDef);
world->DestroyBody(body);
b2Vec2 pos = body->GetPosition(); //crash


If you check google about this there are many many pages to read. Judging from other threads you seem to be quite stuck on this problem and I think your case is the last one above right? Here is one way you could try to avoid accessing the body after you delete it:
Code:
//when destroying a body, also set the pointer to NULL
world->DestroyBody(body);
body = NULL;

//every time you want to use the body somewhere, check if it is not NULL first
if ( body != NULL )
    pos = body->GetPosition();


Top
 Profile  
 
PostPosted: Thu May 31, 2012 7:07 am 
Offline

Joined: Fri Mar 09, 2012 1:43 pm
Posts: 69
I fixed the problem thanks. I forgot to call the createGeyser method from init so yes, when i ran the collision test it crashed :)


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 4 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