I’m making a strategy with top-down 2D graphics. I implemented the way to move the view by holding LMB and then “dragging” the screen. It works fine with the exception of the movement being a little too fast, and I have no idea how to calculate the movement properly.
The code for camera movement (and scale) is
class CameraControls : WGComponent
{
public override void Update(GameTime gameTime)
{
if (WGame.userInput.mouse.LeftButton == ButtonState.Pressed) WGame.camera.position -= (WGame.userInput.mouse.Position - WGame.userInput.prevMouse.Position).ToVector2().InvertY() * WGame.camera.scale;
WGame.camera.scale = Math.Max(WGame.camera.scale - WGame.userInput.frameScroll * 0.1f, 1f);
}
}
WGame
is my Game
class, which holds objects I expect to have only one instance as static variables. WGComponent
just overrides the constructor to use the WGame
instance stored in the static variable.
WGame.userInput.mouse
is the MouseState
for the current frame, while WGame.userInput.prevMouse
is for the previous frame. WGame.userInput.frameScroll
is calculated with frameScroll = mouse.ScrollWheelValue - prevMouse.ScrollWheelValue
The camera code is
public class Camera
{
public Vector2 position = new();
public float scale = 1;
}
And its position is used in WGame’s Update method
protected override void Update(GameTime gameTime)
{
translationMatrix = Matrix.CreateTranslation(-camera.position.X, camera.position.Y, 0);
scaleMatrix = Matrix.CreateScale(100 / (camera.scale * camera.scale));
screenSizeMatrix = Matrix.CreateTranslation(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2, 0);
resultMatrix = translationMatrix * scaleMatrix * screenSizeMatrix; //This one is later used in SpriteBatch.Begin()
base.Update(gameTime);
}
So, am I doing everything right and if I do, is there a way to make the screen be dragged exactly as much as the mouse is moved?
Manender is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.