Getting distance between two objects after transformation

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

Previous topic - Next topic

adamtg48

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?

thank you ahead time for your help

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.

adamtg48

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.

adamtg48

I think this is a correct way to do it.  at least it looks correct on the screen...  ;D  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:
public float[] getCurrentMatrix() {		 
       float[] matrix = new float[16];
       glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
       return matrix;
}


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

and then i manually calculate the new points
    // firstPoint and secondPoint are the points in the objects i 
    // i want to draw the line
    Vector3f  v1 = new Vector3f(firstPoint[0], firstPoint[1], firstPoint[2]);
    Vector3f  v2 = new Vector3f(secondPoint[0], secondPoint[1], secondPoint[2]);
	
     // set the matrices for JOML to use     
     Matrix4f m1 = new Matrix4f();	       
     m1.set(firstMatrix);	       

     Matrix4f m2 = new Matrix4f();
     m2.set(secondMatrix);
	       
     // calculate the positions of those points
     // using the same transformations
     Vector3f newPoint1 = m1.transformPosition(v1);
     Vector3f newPoint2 = m2.transformPosition(v2);
		   
     // draw the line
     glLineWidth(10f); 
     glColor3f(1.0f, 1.0f, 0.0f);
     glBegin(GL_LINES);
	 glVertex3f(newPoint1.x, newPoint1.y, newPoint1.z);
	 glVertex3f(newPoint2.x, newPoint2.y, newPoint2.z);
     glEnd();


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:
public float[] getCurrentMatrix() {		 
       float[] matrix = new float[16];
       glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
       return 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

adamtg48

It's a very simple reason why i did it that way.  i do not know what i am doing....  ;D

Thanks for all your help, and i have lot of good stuff to start learning.