I'm currently developing a monogame game with a tile engine.
I have been experimenting with different tiles and sprites sizes : 16px, 32px, 64px I have settled for 32x32 pixels tiles rendered at a 64x64px size on screen.
I have read that a good way to do so is to use a RenderTarget2D. I've done so. It works well. I'm using SamplerState.PointClamp to keep the pixel aspect clean.
So for a 1440x900 resolution I use
renderTarget = new RenderTarget2D(graphics.GraphicsDevice, 720, 450);
My question : when using the RenderTarget2D to upscale the screen, it logically messes up the camera I had working, and even more so the conversion of mouse coordinates to world coordinates. How should I modify the camera and the "mouse coordinates" to world position function. Is it even possible to convert properly mouse coordinates when you are upscaling everything with a renderTarget ?
This answer hints that it may be problematic, can somebody explain to me why ?
From the link : so if your graphics aren't dependant on other graphical features like a mouse, then I would use a RenderTarget
I can't find any resources about this. I always find a solution by myself, but this one is too much for me. Linear algebra is not my strong suit. Thank to anybody who can help.
My transform Matrix and Worldposition conversion
public Matrix Transform(GraphicsDevice graphicsDevice)
{
m_Transform =
Matrix.CreateTranslation(new Vector3(-m_CameraPosition.X, -m_CameraPosition.Y, 0)) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) *
Matrix.CreateTranslation(new Vector3(ViewportWidth * 0.5f, ViewportHeight * 0.5f, 0));
return m_Transform;
}
public Vector2 Worldposition(GraphicsDevice graphicsDevice, Vector2 position)
{
//Invert camera
Matrix m_InvertTransform =
Matrix.CreateTranslation(new Vector3(-(m_CameraPosition.X + ViewportWidth * 0.5f), 0, -(m_CameraPosition.Y + ViewportHeight * 0.5f))) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateScale(new Vector3(Zoom, Zoom, Zoom)) *
Matrix.CreateTranslation(new Vector3(ViewportWidth * 0.5f, 0, ViewportHeight * 0.5f));
m_InvertTransform = Matrix.Invert(m_InvertTransform);
//Transform the position
Vector3 newPos3 = Vector3.Transform(new Vector3(position.X, 0, position.Y), m_InvertTransform);
return new Vector2((newPos3.X - 720) , (newPos3.Z - 450) );
}
Edit :
In my Update() method :
Vector2 vector = new Vector2((int)player.X, (int)player.Y);
camera.ChangePosition(GraphicsDevice, vector);
The ChangePosition function in my Camera Class public void ChangePosition(GraphicsDevice graphicsDevice, Vector2 newposition) { m_CameraPosition = newposition; }
mouseposition = mouseposition / Scale;
wherescale = screenwidth / screenheight;
\$\endgroup\$//camera.ChangePosition(GraphicsDevice, vector);
And change Worldposition like this : ` return new Vector2(newPos3.X - 720 , newPos3.Z - 450) / 2; ` I get the right world position with my mouse. But as soon as my camera is centered on the player, mouse coordinates are not in sync with the tile clicked anymore. \$\endgroup\$mouseposition = (mouseposition + (1500,0)) / Scale;
\$\endgroup\$