Camera, TiledMap, and Player position

Hi guys,

Quick question. I have a TiledMap along wtih a camera (for the matrix) and a player object (with a Sprite attached). In my system I have the camera move to the middle of the map (for now, later I’ll figure out a better starting position) . My update method handles moving the camera around (based on WASD keys) and then positions the player to where the camera is. Then my draw method will draw the map then the player (using the camera matrix).

All this is good except the camera is looking at the top left corner of the screen as it’s origin point so the player is up there too. What I want is the camera to handle the map but the player to be in the middle of the screen.

Just wondering what the best option here is?

  • Have the camera LookAt a new position (after moving the player) that’s half the width and height of the screen
  • Position the player so they’re half the width and height of the screen away from the top left corner of the screen

I don’t necessarily want to fix the player to the middle of the screen (using the viewport adapter) so I can get the tile under the player and that kind of stuff, so if I just apply the camera matrix to the player it all lines up with the map.

Let me know what you think (and I can provide a better explanation, code, images, etc. if you’re not following this).


Looking at your query, it’s more aimed at the MonoGame.Extended project. So I’d recommend logging your issue there.

MG.Extended is a supporting framework not managed by the MonoGame project it’self.

I’d recommend looking through the MG.Extended samples to see if any of them implement what you are looking for already.

Not sure what you’re talking about, this is the MGE forum and I am talking about MGE stuff (Camera2D, TiledMap, etc.)

My Bad, missed that we created a MG.Extended specific category on the MonoGame forums :smiley:

Just translate the camera in such a way that player is always centred (or near centred) to the middle of the screen. You may also want to clamp the camera position with minimum and maximum values so when the player moves to the edge of the map, the camera doesn’t show the empty void of whatever clear colour you used.

1 Like

What I normally do is start by having the camera LookAt the player position. This will keep the player centered in the middle of the screen and make the camera follow the player around.

Of course, as you get more sophisticated, you’ll probably want to do something more interesting. Camera systems can range from being super simple to quite complicated. I watched a really good talk on the topic recently.

Maybe it would be cool to have some of this stuff in MonoGame.Extended but for now, we’ve just got the basic camera and you’d have to implement the rest yourself.

1 Like

In a top-down 2D game (not sure how it works with a side scroller) that would be good then to look at the player but then how do you get the player into the middle the of the screen in the first place? Thanks for the video, will watch it (and get some ideas on what we might be able to add to the Camera2D class).

Try not to think of it as moving the player to the middle of the sceen, but rather, focusing the middle of the camera on the player. That’s what the LookAt method does. It centers the middle of the camera on a position.

So in your Update method you’d have something like this:


And in the Draw method you’d have something like this:

_spriteBatch.Begin(transformMatrix: _camera.GetViewMatrix());

I mean, there’s a bunch of different ways to do it but that’s the basics to get started.