0
\$\begingroup\$

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.

Here's an image of the output: enter image description here

\$\endgroup\$
  • \$\begingroup\$ It should be added that when I plug in simple points like (1,0), (0,0) and (0,1), the functions return 90 degrees which is correct obviously. \$\endgroup\$ – Dbilyk Jul 3 '17 at 0:52
2
\$\begingroup\$

Just use the built-in Angle method:

Vector2.Angle(A - B, C - B);

From the docs:

Vector2.Angle

public static float Angle(Vector2 from, Vector2 to);

Description

Returns the angle in degrees between from and to.

\$\endgroup\$
  • \$\begingroup\$ Hey! Thanks for your response! I tried that one too, written exactly as you put it, but it also gives strange results! the angles are simply incorrect, and not in any obvious way either, like all are 90 off, or anything like that. I'm going to upload an image to show the plot and output so you can see! :) \$\endgroup\$ – Dbilyk Jul 3 '17 at 2:21
  • \$\begingroup\$ The output you've shown in your question does not match the output of this method. (eg. the first trio gives a result of 86.8 degrees, not 50.7), so it's unclear to me what specific error you're encountering. If you find this method does not give the results that you expect, can you please edit your question to include a table of inputs and expected outputs? That should help us track down where the confusion is originating. \$\endgroup\$ – DMGregory Jul 3 '17 at 19:18
  • \$\begingroup\$ Thanks for pitching in, the problem was elsewhere in my code since Vector2.Angle() is pretty bullet proof. It's a bit to hairy to explain in detail why it wasn't working but suffice it to say, it was a combination of stray negative signs and my data was being modified before the function that I was using had a chance to crunch the original data. Thank you guys for chiming in though, greatly appreciate it! \$\endgroup\$ – Dbilyk Jul 4 '17 at 6:08

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.