-1

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.

enter image description here

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!

DrStrange
  • 94
  • 1
  • 9
  • This is probably what you are looking for https://stackoverflow.com/questions/5204619/how-to-find-the-point-on-an-edge-which-is-the-closest-point-to-another-point – Killzone Kid Feb 26 '18 at 23:53
  • Dot products will give you what you need. Think of the project of of the vector AC mapped onto the vector AB. Basically, you can dot the 2 and take that result and use it as a magnitude and reapply apply it to a normalized AB. – Michael Dorgan Feb 26 '18 at 23:54
  • Thanks Killzone Kid, I already checked this but the replies seem very vague. Any code examples in C++ would really help in this situation, thanks! – DrStrange Feb 26 '18 at 23:57
  • 2
    Possible duplicate of [How to find the point on an edge which is the closest point to another point](https://stackoverflow.com/questions/5204619/how-to-find-the-point-on-an-edge-which-is-the-closest-point-to-another-point) – Cris Luengo Feb 27 '18 at 00:04
  • (comment above automatically generated). Show us how you tried to implement the algorithm as shown in that answer, and where the problem is. "Gimme the codez" is not an appreciated type of question here. – Cris Luengo Feb 27 '18 at 00:06
  • Thanks Cris Luengo! As stated however, the answer to the "duplicated" is deprecated and awfully vague. Any chance someone could explain it in the form of C++ or GLM? Thanks. – DrStrange Feb 27 '18 at 00:06
  • BTW, your diagram is not at all to scale.Your projection is not on your line segment as you are showing. You can tell this as the dot product of AB, AC is negative... – Michael Dorgan Feb 27 '18 at 00:23
  • @William the second answer in the question I linked has Python code, which is pretty self explanatory even if you don't know Python, should be easy to convert to c++. I just feel bad copying it for the answer – Killzone Kid Feb 27 '18 at 01:31

3 Answers3

1

Ref: http://glm.g-truc.net/0.9.1/api/a00244.html

You should use this

detail::tvec3<T> glm::gtx::closest_point::closestPointOnLine    (   detail::tvec3< T > const &  point,
detail::tvec3< T > const &  a,
detail::tvec3< T > const &  b 
)       
Find the point on a straight line which is the closet of a point.
heLomaN
  • 1,634
  • 2
  • 22
  • 33
  • I know this is rather late but I already made a formula using a dot and cross product system - great to know that glm supports this though, thank you! – DrStrange Mar 22 '19 at 15:55
1

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

0

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...

Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
  • That's a great source, thanks! Again, the math is very vague to me as I only know C++ and glm. I'll try to understand the theory though and put more time into the subject, thanks again! – DrStrange Feb 27 '18 at 00:29
  • If you do not understand the math, then you are going to have a very hard time verifying if your code works. – Michael Dorgan Feb 27 '18 at 01:10