[RPG] How do you move all of these things?

Hey,

I’ve been thinking about this since I first managed to get player/map movement working a while back. How do you move the map, camera, npcs and the player’s character? The way that I’ve been doing it seems really crude and it puts a lot of limitations on how I do things, I’d like to change the way I’m doing it atm but I’m not sure of any other ways to do this.

The way that I currently do everything is… The player’s character is always centered on the screen, everything else is moved around the player to make it look as if the player is moving. By doing it this way I’m unable to allow the window to be resized because it’ll throw off all of the math which I use to draw everything, the player’s speed can only be slightly increased before it goes into super speed and looks terrible and I won’t be able to do decent looking cutscenes with this setup.

So, how would you go about moving the player’s character, camera, npcs, etc… in a 2D game? (Just theory, I’m not looking for any code.)

The player, all the characters and objects in the game and the camera all have a position. That position is not the absolute position on the screen. The positions are always relative to the game map. So when you move or zoom the camera the position of everything else in the game is not affected by that. The screen coordinates are calculated only in the rendering code.

Exactly what DrZoidberg said. Decouple the view from the world.

For example, treat your view as a ‘camera’, which itself has a position. In your case the camera position is updated when the player position changes, keeping the player in the centre of the screen. When it comes to rendering everything, just convert their world coordinates to ‘view’ coordinates. In a general case this will be worldCoords - cameraCoords.

It will free you from your issues with screen resizing, unless of course you wanted to implement scaling.

This will make your camera object more versatile and reusable too. For example, you could implement scrolling, or control the camera for certain animations or events. Furthermore, you no longer have to update every object in the world when your player moves; you are just updating the player and camera position.

EDIT: (And a bit off-topic). I see you are working on an RPG. As your game expands in size, you could use this camera approach with your world design to optimize things a bit. For example, with a small game there are no problems iterating through each object and converting world coordinates to screen coordinates, and drawing them if they are within the viewport. However if you had a lot of objects you may end up with a lot of unneeded calculations. You could for example break your world/map up into ‘segments’. You could then quickly calculate which segments are currently within or near the viewport, and only do the ‘world to screen’ calculations for objects that are in those segments, and hence are potentially visible.

Thanks for the replies, I was actually had a similar idea floating around in my head. I’ll give it a shot and see if it works out. ^.^ :slight_smile: