issues with making a box2d body move in a straight line

Discuss issues specific to using Box2D on the iPhone
gamist
Posts: 17
Joined: Wed May 30, 2012 11:05 am

issues with making a box2d body move in a straight line

Postby gamist » Wed Apr 17, 2013 2:06 am

I am having a few issues with making a box2d body move in a straight line. At times when I run the code it works (body moves in a straight line) and other times it falls as it moves. Below is the scheduleSprite method which I call in the update method.

Code: Select all

-(void)scheduleSprite {

if ([testSprite physicsSprite].b2Body != NULL) {
    b2Vec2 force = b2Vec2(-5, 10.0 * [testSprite physicsSprite].b2Body->GetMass());
    [testSprite physicsSprite].b2Body->ApplyForce(force, [testSprite physicsSprite].b2Body->GetWorldCenter() );
}

if ([testSprite physicsSprite].position.x < 0) {
    world->DestroyBody([testSprite physicsSprite].b2Body);
    [testSprite physicsSprite].b2Body = NULL;
}
}

Also, when the second if condition is satisfied I get an EXC_BAD_ACCESS pointing to line

Code: Select all

b2Vec2 pos  = _b2Body->GetPosition();

in the

Code: Select all

-(CGAffineTransform) nodeToParentTransform


method, within the class

Code: Select all

CCPhysicsSprite.mm


It would appreciate any assistance.

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: issues with making a box2d body move in a straight line

Postby irresistible force » Wed Apr 17, 2013 3:50 pm

I don't know how the CCPhysicsSprite works, but it's probably a bad idea to just set the b2Body member to NULL like that....
There must be a correct way to destroy the body and clean up everything. Check the docs or an example usage.


Return to “iPhone”



Who is online

Users browsing this forum: No registered users and 1 guest