0
\$\begingroup\$

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)?

\$\endgroup\$
2
\$\begingroup\$

You need a unit vector tangent to the planet surface:

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

Then just multiply v by your force amount.

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

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – sam hocevar Apr 26 '17 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.