'How to calculate the closest point onto an edge segment in C++ and GLM?
I'm developing a 3d collision pipeline in C++ using OpenGL and GLM, however I cannot seem to figure out a formula for calculating the closest point (the player origin) to a triangle's edge segment.
For example, say you have vertex "A" and vertex "B" constructing the edge, point "C" representing the player origin and "D" being the closest point from "C" to the edge. How would one go about calculating this? I understand that by getting the cross product of (b - a) and (c - a) can help with edge alignment, however I need to get the coordinate in 3D Cartesian space, not a comparison.
Here is a diagram to illustrate my problem.
I need to find "Point on edge". Example code in the form of GLM or similar would be much appreciated! This question differs to the original as this refers to the C++ language and GLM library for vector math.
Thanks!
Solution 1:[1]
Was going to show you, but here is my 5 seconds of googling:
http://www.sunshine2k.de/coding/java/PointOnLine/PointOnLine.html
See example 5. Even haz the c0d3z. Even if they are in Java. I hope you can figure out how to C that...
Solution 2:[2]
You need to find the a to b normalized direction: dir = (b-a) / length(b-a)
Then the projection length: pl = (p-a) dot dir
and finally the closest point = a+dir*pl
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael Dorgan |
| Solution 2 | Yigal Eilam |

