Search code examples
javabox2dgame-physicstrigonometry

Making Character move around circle


So I have a 2d game which normally just has gravity and "flat" levels however I have added in "planets" which have their own gravity.

I have a function called addForce(float xForce, float yForce) that I use to move my character. So say if I called player.addForce(1, -1); the player would move up and to the right(albeit slightly). This worked fine on the levels with regular downward gravity, however with planets it is not so. There is another float called earthAngle which is:

atan2(player.getY()-earth.getY(), player.getX()-earth.getX());

What I did for the jumping code on the planets is:

player.addForce(cos(earthAngle)*1500, sin(earthAngle)*1500);

which works well. However I am stuck on how to make the character walk around the planet.

Currently for the movement code I have:

player.addForce(25*x_*cos(earthAngle), 25+x_*sin(earthAngle));

which only works on some parts and works in reverse on the bottom as well as being stronger/weaker on some parts, x_ can be either -1(left) or 1(right). I'm guessing their is a really elegant solution I am just overlooking. Thanks.


Solution

  • Since you already have the vector from the center of the planet to the player eg (x,y), you can use a vector perpendicular to that (-y,x) as the direction for the walking force.