Problem in moving body through NSTimer
Posted: 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.");
}
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.");
}