Jump to content
  • Advertisement
Sign in to follow this  

Getting pick ray

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

Thought I would share since it took me a while to figure this out. No matrix multiplications or inverse matrix calculations needed. Here is how I do it (takes screen coordinates as input):
public Ray GetPickRay(int x, int y)
	// transform screen coordinates to clip coordinates (-1, -1) - (1, 1)
	float cX = 2 * x / windowWidth - 1;
	float cY = 1 - 2 * y / windowHeight;

	// transform clip coordinates to view space coordinates
	float a = camera.Projection.ProjectionMatrix.M00;
	float b = camera.Projection.ProjectionMatrix.M11;
	Vector3 v = new Vector3(cX/a, cY/b, camera.Projection.Near);

	// rotate and normalize
	Vector3 w = camera.Rotation * v;
	return new Ray(camera.Position, w.Normalize());

This requires that you have information about distance to near clipping plane and camera rotation and position. The ray is represented by an origin and direction vector.

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!