Problem in moving body through NSTimer

Official forum for the Chipmunk2D Physics Library.
Maggi
Posts: 10
Joined: Tue Apr 26, 2011 3:21 pm
Contact:

Re: Problem in moving body through NSTimer

Post by Maggi »

Okay, so after u informed me to remove and free the body and shapes, i defined all those variables in header file, so that i can use it anywhere to remove and free the body and shapes.
I think i am not calling remove and free at proper place.
Can you tell me where do i need to call remove and free??
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem in moving body through NSTimer

Post by slembcke »

You just need to do it before you add the new balls.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Maggi
Posts: 10
Joined: Tue Apr 26, 2011 3:21 pm
Contact:

Re: Problem in moving body through NSTimer

Post by Maggi »

-(void)setupChipmunk {

NSLog(@"SetupChipmunk");
cpSpaceRemoveShape(space, ballShape);
cpSpaceRemoveBody(space, ballBody);
cpShapeFree(ballShape);
cpBodyFree(ballBody);

cpSpaceRemoveShape(space, ball1Shape);
cpSpaceRemoveBody(space, ball1Body);
cpShapeFree(ball1Shape);
cpBodyFree(ball1Body);

//---------------------------------------------------------------------//
//Create ball's body
ballBody = cpBodyNew(100.0, INFINITY);
//Set the initial position
ballBody->p = cpv(50, 450);
// Add the body to the space
cpSpaceAddBody(space, ballBody);

//Create our shape associated with the ball's body
ballShape = cpCircleShapeNew(ballBody, 10.0, cpvzero);

ballShape->e = 0.5; //Elasticity
ballShape->u = 0.8; // Friction
ballShape->data = ball; // ball's image
ballShape->collision_type = 1;

// Add the shape to our space
cpSpaceAddShape(space, ballShape);

//-------------------------------------------------------------------//
//Create ball1's body
----- creating 2nd ball---------
}

This is way i remove and free shapes and bodies.
It is giving me: Program receive signal: "EXC_BAD_ACCESS" and break my proj.

Please suggest..!!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem in moving body through NSTimer

Post by slembcke »

Unless you did something special in some code you didn't paste here, it will crash the first time you run that method. You need to put if(ballBody != NULL), etc checks in there.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Maggi
Posts: 10
Joined: Tue Apr 26, 2011 3:21 pm
Contact:

Re: Problem in moving body through NSTimer

Post by Maggi »

Great.!! it worked now..
Thank you very much..
I really appreciate your help.
Thanks again..
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Re: Problem in moving body through NSTimer

Post by Buschmaster »

Just another quick consideration for you, you shouldn't use NSTimer for a game run loop. The best I've found is a CADisplayLink.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Problem in moving body through NSTimer

Post by slembcke »

To add to the last comment. NSTimers work fine for long running timers (like your 4 second one), but for screen refreshes the CADisplay link will give you a much smoother framerate as it calls your code from the video hardware each time it finishes refreshing the screen.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Maggi
Posts: 10
Joined: Tue Apr 26, 2011 3:21 pm
Contact:

Re: Problem in moving body through NSTimer

Post by Maggi »

Thanks for your info..
I will work on it.
Thanks again..:)
Buschmaster
Posts: 71
Joined: Sat Dec 20, 2008 12:16 pm
Location: Minnesota
Contact:

Re: Problem in moving body through NSTimer

Post by Buschmaster »

No problem, and thanks for the addition, Scott.

The CADisplayLink is actually pretty simple and you'll just want to have it call your tick method :)
Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests