# Picking/Selection not quite right

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

## Recommended Posts

Hi everyone, I was annoyed with my previous picking solution. So I started to write a new one today, and have it *mostly* working.
        public StaticModel Pick()
{
double x, y, z;
int t_x = InputStates.Instance.Mouse.X;
int t_y = CameraHandler.Instance.ViewMatrix[3] - InputStates.Instance.Mouse.Y;
float[] t_z = new float[1];

Gl.glReadPixels(t_x, t_y, 1, 1, Gl.GL_DEPTH_COMPONENT, Gl.GL_FLOAT, t_z);
Glu.gluUnProject(t_x, t_y, t_z[0], CameraHandler.Instance.ModelviewMatrix, CameraHandler.Instance.ProjectionMatrix, CameraHandler.Instance.ViewMatrix, out x, out y, out z);

Ray t_ray = new Ray(new Vector3(x, y, z), CameraHandler.Instance.ActiveCamera.Position);

List<StaticModel> t_models = TreeHandler.Instance.StaticTree.FindCollisions(t_ray);
for (int i = 0; i < t_models.Count; i++)
{
float t_i = (i + 1) / 256.0f;
t_models.Draw();
}

int[] t_o = new int[1];
Gl.glReadPixels(t_x, t_y, 1, 1, Gl.GL_RED, Gl.GL_UNSIGNED_BYTE, t_o);
if (t_o[0] <= 0 || t_o[0] > t_models.Count) return null;
else return t_models[t_o[0] - 1];
}


So I'm first getting the x, y, z co-ordinates of the mouse using glReadPixels for the depth, and then gluUnProject for the world co-ordinates. Then I build a Ray between the camera and the clicking position. This is where I think I am going wrong, because 99% of the time the picking algorithm works. However, sometimes the Ray doesn't look quite right. It will be off by just a bit (almost as though perspective calculations aren't quite right). The rest of the code is just to build up a list of all the objects that the ray collides with (using quadtree and AABB). Then I use a shader to output different colors for each object to the backbuffer, and then get that color to find which object was selected. Any suggestions as to where I might be going wrong. I'm fairly certain that there is a problem in the way I am generating my Ray. Thanks, Giawa

##### Share on other sites
Solved: I needed to take the direction, not just the two positions I found.

[source land="csharp"]Ray t_ray = new Ray(CameraHandler.Instance.ActiveCamera.Position, (new Vector3(x, y, z) - CameraHandler.Instance.ActiveCamera.Position).Normalize());

[Edited by - Giawa on December 20, 2008 6:33:16 PM]