In a scrolling 2D game, should the objects move or should the camera move?

I'm new to game design and don't really know how things are done. I'm thinking of building an iOS game where a ship has to fly as high up as possible (at variable speeds), collecting power ups, et cetera, on the way.

What is the way to go here? Should the background and items scroll down from the top or should the ship really fly up moving the view with it?

• The only correct answer is 'both'. – Blecki Oct 24 '11 at 22:55

It's all relative. Really, you're always transforming your view relative to your fixed player position.

So, forgetting the screen for now, let's say you have these 3 steps (representing positions as they update on each game loop):

1. playerWorldPosition = 50, 100; objectWorldPosition = 60, 200;
2. playerWorldPosition = 55, 110; objectWorldPosition = 60, 160;
3. playerWorldPosition = 60, 120; objectWorldPosition = 60, 120;

(as x,y in each case)

As you can see, the player is flying up or jumping up and moving slightly to the right, while the object (could be a powerup) is falling straight down. The player catches the powerup. Now how do you draw this onscreen?

First, let's assume you want your player in the centre of the screen at all times. So the view locks to the player -- this is the simplest approach to visualise. The position of your player will then always be screenCentre (i.e. screenWidth/2, screenHeight/2). How then do you get the onscreen position of other objects in the world?

objectScreenPosition = (objectWorldPosition - playerWorldPosition) + screenCentre;


You can see that this formula works even for the actual player, since (playerWorldPosition- playerWorldPosition) + screenCentre is equal to screenCentre. So you can apply the same formula to each entity in your game, running through a list. You can use this simple approach to build on for other things (like camera lag, cases where you are in the corner of a level, etc.)

• And what if there are zero or many player objects? What if you want to do special effects like camera shake? Even if the math looks the same, it's really not the same thing. And it's not one or the other. Objects move, and the camera moves too. – Blecki Oct 24 '11 at 22:54
• @Blecki ...You say, after the answer was accepted. If you really think they're so different, you must have a hard time getting your views set up. Go write your own concise answer on view transformations that deals with all the corner cases, then. No-one's stopping you. – Engineer Oct 25 '11 at 0:30
• You have a camera, it has a position, you create a view matrix. There is no corner case. – Blecki Oct 25 '11 at 2:03
• @Blecki Now you're contradicting yourself. "What if you want to do special effects like camera shake?" – Engineer Oct 25 '11 at 12:29
• @Blecki. Correct. Glad we're in agreement. – Engineer Oct 25 '11 at 12:33