I have three points, A, B and C (stored as Vector2 in Unity). I am trying to find the angle at point B if there were a line AB and and a line BC. I know, this should be a simple google search, and I have found several methods but for some reason, they ALL return totally unexpected results with my data set!
I checked to make sure that the three points I pass to compute the angles are indeed the correct (x,y) coordinates, and that same data is used elsewhere in my code without any problems.
Considering that all functions return strange results, it must be that I am not understanding something here...
here is one formula:
Vector2 A = //some vector2.normalized
Vector2 B = //some vector2.normalized
Vector2 C = //some vector2.normalized
float Angle = Mathf.Atan2(B.y - A.y, B.x - A.x) - Mathf.Atan2(B.y - C.y, B.x - C.x);
Here's another that I tried where p0,p1,and p2 and just vector2's:
function findAngle(p0, p1, p2) {
var a = Math.pow(p1.x - p0.x, 2) + Math.pow(p1.y - p0.y, 2),
b = Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2),
c = Math.pow(p2.x - p0.x, 2) + Math.pow(p2.y - p0.y, 2);
return Math.acos((a + b - c) / Math.sqrt(4 * a * b));
}
The data is a List of Vector2 control points for creating Bezier curves. I want to calculate the angle between index 0,1,2 then 2,3,4 then 4,5,6 etc. with the middle index as the angle needed.
I understand that both formulas return radians, but converting to degrees returns sometimes 180 for all points even though the points are all different and the angles vary greatly, sometimes 0 (if I switch the points around), and in the case of the atan2 function above, it gives numbers that "look" like angles, but plotting the points, one can see that the angles do not correspond to the visual plots... I must be doing something very dumb here and I'm not great with math so any hand-holding is much appreciated.