Keeping Velocity Constant and Player in Position - Sidescrolling

Posted by user2904951 on Stack Overflow See other posts from Stack Overflow or by user2904951
Published on 2013-10-29T11:40:14Z Indexed on 2013/10/31 9:54 UTC
Read the original article Hit count: 169

Filed under:
|

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 :

  1. move this Body with a constant velocity along the x-axis, no matter if it's rolling (it's a circleshape) upwards or downwards
  2. keep the body nearly sticking to the ground on which it's rolling
  3. keep the Body and the according Sprite in the Center of the Screen.

What I tried :

  1. 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 velocity

  2. I used to set very high y-Values in body->SetLinearVelocity(b2Vec2(x,y))

  3. 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...

  1. ...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 in world->Step(...)) there's a small delay, which pushes the player Body more or less further of the Center of the Screen.

  2. ... 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....

  3. ... 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

© Stack Overflow or respective owner

Related posts about cocos2d-iphone

Related posts about box2d