0
\$\begingroup\$

My mouse ray is screwing up precision. I don't really know how to fix it, maybe you guys can help.

problem : (5.6mg gif) https://www.dropbox.com/s/v0z67afso88hsd1/perspective_ray.gif

how i create the mouse ray:

 private Ray GetMouseRay(GraphicsDevice gd, ref Matrix view, ref Matrix proj)
    {
        //create source positions
        // i dont really understand why the 0 and the 1, since the near/far clip plane are totaly diferent, but from experimentation, this is a must
        Vector3 nearsource = new Vector3((float)MousePosition.Value.X, (float)MousePosition.Value.Y, 0.0f);
        Vector3 farsource = new Vector3((float)MousePosition.Value.X, (float)MousePosition.Value.Y, 1.0f);
        //Console.WriteLine("nearsource :" + nearsource.ToString() + " farsource :" + farsource.ToString());
        //Matrices needed are the view proj and this world
        //we are positioning the mouse ray in the origin(model origin, its a 3Dspace ray)
        Matrix world = Matrix.CreateTranslation(0, 0, 0);

        //unproject mouseposition in the clipping planes
        Vector3 nearPoint = gd.Viewport.Unproject(nearsource, proj, view, world);
        Vector3 farPoint = gd.Viewport.Unproject(farsource, proj, view, world);
        //Console.WriteLine("nearPoint :" + nearPoint.ToString() + " farpoint :" + farPoint.ToString());
        // Create a ray from the near clip plane to the far clip plane.
        Vector3 direction = farPoint - nearPoint;
        direction.Normalize();
        return new Ray(nearPoint, direction);
    }

How i am drawing the ray: CDebugShapeRenderer.AddLine(mouseRay.Position, mouseRay.Position * mouseRay.Direction * 1000, Color.Red);

how i am calculating the Obb:

Bounding Box in Monogame for mouse picking

How i am calculating the collision: Line: 349 https://github.com/CartBlanche/MonoGame-Samples/blob/master/CollisionSample/BoundingOrientedBox.cs#L349

So how can i create a mouse ray that is accurate? or remove that perspective somehow.

Roger.

Edit: I forgot to add the matrices used

 proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, this.GraphicsDevice.Viewport.AspectRatio, 1.0f, 1000.0f);
 view = Matrix.Identity;
\$\endgroup\$
1
\$\begingroup\$

I totally forgot about this, so the problem was on how my view matrix was built.

So my view port was located just in front of the ray cast, messing everything up, i was projecting into the right place, i was just looking at it wrong.

I just needed to adjust my view matrix and done.

\$\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.