Jump to content
  • Advertisement
Sign in to follow this  

Finding the object a user clicked on

This topic is 2305 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

Hello all,

I am trying to devise an algorithm to which object in my 3D world the user clicked. If two targets are close to eachother, the one closer to the near plane will be chosen. Here is my thought process for how to build this algorithm, and I just want to make sure I'm on the right track before I get too deep into this

My near distance on my view frustum is 1.0

1) Determine the direction vector between the origin and the spot clicked on the screen. This is done by calculating the following values:
[source lang="cpp"]float rayX = (position.x / halfWidth) - 1;
float rayY = -(position.y / halfHeight) - 1;
float rayZ = 1.0f;[/source]

This produces a ray with values -1 <= x <= 1 and -1 <= y <= 1. The z-coordinate is equal to the distance to the view frustum near plane.

2) Transform the near plane by the inverse transpose of the (modelview * projection) matrix

3) Calculate the intersection point between this ray and the transformed near plane.

4) From this intersection point create a ray with the intersection point as the start point of the ray, and the vector (0, 0, 1) as the direction vector.

5) For each object that is clickable, test if the ray intersects the bounding volume of the mesh

5a) if more than 1 match is found, the intersection point with the closest z coordinate to the near plane is chosen.

Am I totally off here? Am I close but need some tweaking? Am I greatly overcomplicating this problem? Any help would be great.

Thanks in advance.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!