0
votes

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

2
this will be difficult to accomplish with box2d, you'll find some explanations here: learn-cocos2d.com/2013/08/…LearnCocos2D

2 Answers

0
votes
  1. To make it easy to control the body, the main figure to which the force is applied should be round. This should be done because of the processing mechanism of collisions. More details in this article: Why does the character get stuck?. For processing collisions with the present contour of the body you can use the additional fixtures and sensors with an id or using category and mask bits. For of constant velocity is often better to use SetLinearVelocity, because even when using impulse velocity gets lost at sharp uphill or when jumping. If you want to use the implulse to change the position of the body, then you need to use the code for the type of this:

    b2Vec2 vel = m_pB2Body->GetLinearVelocity();
    float desiredVel = mMoveSpeed.x; //set there your speed x value 
    float velChange = desiredVel - vel.x;
    float impulse = m_pB2Body->GetMass() * velChange;
    m_pB2Body->ApplyLinearImpulse( b2Vec2(impulse, mMoveSpeed.y), m_pB2Body->GetWorldCenter());
    

    This will allow maintain a constant speed most of the time. Do not forget that these functions must be called every time in your game loop. You can combine these forces, depending on the situation. For example, if the at the beginning you need to make a small acceleration, it is possible to use ApplyForce to the body, and when a desired speed is to use ApplyLinearImpulse or SetLinearVelocity. How correctly to use it is described here: Moving at constant speed

  2. If you use world with the normal gravity(b2Vec2(0, -9.81)), then it should not be a problem.

  3. I answer for this question here: Cocos2D-x - Issues when with using CCFollow. I use this code, it may be useful to you:

    CCPoint position = ccpClamp(playerPosition, mLeftBounds, mRightBounds);
    CCPoint diff = ccpSub(mWorldScrollBound, mGameNode->convertToWorldSpace(position));
    CCPoint newGameNodePosition = ccpAdd(mGameNode->getPosition(), mGameNode->getParent()->convertToNodeSpace(diff));
    mGameNode->setPosition(newGameNodePosition);
    

P.S. If you are new to box2d, it is advisable to read all the articles iforce2d(tuts), they are among the best in the network, as well as his Box2D Editor - RUBE. At one time they really helped me.

0
votes

I do not know if this is possible but I have an idea:

Keep the circle at a fixed position and move the background relatively. For example, during the course of the game, if the circle has a velocity of 5 towards left then keep circle fixed and move screen with velocity 5 towards right. If circle has 5 velocity towards left and screen has 3 velocity towards right, then keep circle fixed and move screen with 8 velocity towards left and so on. This should allow you to fix the circle at the center of the screen.

Another method would be to translate the entire screen along with the ball. Make everything on the screen an object that can have a velocity. And the x-component of the velocity of the ball (circle) should be the velocity of all other objects. This way, whenever the circle moves, all the other objects will try and keep up with it.