Hey everyone,
I recently got to work on a new project (for which I’ve chosen MonoGame, obviously), and thing is - I don’t really have much experience doing low-level stuff; I’ve made games using Godot, Unity etc, where all the heavy lifting is done for you. I went ahead to get started on my camera class (‘Camera2D.cs’) and experienced some weird issues.
Here’s the code for the camera class, as well as the player class:
‘Camera2D.cs’:
public class Camera2D
{
#region Fields
public Matrix Transform;
public Matrix Offset;
public Entity Target;
public bool SmoothingEnabled;
public float SmoothingValue
{
get;
set;
}
#endregion
#region Constructors
public Camera2D()
{
}
public Camera2D(Entity target)
{
Target = target;
}
#endregion
#region Methods
public void Follow()
{
var Position = Matrix.CreateTranslation(
// Create a "look-at", wherein the focus is on the center of entity. //
-Target.Position.X - (Target.Collider.Width / 2), // X-position of the entity, minus half of its width. //
-Target.Position.X - (Target.Collider.Height / 2), // Y-position of the entity, minus half of its height. //
0
);
Offset = Matrix.CreateTranslation(
// The offset value the position should be multiplied by, to ensure that the camera always keeps the entity at the center of the screen. //
MainGame.ScreenHeight / 2,
MainGame.ScreenWidth / 2,
0
);
// Set the camera's transform to be the position value multiplied by the offset value. //
if (SmoothingEnabled == true)
{
Transform = (Position * Offset) * SmoothingValue;
}
else
{
Transform = Position * Offset;
}
}
#endregion
}
‘Player.cs’:
public class Player : Entity
{
#region Fields
public Camera2D mainCamera;
public Input playerInput;
#endregion
#region Constructors
#endregion
#region Methods
public override void Initialize()
{
base.Initialize();
MoveSpeed = 2.0f;
Rotation = 0.0f;
Scale = 1.0f;
LayerDepth = 0.0f;
mainCamera = new Camera2D();
mainCamera.Target = this;
playerInput = new Input();
playerInput.KBMoveForward = Keys.W;
playerInput.KBMoveBackward = Keys.S;
playerInput.KBMoveLeft = Keys.A;
playerInput.KBMoveRight = Keys.D;
}
public override void Load()
{
base.Load();
Texture = Globals.Content.Load<Texture2D>("Graphics/space");
}
public override void Unload()
{
base.Unload();
}
public override void Update()
{
base.Update();
var velocity = new Vector2();
if (Keyboard.GetState().IsKeyDown(playerInput.KBMoveForward))
{
velocity.Y = -MoveSpeed;
}
if (Keyboard.GetState().IsKeyDown(playerInput.KBMoveBackward))
{
velocity.Y = MoveSpeed;
}
if (Keyboard.GetState().IsKeyDown(playerInput.KBMoveLeft))
{
velocity.X = -MoveSpeed;
}
if (Keyboard.GetState().IsKeyDown(playerInput.KBMoveRight))
{
velocity.X = MoveSpeed;
}
Position += velocity;
mainCamera.Follow();
}
public override void Draw()
{
base.Draw();
}
#endregion
}
Here’s the ‘Game1.cs’ code:
public MainGame mainGame = new MainGame();
public MainMenu mainMenu = new MainMenu();
Globals.spriteBatch.Begin(SpriteSortMode.BackToFront, transformMatrix: mainGame.player.mainCamera.Transform);
And here’s the result. If you need to know anything else, do let me know. Thanks in advance.