- Home /
Calculate point coords which lays on normal vector
Hello. I have A and B points which z coord the same. I need calculate point C coords between this two points and calculate D point coords, which lays on normal vector to AB vector, and on length l.
I can calculate C point coords using A.transform.position + (B.transform.position - A.transform.position) / 2f but how can I do other stuff (calculate D point coords)? (this is 2d game and z coords always 0 for all points) thanks.
Answer by Prodigga · May 16, 2013 at 02:03 AM
If you get a cross between (B-A) and whatever is the 'up' vector for your points (the vector pointing to you from your screen), then this will give you a vector (which we will call 'N') that points perpendicular to the line A->B. If you normalize 'N', then
D = C + N * distance to D
Thanks. But can you explain a little bit more about how can I get N vector which will be perpendicular to AB using only world forward direction and A and B coords? Thanks again!
Answer by robertbu · May 16, 2013 at 03:23 AM
As @Prodigga indicates, the cross product is a good way to go to solve this problem. Given you have positions A and position C (which you calculate in your question) here is a bit of code:
var v3AC = v3A - v3C;
var v3D = v3C + (Vector3.Cross(v3AC, Vector3.forward)).normalized;
You can change which side of the line the point is on by either changing the order of the parameters in the Vector3.Cross() or by using Vector3.back instead of Vector3.forward.
Your answer
Follow this Question
Related Questions
Convert 3D model to point cloud - possible? 1 Answer
How to find perpendicular line in 2D? 3 Answers
Move to a clicked point? 1 Answer
Get new Point from Vector and Angle 1 Answer
2D Array for pathfinding 0 Answers