## News:

LWJGL 3.3.2 released

## Getting distance between two objects after transformation

Started by adamtg48, January 26, 2017, 16:07:05

I am a newbie, so if this has been answered already, i probably did not use the correct search terms.  If i apply different transforms to two different objects, and i want to know the distance between the two objects, is the best way is to get the final transform matrix for each object and apply it manually to get the new co-ordinates, and then do the calculations?  Or is there a better way to do it?

#### Kai

This depends on how exactly you define "the distance between two objects."
If you merely mean the Euclidean distance between two respective points on the objects (possibly the local origin of each object in model-space), then it's simple: Usually you compose the model matrix of a translation, rotation and some scaling. In your case you only need the translation vector, which defines the transformation from model-space into world-space. So you would just subtract both translation vectors and compute the norm of this vector to get the distance between the two object's origins in world-space.
If you mean "the distance between the two closest points on the surface of both objects" then it's a lot more difficult as then you indeed have to apply the model transformation matrices to each object (all of their vertices) and use some method to find the closest points on the surfaces.

Kai,

Thanks for your response.  The "distance between two points" is actually going to be to specific vertices.  As for the rest of your answer, well, at least i know now what i need to research and learn before i can do this properly.  thank you very much for your help.

I think this is a correct way to do it.  at least it looks correct on the screen...    I am using the JOML library for the matrix math

Instead of calculating the distance, i just drew a line between the two points:

First i have a method to get the current matrix:

then after i do the transformations, i save the matrix into "m1" and "m2"

and then i manually calculate the new points

i am sure once i re-learn all my linear algebra that i have not looked at for 15 years, there will be a more efficient way of doing this, but this does work for now.

#### Kai

Quote from: adamtg48 on January 26, 2017, 20:57:37
First i have a method to get the current matrix:

Glad it works, but why do you need the above? You can completely eliminate the OpenGL matrix stack methods (except for glLoadMatrixf) by using the equally named (minus the gl/glu prefix) methods in Matrix4f and even MatrixStackf.
See the JOML Wiki: https://github.com/JOML-CI/JOML/wiki/From-glRotate-to-JOML
Also see the last paragraph of this section in the README.md: https://github.com/JOML-CI/JOML#using-with-lwjgl