Jump to content
  • Advertisement
Sign in to follow this  

Getting pick ray

This topic is 4709 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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!