0
\$\begingroup\$

i made a small 3D game and i just try to apply a basic sample from here : http://rbwhitaker.wikidot.com/picking for picking model by the mouse cursor. For that i use the Ray object and check if some model intersect my Ray. But i don't know why, the model object target by my cursor is always offset to the left.there is a shift difference.

enter image description here Here is the code i use.

// Update Ray cursor each frame in the Update loop function

public void UpdateRayCursor()
        {
            Vector3 nearPoint = new Vector3(Globals.mouseState.Position.ToVector2(), 0);
            Vector3 farPoint = new Vector3(Globals.mouseState.Position.ToVector2(), 1);

            nearPoint = graphic.GraphicsDevice.Viewport.Unproject(nearPoint, CameraNode.Camera.Projection, (Matrix)CameraNode.View, Matrix.Identity);
            farPoint = graphic.GraphicsDevice.Viewport.Unproject(farPoint, CameraNode.Camera.Projection, (Matrix)CameraNode.View, Matrix.Identity);

            Vector3 direction = farPoint - nearPoint;
            direction.Normalize();

            Ray = new Microsoft.Xna.Framework.Ray(
                nearPoint
                ,
                direction
                );
        }

// check if a model is target by the Ray

public void UpdateIntersectSceneRay()
    {
        float? nearestDistance = 0f;
        bool findModelTarget = false;
        foreach (var bdgToCheck in ListBlocksSelectable)
        {
            //Check if the boundingBox intersect with the ray
            var checkIntersectDistance = _mainPlayer.Ray.Intersects(bdgToCheck);
            if (checkIntersectDistance != null)
            {
                if ((float)checkIntersectDistance < (float)nearestDistance || nearestDistance == 0)
                {
                    nearestDistance = checkIntersectDistance;
                    _mainPlayer.ItemFocusByReticle = item;
                    findModelTarget = true;
                }
            }
        }
        if(!findModelTarget)
        {
            _mainPlayer.ItemFocusByReticle = null;
        }

    }

Some people have some idea why ? i use monogame library with XNA. Thank you in advance

\$\endgroup\$
0
\$\begingroup\$

It's ok , i finally find the solution by myself. Is because when i try to get the mouse position. It's not exactly the center of the screen which must match the player cursor. I must use like this :

Vector2 posCursor = new Vector2(
            graphic.GraphicsDevice.Viewport.Width/2,
            graphic.GraphicsDevice.Viewport.Height/2
            );
        Vector3 nearPoint = new Vector3(posCursor, 0.0f);
        Vector3 farPoint = new Vector3(posCursor, 1.0f);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.