I’m working on (what should be) a simple function to determine which side of a given line segment another line segment is located on. My expectation is that the following calculation returns a positive number if line segment dirB is located to the right of dirA, and a negative number if dirB is to the left.
public float GetRelativeDirection(Vector3 dirA, Vector3 dirB)
{
Vector2 A = new Vector2(dirA.x, dirA.z); //We're only concerned about orientation in the X and Z planes
Vector2 B = new Vector2(dirB.x, dirB.z);
Vector3 cross = Vector3.Cross(dirA.normalized, dirB.normalized);
float relativeDirection = Vector3.Dot(cross, Vector3.up);
return relativeDirection;
}
To test it, I fed the system a bunch of squares, bisected them, and tried to categorize each line segment relative to the bisecting line. Here’s a visualization of the results- the bisecting line in each square is green, line segments with a positive GetRelativeDirection are blue, negative are cyan:
My expectation was that each square would have all cyan lines on one half of the line, and all blue on the other half. Obviously that isn’t happening- the vast majority of line segments are getting a negative result, which makes me assume that my math is badly off- am I completely mis-understanding how dot and cross are supposed to be applied?