Sign in to follow this  
sickbattery

[SlimDX] Picking objects

Recommended Posts

Hi there, I'm trying to select objects with this function:
        private void TrySelectPrimitive(ref Project project, float x, float y)
        {
            Viewport viewport = new Viewport(0, 0, _camera.Width, _camera.Height, _camera.Znear, _camera.Zfar);

            Vector3 nearClick = new Vector3(x, y, _camera.Znear);
            Vector3 farClick = new Vector3(x, y, _camera.Zfar);

            Vector3 near = Vector3.Zero;
            Vector3 far = Vector3.Zero;
            Vector3 direction = Vector3.Zero;

            bool intersects = false;

            float lastDistance = float.MaxValue;
            float distance = 0f;

            int selectedPrimitive = -1;

            for (int i = 0; i < project.CollisionPrimitiveInfos.Count; i++)
            {
                if (!project.CollisionPrimitiveInfos[i].Hidden)
                {
                    Console.WriteLine("Primitive {0}: {1}", i, project.CollisionPrimitiveInfos[i].Position);

                    switch (project.CollisionPrimitiveInfos[i].Primitive.Type)
                    {
                        case (int)JLG.PrimitiveType.Box:
                            JLG.Box box = (JLG.Box)project.CollisionPrimitiveInfos[i].Primitive;

                            near = Vector3.Unproject(nearClick, viewport, _camera.Projection, _camera.View, Matrix.Scaling(box.SideLengths) * box.TransformMatrix);
                            far = Vector3.Unproject(farClick, viewport, _camera.Projection, _camera.View, Matrix.Scaling(box.SideLengths) * box.TransformMatrix);

                            direction = far - near;
                            direction.Normalize();

                            intersects = _box.Intersects(new Ray(near, direction), out distance);
                            break;

                        case (int)JLG.PrimitiveType.Sphere:
                            JLG.Sphere sphere = (JLG.Sphere)project.CollisionPrimitiveInfos[i].Primitive;

                            near = Vector3.Unproject(nearClick, viewport, _camera.Projection, _camera.View, Matrix.Scaling(sphere.Radius, sphere.Radius, sphere.Radius) * sphere.TransformMatrix);
                            far = Vector3.Unproject(farClick, viewport, _camera.Projection, _camera.View, Matrix.Scaling(sphere.Radius, sphere.Radius, sphere.Radius) * sphere.TransformMatrix);

                            direction = far - near;
                            direction.Normalize();

                            intersects = _sphere.Intersects(new Ray(near, direction), out distance);
                            break;
                    }

                    Console.WriteLine("Near {0}", near);
                    Console.WriteLine("Far {0}", far);
                    Console.WriteLine("Direction {0}", direction);

                    if (intersects)
                    {
                        Console.WriteLine("Intersects at distance {0} (last: {1}).", distance, lastDistance);

                        if (distance < lastDistance)
                        {
                            lastDistance = distance;
                            selectedPrimitive = i;

                            Console.WriteLine("Selected primitive {0}.", i);
                        }
                    }

                    Console.WriteLine("");
                }
            }

            if (selectedPrimitive != -1f && project.SelectedCollisionPrimitiveInfo != selectedPrimitive)
            {
                project.SelectedCollisionPrimitiveInfo = selectedPrimitive;
                UpdateProjectTree(ref project);
            }
        }

.. but somehow I end up selecting an object through an other. Do you see the error? It's mostly when the first object is at position 0,0,0 and the other is at 2,0,0 or 0,-2,0 ... If you want you can have all source code and test it yourself. I'm creating an CollisionSkin Editor for JigLibSDX (my SlimDX specialized physics engine (JigLibX)). http://jiglibsdx.codeplex.com/ (Sorry, I'm in a hurry ... maybe this is enough info to help) Best Regards, sick.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this