0
votes

I am fairly new to cocos2d and box2d. I am attempting to create a game horizontal endless runner using the accelerometer. I have the character falling from the top and is constantly moving downwards. The problem that i am running into is that overtime the sprite and the physics body become out of sync and the physics body moves away from the sprite thus causing wierd behavior with the collision. This only happens when ran on the device, because the player is constantly moving down, the collision is becoming out of sync vertically and the horizontal remains correct. My step function is :

const float32 timeStep = 1.0f / 30.0f;
const int32 velocityIterations = 5;
const int32 positionIterations = 1;

// step the world
world->Step(timeStep, velocityIterations, positionIterations);
1
Can you tell more details, how out of sync looks? Sceenshots or videos can be usefull.Pavel
Are you sure you are updating the sprites position on every box2D tick ?ekinsol

1 Answers

0
votes

How great a distance are we talking about? If you are moving a Box2D body over a very large distance (thousands of Box2D meters) you may run into inaccuracies in either the physics simulation or cocos2d positions (or both) that could lead to the effect you described.

2D Physics engines aren't designed to work in a world without constraints (ie endless scrolling). If that's what you're doing, you'll have to come up with a solution where the world is constraint but the player doesn't realize this. A common way is to reset the position of objects and associated views back to the top of the (constrained) world once they pass a certain threshold. Most games just lock the player in place and instead move the world underneath the player to give the impression of endless movement, whereas only the background is moving.