Determining relative orientations with Vector3.Cross

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?

Your method doesnt have enough variables
A Line segment is defined by startPoint and EndPoint (or start and direction), and you have 2 line segments, you need 4 input varaibles.
Slightly simpler is just comparing a single point vs a line segment (using determinant):

public bool isLeft(Vector2 segPoint1, Vector2 segPoint2, Vector2 testPoint){
     return ((segPoint2.x - segPoint1.x)*(testPoint.y - segPoint1.y) - (segPoint2.y - segPoint1.y)*(testPoint.x - segPoint1.x)) > 0;
}

Do this for both points on the second seg, if both test points have the same ‘isLeft’, then the entire lineseg is on same side

Oh hey, that works perfectly, and it’s considerably cleaner than my implementation to boot- thank you so much! :slight_smile: