Angle of parametric path

Hello!

In my project I am working on I am trying to have an airplane sprite rotate as it moves along a parametric path (figure-8). I want it to always face the direction of the path, but I am having one issue with this. My parametric path is defined as:

x = sin(2t),
y = cos(t)

I calculate the angle with:

angle = -1 * (arcsin(dx / sqrt(dx^2 + dy^2) * (180 / pi))

(where dx and dy are the changes in x and y)

However, when the parametric path crosses itself (on the up-slope and down-slope) the angle stays the same for both cases (when it should be facing ~45 degrees up going counter-clockwise and ~-45 degrees down going clockwise). This is because on the down-slope, the dy becomes negative (which would cause the angle to rotate in the negative direction), but the sign gets lost in the square root and the angle becomes positive.

I was thinking about somehow using an arccos angle instead so that the dy would be on the numerator, but I keep getting stuck.

I can’t figure out how to fix this and I was just wondering if anyone (who is better at trig / math than I am :P) can help me.

Thanks!

Given dx and dy, you can use Mathf.Atan2() to calculate an angle. It would be Mathf.Atan2(dy, dx), and if you were working on the XY plane, the reference would be Vector3.right. I’m not sure how you are using this angle, so you may have to make adjustments to get the angle in the form you desire. Another solution is to turn dx and dy into a Vector. Then you can get the signed angle between this vector and any reference vector. Here is one of references for signed angles between two vectors:

http://answers.unity3d.com/questions/181867/is-there-way-to-find-a-negative-angle.html

My other reference is not coming up (Internal Server Error), so search in a bit for “unity3d signed angle vectors” if you want other solutions.