I need to find a way to calculate the point P, that is perpendicular do the line segment A-B.
All 3 points (A,B and P) are vector3
I need to find a way to calculate the point P, that is perpendicular do the line segment A-B.
All 3 points (A,B and P) are vector3
In 2 dimensions, that’s easy… because you just rotate 90 degrees. There’s 2 possible directions, clockwise or counter clockwise.
In 3 dimensions you’re talking about what is called a ‘orthogonal vector’. Problem is, for any given 3d vector, there are an infinite number of orthogonal vectors. Hold up a pencil, stick another out the side 90 degrees off, now rotate it around the pencil… ALL of those vectors are possible answers to your question.
So usually you get the orthogonal to 2 vectors, the second vector controlling for which direction you want the orthogonal. And you do that by taking the ‘cross product’:
Do note… if you intend to get a position for p, rather than the just the direction and distance (what a vector represents). You’ll also need to determine the point where the dotted line meets segment AB. Which you’ve yet to define as well.
There’s not enough information. You need to specify the intersecting plane where P exists.
In 2D, it’s easy:
You get the slope of segment AB.
(-1/slope) is the slope of the line perpendicular to it.
You can begin at any point in the segment and at any distance and you’ll find a point P anywhere on that particular line.
That was my bad, you guys are right.
I said they were vector3 but the Z value will allways be 0, so doing it in 2D should give me the needed result
In that case, in more practical terms:
Given a directional Vector2 v.
The perpendicular directional Vector2 is (-v.y, v.x).
OK, so technically it’s 2D.
In which case, to get a vector perpendicular to a 2d vector is a very simple formula:
public static Vector2 Orth(Vector2 v)
{
return new Vector2(-v.y, v.x);
}
You can just do that with a Vector3 that has 0 in z.
You can then get a point on AB. We just need some extra info…
Vector2 A = *point A*;
Vector2 B = *point B*;
float midPointPercentage = 0.5f; //how far from A to B are we going, 0.5 is half-way
float distanceFromAB = 1f; //how far from AB should P be?
var vAB = B - A; //vector from A to B
var midPoint = A + vAB * midPointPercentage; //midpoint, the point P is actually perpendicular
var vABToP = new Vector2(-vAB.y, vAB.x); //vector in the direction of P from AB
vABToP.Normalize(); //make it a unit vector for ease
var P = midPoint + vABToP * distanceFromAB;