- Home /
How do i find the normal of a line between two points?
I am trying to calculate the normal of a line between two points. The points are world-space vectors from two Raycast hits.
But I am having trouble calculating the yellow normal shown in the picture below.
Any help would be much appreciated.
Answer by Bunny83 · Nov 16, 2018 at 11:14 PM
Since this is just a 2d problem the solution is actually quite trivial ^^. To get the green line, just subtract the two hit points from each other. To rotate a 2d vector by 90° you just have to swap the x and y values and invert one of them. Which one you invert dictates the direction you're rotating (clockwise / counter clockwise).
Vector2 green = hitLeft - hitRight;
// rotate 90° Clockwise
Vector2 normal = new Vector2(green.y, -green.x);
You may want to normalize the result in case you need a normalized vector.
Note that the rotation direction depends on the coordinate system. I assumed screen space where the origin is in the bottom left and the positive axis go right (x) and up (y).
This worked like a charm! Just what I was looking for. Didn't realize it was this trivial, might have overcomplicated things a bit ^^. Thank you very much for the quick and accurate answer!