Math problem. Is given angle between two other angles.

Hi guys,

Math isn’t my strong suit. Really need some help with the following:

I have 3 vectors (all aligned on the same single (y) axis …VectorToCheck, Vector1 and Vector2.

I want to come up with code to work out if VectorToCheck is in between Vector1 and Vector2.
So far I have:

IsAngleBetween(7.0f, -14.0f, 28.0f) // returns TRUE.

bool IsAngleBetween(float angleToCheck, float angle1, float angle2)
{
// All angle parameters have been generated using Vector3.Angle followed by Vector3.Cross (to obtain sign of angle).

return … // Need this code here…
}

Head has hurt trying to work this one out…would love some help.

You might be able to use Mathf.DeltaAngle() for this.

However, it’s likely you could avoid dealing with angles and/or the periodicity issue entirely by using a different approach. What problem is it exactly that you’re trying to solve?

Hi Jesse,

I have 3 vectors:

VectorToCheck
VectorA
VectorB

which all originate from the same point and are all aligned along the X/Z plane.
Ultimately I need a function:

// Returns true if vectorToCheck is inbetween VectorA and VectorB.

bool IsVectorBetween(Vector3 vectorToCheck, Vector3 vectorA, Vector3 vectorB);

Perhaps I need to provide my information to help solve this problem, if you think I do, please let me know.

Although that information is useful, I think what would be most useful is if you could specify why you need to perform this test. For AI? Field-of-view test? Something else? With that information available, it’ll be easier to point you in the right direction.

Oh I see what are you asking. It’s because I allow a user to drag a particular game-object with the mouse. User clicks mouse button, I record the position of said object…user drags object, user releases mouse button and using the end position of object I can calculate this vector. Note the game is in 2d, all objects are dragged along the x/z plane only.

I need to see if the user has dragged this object within an pre-defined angle range from where it was first dragged.

Although I don’t completely understand the specifications, you should be able to figure this out by computing signed angles as needed. Although there are slightly more straightforward ways to do it in 2-d, an easy way to compute the signed angle between two vectors in this context would be to use Unity’s ‘unsigned angle’ function, and then determine the sign by taking the dot product of the global up axis and the cross product of the two input vectors.

Hi again Jesse, I was already acquiring signed angles, using exactly the method you describe. In fact, your previous suggestion of looking into Mathf.DeltaAngle was the clue I was looking for.

Put my thinking cap on and came up with the following solution:

bool IsAngleBetween(float angleToCheck, float angle1, float angle2)
{
// All angle parameters are signed (have been generated using Vector3.Angle followed by Vector3.Cross).

float smallestAngle = Mathf.abs(Mathf.deltaAngle(angle, angle2));

float angle1Delta = Mathf.abs(Mathf.deltaAngle(angleToCheck, angle1));
float angle2Delta = Mathf.abs(Mathf.deltaAngle(angleToCheck, angle2));

// return TRUE if angleToCheck is between the SMALLEST range between angle1 and angle2).

return ((angle1Delta + angle2Delta) <= smallestAngle);
}

Thankyou very much for your help Jesse, Prob still be working on the prob otherwise.