Public Group

# Distance between two objects

This topic is 4136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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]

##### Share on other sites
Wow, couldn't imagine it was that easy...

Thanks again... ;)

##### Share on other sites
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.

##### Share on other sites

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

##### Share on other sites
Quote:
 Original post by SagitoIs this correct or am I doing something wrong in this process?

Does it work? [wink]

##### Share on other sites
Quote:
 Original post by SagitoIs this correct or am I doing something wrong in this process?

That sounds fine. I just misunderstood.

##### Share on other sites
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...

• 10
• 17
• 9
• 13
• 41