1
votes

I just started doing Box2d programming. I'm trying to create a platform game. When the player (dynamic body) jump and landed on the moving platform (kinematic body), I need to make the player stay and move together with the platform. So i try using distance joint to "stick" the player to the platform. However, I've set the anchor point-Y so that the player will stay above the platform but I don't want to set a fix X. What approach should i use?

eg:

  • A.When player jump from right to left, it will land on left.
  • B. When player jump from left to right, it will land on right.

So when the player jump from any position, the X will be based on the player except Y is fix. Thank you.

enter image description here

b2DistanceJointDef jointDef;

b2Vec2 playerAnchor = curPlayerBody->GetWorldPoint(b2Vec2( 1, -0.8));


jointDef.Initialize( curPlayerBody, curPlatformBody, playerAnchor, curPlatformBody->GetWorldCenter() );
jointDef.length = 0;
jointDef.dampingRatio = 0;
jointDef.frequencyHz = 0;
jointDef.collideConnected = false;
playerPlatformJoint = (b2DistanceJoint*)world->CreateJoint(&jointDef);
2

2 Answers

2
votes

I am assuming that you don't want to fix X because you want the player to move along the X axis on the platform.

I don't think the distance joint is a good solution for this.

One way to do what you want could be to set the friction of the player and platform fixture to a level that makes them "stick" together when the platform is moving. You should also have a look at the Friction Joint which let's you set friction in a more dynamic way. You can learn more about friction and the friction joint in the Fixture and Joint chapters of the Box2D manual.

Another way to do what you want would be to synchronize the movement of your player and the platform. But that depends a bit on how you move the platform and the player. If you are doing it by impulse for example, you could apply the same impulse that you apply to the platform to your player additionally to whatever you already apply to the player. You'll have to take properties like mass into account though.

Also take care on how you move the platform. If you are manually setting the position of the platform instead of using impulses, velocity or forces it will cause problems with the mentioned solutions.

0
votes

I managed to find the answer. The idea is using weld joint and manually move the player.

Referenced from here