Jump to content
  • Advertisement
Sign in to follow this  
Sagito

Distance between two objects

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
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.

Admiral

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Sagito
Is this correct or am I doing something wrong in this process?

Does it work? [wink]

Share this post


Link to post
Share on other sites
Quote:
Original post by Sagito
Is this correct or am I doing something wrong in this process?

That sounds fine. I just misunderstood.

Admiral

Share this post


Link to post
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!