I'm working on a Little Mobile Game with Cocos2D-X and Box2D.
The Point where I got stuck is the movement of a box2d-body (the main actor) and the according Sprite. Now I want to :
- move this Body with a constant velocity along the x-axis, no matter if it's rolling (it's a circleshape) upwards or downwards
- keep the body nearly sticking to the ground on which it's rolling
- keep the Body and the according Sprite in the Center of the Screen.
What I tried :
in the update()- method I used
body->SetLinearVelocity(b2Vec2(x,y))
to higher/lower values, if the Body was passing a constant value for his velocityI used to set very high y-Values in
body->SetLinearVelocity(b2Vec2(x,y))
First tried to use CCFollow with my playerSprite, which was also Scrolling along the y-axis, as i only need to scroll along the x-axis, so I decided to move the whole layer which is containing the ambience (platforms etc.) to the left of my Screen and my Player Body & Player sprite to the right of the Screen, adjusting the speed values to Keep the Player in the Center of the Screen.
Well...
...didn't work as i wanted it to, because each time i set the velocity manually (I also tried to use
body->applyLinearImpulse(...)
when the Body is moving upwards just as playing around with the value of velocityIterations inworld->Step(...)
) there's a small delay, which pushes the player Body more or less further of the Center of the Screen.... didn't also work as I expected it to, because I needed to adjust the x-Values, when the Body was moving upwards to Keep it not getting slowed down, this made my Body even less sticky to the ground....
... CCFollow did a good Job, except that I didn't want to scroll along the y-axis also and it Forces the overgiven sprite to start in the Center of the Screen. Moving the whole Layer even brought no good results, I have tried a Long time to adjust values of the movement Speed of the layer and the Body to Keep it negating each other, that the player stays nearly in the Center of the Screen....
So my question is :
Does anyone of you have any Kind of new Approach for me to solve this cohesive bunch of Problems ?
Cheers,
Seb