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?

  • \$\begingroup\$ The only correct answer is 'both'. \$\endgroup\$ – 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.)

  • \$\begingroup\$ 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. \$\endgroup\$ – Blecki Oct 24 '11 at 22:54
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Engineer Oct 25 '11 at 0:30
  • \$\begingroup\$ You have a camera, it has a position, you create a view matrix. There is no corner case. \$\endgroup\$ – Blecki Oct 25 '11 at 2:03
  • 1
    \$\begingroup\$ @Blecki Now you're contradicting yourself. "What if you want to do special effects like camera shake?" \$\endgroup\$ – Engineer Oct 25 '11 at 12:29
  • 1
    \$\begingroup\$ @Blecki. Correct. Glad we're in agreement. \$\endgroup\$ – Engineer Oct 25 '11 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.