Cocos2d: Changing b2Body x val every frame causes jitter

Posted by Joey Green on Game Development See other posts from Game Development or by Joey Green
Published on 2012-06-19T14:57:13Z Indexed on 2012/06/19 15:26 UTC
Read the original article Hit count: 479

Filed under:
|

So, I have a jumping mechanism similar to what you would see in doodle jump where character jumps and you use the accelerometer to make character change direction left or right.

I have a player object with position and a box2d b2Body with position. I'm changing the player X position via the accelerometer and the Y position according to box2d.

pseudocode for this is like so

-----accelerometer acceleration------ player.position = new X

-----world update--------- physicsWorld->step() //this will get me the new Y according to the physics similation //so we keep the bodys Y value but change x to new X according to accelerometer data playerPhysicsBody.position = new pos(player.position.x, keepYval) player.position = playerPhysicsBody.position

Now this is simplifying my code, but I'm doing the position conversion back and forth via mult or divide by PTM_.

Well, I'm getting a weird jitter effect after I get big jump in acceleration data.

So, my questions are: 1) Is this the right approach to have the accelerometer control the x pos and box2d control the y pos and just sync everthing up every frame?

2) Is there some issue with updating a b2body x position every frame?

3) Any idea what might be creating this jitter effect?

I've collecting some data while running the game. Pre-body is before I set the x value on the b2Body in my update method after I world->step(). Post of course is afterwards. As you can see there is definitively a pattern.

012-06-19 08:14:13.118 Game[1073:707] pre-body pos 5.518720~24.362963

2012-06-19 08:14:13.120 Game[1073:707] post-body pos 5.060156~24.362963

2012-06-19 08:14:13.131 Game[1073:707] player velocity x: -31.833529

2012-06-19 08:14:13.133 Game[1073:707] delta 0.016669

2012-06-19 08:14:13.135 Game[1073:707] pre-body pos 5.060156~24.689455

2012-06-19 08:14:13.137 Game[1073:707] post-body pos 5.502138~24.689455

2012-06-19 08:14:13.148 Game[1073:707] player velocity x: -31.833529

2012-06-19 08:14:13.150 Game[1073:707] delta 0.016667

2012-06-19 08:14:13.151 Game[1073:707] pre-body pos 5.502138~25.006948

2012-06-19 08:14:13.153 Game[1073:707] post-body pos 5.043575~25.006948

2012-06-19 08:14:13.165 Game[1073:707] player velocity x: -31.833529

2012-06-19 08:14:13.167 Game[1073:707] delta 0.016644

2012-06-19 08:14:13.169 Game[1073:707] pre-body pos 5.043575~25.315441

2012-06-19 08:14:13.170 Game[1073:707] post-body pos 5.485580~25.315441

2012-06-19 08:14:13.180 Game[1073:707] player velocity x: -31.833529

2012-06-19 08:14:13.182 Game[1073:707] delta 0.016895

2012-06-19 08:14:13.185 Game[1073:707] pre-body pos 5.485580~25.614935

2012-06-19 08:14:13.188 Game[1073:707] post-body pos 5.026768~25.614935

2012-06-19 08:14:13.198 Game[1073:707] player velocity x: -31.833529

2012-06-19 08:14:13.199 Game[1073:707] delta 0.016454

2012-06-19 08:14:13.207 Game[1073:707] pre-body pos 5.026768~25.905428

2012-06-19 08:14:13.211 Game[1073:707] post-body pos 5.469213~25.905428

2012-06-19 08:14:13.217 Game[1073:707] acceleration x -0.137421

2012-06-19 08:14:13.223 Game[1073:707] player velocity x: -65.022644

2012-06-19 08:14:13.229 Game[1073:707] delta 0.016603

© Game Development or respective owner

Related posts about box2d

Related posts about cocos2d