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.

Suppose you have your animated character controlled by the player and a 2D world (like the old 2D side-scrolling games). When the user presses right on the keyboard, the background is moved to the right. If the path is always horizontal, this is simple to do (incrementation/decrementation of the x-coordinate).

But suppose that the path is instead a polygonal chain. My questions are:

  1. How do you move the background?
  2. How do you move the background if the game objects are managed with a physics engine like box2D?

Note.

What I wanted to ask is, how do you move a 2D character in a side-scroller? For example, consider the following path:

A---------B     C'--------D'
           \
            \C__________D

You start from A, move horizontally to B, then move diagonally to C, then move horizontally to D OR, from B, jump on C' and continue till D' and so on.

share|improve this question
    
What is a "polygonal chain"? –  Nicol Bolas Nov 28 '11 at 19:01
    
You seem to have changed the question halfway through. Are you asking about the background or about moving a character? –  Anko Nov 28 '11 at 21:23

1 Answer 1

You're thinking about this the wrong way. You're thinking in terms of characters, background, etc. You need to be thinking in terms of a world, which can be viewed from any location. Or at least, a world that has a bounded area from which it can be viewed.

You should be able to position your camera in any arbitrary location you wish, within restrictions (like world boundaries). Your camera is what determines the offset for the "background".

For example, let's say your world starts at (0, 0). And it advanced to the right by 400 units, and up by 200 units. That's the boundary of your world. Now let's say that your camera can show a 20x15 rectangle of this world.

All entities have a position in the world. For example, maybe the player starts out at location (10, 5) (remember; positive X goes right, and positive Y goes up in this example). That's the player's world-space location.

When you render the player, you don't render them in world-space. You transform the player's position into screen-relative coordinates. To do that, you must define a camera, which represents a particular view of the world.

Let's say we want to center our camera (which is 20x15 in size) on the player's position, as much as possible within the boundaries of world space.

So the bottom-left position of the camera, given a player at (10, 5), would be the camera at (0, -2.5). That is half the camera width/height subtracted from the player's position. But since that is outside of the world, we clamp the Y value to 0. So the bottom-left of the camera is (0, 0).

Now, let's say the player moves to location (30, 20). Well, the bottom-left of the camera should be (20, 12.5). That's still within the world, so there's no need to clamp.

All of those camera positions we computed? Those are used to transform entities from world-relative coordinates to screen relative coordinates. This is done by simple subtraction.

If your camera is at (20, 12.5), and there's an entity somewhere that is at (30, 16), then the actual position of that entity on the screen is the entity's position minus the camera's position: (10, 3.5). This is the location you render him at (again, assuming bottom-left orientation. Most 2D renderers try to use top-left. The math is ultimately the same either way though).

So there needs to be a separation between "where the character actually is" and "where the character gets rendered on the screen".

share|improve this answer
    
Ok, but let's talk about world coordinates, as you say. The designer create the map above (A,B,C,D,C',D'), and put the character at A. When the game start, the user press continuously the right key. The character move till B, then follow BC then walk till D. There is this virutal 'floor' on which the character walk. How do you model your software to handle arbitary 2D side-scrollers maps? Sorry for my unclear questions, but this is my first attempt and i just want to understand the basic facts... –  lukeluke Nov 28 '11 at 21:49
    
@lukeluke: Your original question was about moving the background and keeping the character positioned on screen. Now you're asking about collision detection with the ground, which is a separate question. This is not a forum; if you have a new question, then you should press the "Ask Question" button and ask the new one. –  Nicol Bolas Nov 28 '11 at 22:12

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.