Hi everyone,
This is my first attempt at writing a game with monogame. I’m using spritebatch to draw a bunch of sprites on a XY plane and I’m passing a basic effect with my camera matrices where:
world = Matrix.CreateScale(1, -1, 1);
view = Matrix.CreateLookAt(new Vector3(0, 0, 1.5f), new Vector3(0, 0, 0), Vector3.UnitY);
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(90), (float)_windowWidth / (float)_windowHeight, 0.1f, 100f);
I’m using Unproject to get the World space coordinates of my cursor to add and drag the drawn sprites on the plane, and this doesn’t give me the coordinates that I want. I’m using a workaround which I’m sure is not a good solution, by multiplying the unprojected mouseposition by the camera position Z coordinate * 20. Otherwise I can barely move the points and they are added near Vector.Zero, which is understandable since the values are very small without the multiplication.
Vector3 previousClientToWorldMousePosition = Game.GraphicsDevice.Viewport.Unproject(new Vector3(previousMouseState.X,
previousMouseState.Y, 0f),
ProjectionMatrix,
ViewMatrix,
WorldMatrix);
Vector3 curentClientToWorldMousePosition = Game.GraphicsDevice.Viewport.Unproject(new Vector3(currentMouseState.X,
currentMouseState.Y, 0f),
ProjectionMatrix,
ViewMatrix,
WorldMatrix);
deltaXmouseWorldPosition = previousClientToWorldMousePosition.X - curentClientToWorldMousePosition.X;
deltaYmouseWorldPosition = previousClientToWorldMousePosition.Y - curentClientToWorldMousePosition.Y;
//This is the workaround
viewMatrixZoomRelatedTranslation = -ViewMatrix.Translation.Z * 20;
if (currentMouseState.LeftButton == ButtonState.Pressed && intersectPointIndex != null)
{
var pointIndex = intersectPointIndex.Value;
path.MovePoint(pointIndex, new Vector3(path[pointIndex].X - deltaXmouseWorldPosition * viewMatrixZoomRelatedTranslation, path[pointIndex].Y - deltaYmouseWorldPosition * viewMatrixZoomRelatedTranslation, path[pointIndex].Z));
}
if (currentMouseState.RightButton == ButtonState.Pressed && previousMouseState.RightButton == ButtonState.Released && intersectPointIndex == null)
{
path.AddSegment(new Vector3(curentClientToWorldMousePosition.X * viewMatrixZoomRelatedTranslation, curentClientToWorldMousePosition.Y * viewMatrixZoomRelatedTranslation, 0f));
}
I’m assuming I should add some kind of scale or transformation Matrix for the matrices passed to the Unproject function, but I have no idea how it should be done. Any help will be appreciated.
Also the reason I’m using basiceffect lookat camera it to later apply an orbit camera and rotate around drawn sprites if anyone is curious