Distance between two objects

Started by
6 comments, last by Sagito 17 years, 2 months ago
Hi all... In order to correctly perform picking, I need to know which of the objects of my scene are closer to the camera position. But, as there might be rotations I can't use the original position vector as it was transformed. So, the only thing left are position matrices (in my case D3DXMATRIX, Im working with DX 9.0c). Is there any way (mathematically or with DirectX) I can find out the distance between an object and the camera using only their position matrices? Thanks...
http://sagito.wordpress.com
Advertisement
Multiplying a transform matrix by a position vector will give you the composite transformation - i.e. where the position vector is now, based on the transform matrix. Pseudocodestyle:
Vector vecNewPosition = matTransformMatrix * vecOldPosition;
Then, perform the usual distance test on vecNewPosition.

That should do it. [smile]
-jouley

[Edit: Ver-ified.]

[Edited by - jouley on January 27, 2007 12:08:49 PM]
Wow, couldn't imagine it was that easy...

Thanks again... ;)
http://sagito.wordpress.com
I'm not sure if you're already aware of this, but your post makes it a little unclear.
You should unproject (pick) the geometry with the same transformations applied as when it was rendered, so that all objects are in world space. So not only should you calculate vecNewPosition for the purposes of depth-testing, but also for picking.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

Hmm, I don't know if I totally understood what you meant. What I'm doing is trying to pick a cube between several of them. So, in order to perform picking, what I do is to create a local copy of a cube (sending it to a picking routine), using the same rotation, translation and scaling matrices I used to place that object on the scene to get the picking right. But, to check if I picked the right cube, before I pick anything, I calculate the distance of each cube to the camera, put them into an ordered vector and render them in that order. This way I can guarantee I'm picking the right cube (nearest to the camera).

Is this correct or am I doing something wrong in this process? =
Thanks once again... ;)
http://sagito.wordpress.com
Quote:Original post by Sagito
Is this correct or am I doing something wrong in this process?

Does it work? [wink]
Quote:Original post by Sagito
Is this correct or am I doing something wrong in this process?

That sounds fine. I just misunderstood.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Seems to work, although the picking is somehow strange... Sometimes I compile the whole project and it works, then I recompile it without change a line of code and it doesn't work as it should... Strange behaviour... =P

Thank you both...
http://sagito.wordpress.com

This topic is closed to new replies.

Advertisement