# Math to set tangential force?

I have an 2-dimensional planet at coordinate (pX, pY) and an object at the surface of the planet at some coordinates (oX, oY). I want to have the object go left or right when the user press the keys, which I'd like to simulate by applying a force. I have a homemade physics engine built and all I'm missing is the x/y amounts to use to apply the force in a tangential direction.

What calculation is needed to apply a tangential force clockwise/counterclockwise on an object at (oX, oY) around a center of (pX, pY)?

You need a unit vector tangent to the planet surface:

v = normalize((pY - oY, oX - pX))


Then just multiply v by your force amount.

• How does the normalize function work? – Aaron Franke Apr 27 '17 at 4:33
• @AaronFranke Maybe this link can explain properly. – sam hocevar Apr 27 '17 at 8:52

First, calculate the relative coordinates using rX = oX - pX and rY = oY - pY

Next, we'll find the angle. Use the Atan2 method: angle = atan2(rY, rX)

Next, more trigonometry! We should be able to do totalforce * sin(angle) to find the amount of force to be applied on the X-axis and totalforce * -cos(angle) to find the amount of force to be applied on the Y-axis. Change totalforce to be positive or negative based on the direction desired.

• While your answer gives the correct result, calling sin or cos after atan2 is an indication that there is a much simpler operation available, avoiding trigonometry altogether. – sam hocevar Apr 26 '17 at 18:38