Problem in moving body through NSTimer

Official forum for the Chipmunk2D Physics Library.

Problem in moving body through NSTimer

Postby Maggi » Tue Apr 26, 2011 3:35 pm

Hello Pple,
i am new to chipmunk and my project includes motion of ball from top to bottom every given seconds(lets say 4 secs).
To do that, i call chipmunk function every 4 secs using timer function.
If i run this proj, timer gets called but the motion of ball is very irregular. Sometimes it moves, sometimes it doesnt.
I would really appreciate it anyone can help.
Here is my code:

-(void)viewDidLoad {
[super viewDidLoad];
lwall = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Left_wall.png"]];
lwall.center = CGPointMake(10, 250);

rwall = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Right_wall.png"]];
rwall.center = CGPointMake(40, 250);
ball = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"slider_ball.png"]];
ball.center = CGPointMake(100, 100);

ball1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"slider_ball.png"]];
ball1.center = CGPointMake(400, 150);


[self.view addSubview:rwall];
[self.view addSubview:lwall];
[self.view addSubview:ball];
[self.view addSubview:ball1];
[self setup];
}

-(void)setup {
//Start Chipmunk
cpInitChipmunk();

//Create a space object
space = cpSpaceNew();

// Add some elastic effects to the simulation
space->elasticIterations = 3;

//Define gravity vector
space->gravity = cpv(0, -100);

[self addWallsFloor];

NSLog(@"Timer start");

//Add the new ball timer
[NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(timesetup:) userInfo:nil repeats:YES];

//Add the tick
//Creates a timer firing at a constant interval (desired frame rate)
[NSTimer scheduledTimerWithTimeInterval:1.0f/120.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}

-(void)timesetup:(NSTimer*)timer {
[self setupChipmunk];
}

// Below function has static bodies which has tons of coordinates. and hence i am not displaying it here
// This function adds the left and right wall where ball collides while moving down
- (void) addWallsFloor{
}

-(void)setupChipmunk {

NSLog(@"SetupChipmunk");
//---------------------------------------------------------------------//
//Create ball's body

cpBody *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
cpShape *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
cpBody *ball1Body = cpBodyNew(100, INFINITY);

//Set up initial position
ball1Body->p = cpv(140,480);

cpSpaceAddBody(space, ball1Body);

//Create our shape associated with ball's body
cpShape *ball1Shape = cpCircleShapeNew(ball1Body, 10.0, cpvzero);
ball1Shape->e = 0.5;
ball1Shape->u = 0.8;
ball1Shape->data = ball1;
ball1Shape->collision_type = 3;
cpSpaceAddShape(space, ball1Shape);
}

-(void)tick:(NSTimer *)timer {
cpFloat dt = 1.0f/120.0f;
cpSpaceHashEach(space->activeShapes, &updateShape,nil);
cpSpaceStep(space, dt);
cpSpaceRehashStatic(space);
}

void updateShape(void *ptr, void* unused) {
//Get our shape
cpShape *shape = (cpShape*)ptr;

if(shape == nil || shape->body == nil || shape->data == nil) {
NSLog(@"Unexpected shape please debug here... ");
return;
}
//Check if the object is an UIView of any kind and update its position
if([shape->data isKindOfClass:[UIView class]]) {
[(UIView*)shape->data setCenter:CGPointMake(shape->body->p.x, 480-shape->body->p.y)];
}
else
NSLog(@"The shape data wasn't updateable using the code.");
}
Maggi
 
Posts: 10
Joined: Tue Apr 26, 2011 3:21 pm

Re: Problem in moving body through NSTimer

Postby Maggi » Tue Apr 26, 2011 3:55 pm

Just wanted to inform that there are 2 balls named: ball and ball1.
THanks
Maggi
 
Posts: 10
Joined: Tue Apr 26, 2011 3:21 pm

Re: Problem in moving body through NSTimer

Postby slembcke » Tue Apr 26, 2011 4:04 pm

What do you mean it's irregular? The framerate is jerky?

Another strange thing, why are you calling cpSpaceRehashStatic() every frame? You would only need to do that if you were moving static objects every frame which is not recommended anyway.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Problem in moving body through NSTimer

Postby Maggi » Tue Apr 26, 2011 4:08 pm

irregular means, first time ball will move at 4 secs, next after 8th sec it will move, but it wont move at 12th secs,
It will move next at 20th sec.
Also, there are two balls which are suppose to move. Sometimes, just one ball will move..Its very weird

It is not moving at every 4 secs.
and sorry about keeping cpSpaceRehashStatic().
i forgot to remove that.
Maggi
 
Posts: 10
Joined: Tue Apr 26, 2011 3:21 pm

Re: Problem in moving body through NSTimer

Postby Maggi » Wed Apr 27, 2011 8:26 am

Hey slembcke, did u get my problem??
I would really appreciate if u can help me out.
Maggi
 
Posts: 10
Joined: Tue Apr 26, 2011 3:21 pm

Re: Problem in moving body through NSTimer

Postby slembcke » Wed Apr 27, 2011 11:32 am

Oh, so what are you doing with the old bodies and shapes for the balls? I don't see where you are removing them or freeing them at all. You probably have several objects overwriting the position of your ball graphic.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Problem in moving body through NSTimer

Postby Maggi » Wed Apr 27, 2011 12:31 pm

Thanks for your reply..
Where do i need to release ballBody and ballShape??
Is it something needs to be done in the function where i create bodies and shapes??
Maggi
 
Posts: 10
Joined: Tue Apr 26, 2011 3:21 pm

Re: Problem in moving body through NSTimer

Postby slembcke » Wed Apr 27, 2011 1:27 pm

Yes, you should remove and free the old ones before replacing them with new ones.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Problem in moving body through NSTimer

Postby Maggi » Wed Apr 27, 2011 2:12 pm

thanks for your reply.
So, i tried doing like this for both the balls:
cpSpaceRemoveShape(space, ballShape);
cpSpaceRemoveBody(space, ballBody);
cpShapeFree(ballShape);
cpBodyFree(ballBody);

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

Now, there is no motion of balls.

Where am i going wrong??
Can you throw some light on it?
Maggi
 
Posts: 10
Joined: Tue Apr 26, 2011 3:21 pm

Re: Problem in moving body through NSTimer

Postby slembcke » Wed Apr 27, 2011 3:05 pm

ballBody, ballShape, etc were local variables before. Did you make them instance variables or are you just referencing the local variables?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Next

Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 1 guest

cron