Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
The only correct answer is 'both'. –  Blecki Oct 24 '11 at 22:55
add comment

1 Answer 1

up vote 6 down vote accepted

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.)

share|improve this answer
    
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
1  
@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. –  Nick Wiggill 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
1  
@Blecki Now you're contradicting yourself. "What if you want to do special effects like camera shake?" –  Nick Wiggill Oct 25 '11 at 12:29
1  
@Blecki. Correct. Glad we're in agreement. –  Nick Wiggill Oct 25 '11 at 12:33
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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