PDA

View Full Version : Calculate the distance between two Matrixes



laserwiz
05-30-2002, 12:15 AM
Hi all programmers out there,

I am currently programming a simulator, and now reach a problem:
I have in my scene a number of objects drawn with 3dS-Max. These objects are linked together in a hierarchy like this:

Object 1
Object 1,1
Object 2,1
Object 2,2
Object 2,3
Object 3,1
Object 4,1


After imported them into OpenGL I use the glDisplayList to draw each object.
All translation and rotation of each object inherits the parent orientation.
After doing some rotation and translations I have to retrieve a distance between to of my object.
When I get the current MODELVIEW Matrix of each object I do not know how to operate them to retrieve the distance between them. Could some one advice me with this basic problem?

Thanks to allÖ.

Gavin
05-30-2002, 01:38 AM
find a math/cg site that shows how to retreive scale, translation, rotation from a matrix.... it isn't too hard. Then you can cal the pos and the distance between the 2

mphanke
05-30-2002, 04:18 AM
Hi,

as far as I know it is impossible to retrieve data from a matrix when you have scale, translation and rotation multiplied into it.
It is only possible if you know all translations and rotations which are applied to it.

Martin

Nothing
05-30-2002, 04:52 AM
If you have the modelmatrix for object A and the modelmatrix for object B and you want to know the distance between them, simply multiply modelmatrix A with the midpoint of object A and multiply modelmatrix B with the midpoint of object B.

The rest is simple vector math.

laserwiz
05-30-2002, 11:27 AM
Gavin =>
I did find several sites that explain the Linear Algebra of an Rotation-Matrix. But I need a site that explains how to use the MODELVIEW Matrix retrieved from glGetFoatv(GL_MODELVIEW_MATRIX, AMatrix)
I know by now that this AMatrix has:
1 2 3 4
1[RX][00][00][TX]
2[00][RY][00][TY]
3[00][00][RZ][TZ]
4[00][00][00][00]
But I do not know how to do calculations on it.

Nothing =>
I have tried the equation you mention but it seams to be something wrong...

Just be patient with me and read this:
Let object A be the mother of object B, and B is the mother of object C.
This is like an human arm were A Has its rotation pivot in the shoulder and object B has its rotation pivot at the elbow and C is the hand.
I hope you are still following me. Ok...

Now if you rotate your shoulder so that your arm point 90deg straight out in front of you. Then rotate the elbow 90deg so your hand points upwards.
After this you will calculate the distance from your shoulder to your hand.

The OpenGL engine know exactly where the hand is, because if I do a glTranslatef with the original coordinates to the hand, and then rotate it, it will the be rotated around itís pivot point. Sow OpenGL has to use the current ModelView-Matrix to find the exact point of the hand.

Here is your equation:
Shoulder.TmpVector = Shoulder.ModelViewMatrix * Sholder.OriginalMatrix
Hand.TmpVector = Hand.ModelViewMatrix * Hand.OriginalMatrix

The result of this equation:
Shoulder.TmpVector=[0.0025,0.003926116,0.001963058,-97216.97]
Arm.TmpVector=[0.0025,0.002519523,0.001259762,13990.34]

I canít se the use of these vectors. But I Will now try to only multiply the rotation section of the matrixes with the translation section and not all of the 4*4 rows. In the meantime maybe you or any one else have some more info on this subject.
If so please let me know.

Tanks to all answers...

laserwiz
05-30-2002, 11:29 AM
Gavin =>
I did find several sites that explain the Linear Algebra of an Rotation-Matrix. But I need a site that explains how to use the MODELVIEW Matrix retrieved from glGetFoatv(GL_MODELVIEW_MATRIX, AMatrix)
I know by now that this AMatrix has:
1 2 3 4
1[RX][00][00][TX]
2[00][RY][00][TY]
3[00][00][RZ][TZ]
4[00][00][00][00]
But I do not know how to do calculations on it.

Nothing =>
I have tried the equation you mention but it seams to be something wrong.
Just be patient with me and read this:
Let object A be the mother of object B, and B is the mother of object C.
This is like an human arm were A Has its rotation pivot in the shoulder and object B has its rotation pivot at the elbow and C is the hand.
I hope you are still following me. Ok...

Now if you rotate your shoulder so that your arm point 90deg straight out in front of you. Then rotate the elbow 90deg so your hand points upwards.
After this you will calculate the distance from your shoulder to your hand.

The OpenGL engine know exactly where the hand is, because if I do a glTranslatef with the original coordinates to the hand, and then rotate it, it will the be rotated around itís pivot point. Sow OpenGL has to use the current ModelView-Matrix to find the exact point of the hand.

Here is your equation:
Shoulder.TmpVector = Shoulder.ModelViewMatrix * Sholder.OriginalMatrix
Hand.TmpVector = Hand.ModelViewMatrix * Hand.OriginalMatrix

The result of this equation:
Shoulder.TmpVector=[0.0025,0.003926116,0.001963058,-97216.97]
Arm.TmpVector=[0.0025,0.002519523,0.001259762,13990.34]

I canít se the use of these vectors. But I Will now try to only multiply the rotation section of the matrixes with the translation section and not all of the 4*4 rows. In the meantime maybe you or any one else have some more info on this subject.
If so please let me know.

Tanks to all answers...

Deiussum
05-31-2002, 04:19 AM
I don't think you need to pull out different parts of the matrix in order to do what you want. Just do the calculation the way OpenGL does.

transFormedVert = currentMatrix * unTransformedVert.

Maybe I'm missing something, but it sounds to me like you just want to know what the location of the vertice is after all the transformations are performed.

Nothing
05-31-2002, 06:17 AM
Originally posted by laserwiz:

Here is your equation:
Shoulder.TmpVector = Shoulder.ModelViewMatrix * Sholder.OriginalMatrix
Hand.TmpVector = Hand.ModelViewMatrix * Hand.OriginalMatrix


No I did not say modelview matrix * original matrix. I did say modelview matrix * untransformed midpoint of the object.

The midpoint is just a vertex that is located in the center of the untransformed object.

The result is a new vector that represents the transformed midpoint of the object.

If you do that for two differend objects you just calculate the distance between the two resulting vectors and you know the distance between the two objects...

If you need that for some sort of collision detection you can do that with the bounding boxes of the objects as well.

Jambolo
05-31-2002, 05:18 PM
Originally posted by laserwiz:
When I get the current MODELVIEW Matrix of each object I do not know how to operate them to retrieve the distance between them.

It is very simple -- just compute the distance between the models' origins in world space. The positions of the origins are the same as the translation vectors in the modelview matrices (unless you are doing something weird).

Two modelview matrices A and B:

TA = ( A[12], A[13], A[14] )
TB = ( B[12], B[13], B[14] )

Distance = sqrt( ( TAx - TBx ) ** 2 + ( TAy - TBy ) ** 2 + ( TAz - TBz ) ** 2 )