Jump to content
  • Advertisement
Sign in to follow this  

Picking/Selection not quite right

This topic is 3438 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 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);
            p_shader.UseProgram();
            for (int i = 0; i < t_models.Count; i++)
            {
                float t_i = (i + 1) / 256.0f;
                p_shader.SetEffectAttributeVector("s_color", new Vector3(t_i, t_i, t_i));
                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 this post


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

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!