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.
// 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