Computing orbit direction depending on angle of approach

I'm trying to develop a simple 2D space game with no friction using . In the game, I have several planets which all applies forces to the player according to Newton's law of universal gravitation. Once the player is close enough to a planet, I need to put him in orbit. I do so, by creating a pivot joint between the planet and the player. From the player's liner velocity, I compute the joint's motor speed in order to achieve an as much as possible smooth cyclic rotation around the planet.

The solution, which might not be optimal (in future versions I would like to avoid completely the joint), so far is working but I'm not able to determine correctly the joint's rotation direction. The rotation of the player around the planet should be CW or CCW depending on the player's angle of approach. How do I compute that ?

Thanks !

I'd construct a line from the centre of the planet to the ship, and also calculate a point which is the ship's position + ship's velocity.

From there you can use a standard test to find out which side of the line the point is on, which tells you if it should orbit clockwise or anticlockwise.

• Pretty clear ! I've tested it and it is working as expected. Thank you very much ! Jan 12, 2013 at 22:16