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.

This question already has an answer here:

I'm developing a 2D Platform game, I have a 2D camera, some backgrounds, and everything is working good. Now I want to implement parallax scrolling to give the illusion of depth in my game, but i can't imagine how to make my different layers of background to move (most correctly, be rendered by the camera) with a different speed from each other).

In brief this is my game structure:

I have 3 layers of backgrounds: the first is the road level, where the player runs; the second is the intermediate; the third are some mountains very distant (in my mind). Everyone of these layers is created with seamless textures and organized with the tilemap technique, with every texture positioned one after the other. Of course I make the spritebatch draw only the visible ones.

The camera is a rectangle that follows player movements and goes just horizzontaly.

Since layers aren't moving, but only the camera is moving, I don't know how to make a background scroll with a different speed from another!

share|improve this question
1  
Take a look at jgallant.com/blog/how-to-create-a-parallax-effect-in-monogame It says Monogame, but the XNA code is exactly the same. –  Jon Sep 11 '13 at 9:53
add comment

marked as duplicate by Anko, Josh Petrie Feb 14 at 17:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 5 down vote accepted

You just move the backgrounds despite them being meant to be static.

  • For example, your character (and the camera) move 5 units to the right.
  • Move the background right behind the character to the right by 1 unit.
  • Move the background behind the other background by 2 units.
  • Move the next background by 3 units, etc.
  • The furthest away background (i.e. the background supposed to be static) is moved with the player/camera (here: 5 units).

Doing this will already create the parallax effect you're looking for.

Simplified:

Move your backgrounds with your camera, where the actual movement is a function based on the camera's movement and the distance to/behind the screen. The further away something is, the more you move it (so it appears static).

This can get a bit tricky drawing wise, but you'll essentially just have to draw your backgrounds twice at most (so it always fills the screen, no matter the offsets).

share|improve this answer
    
Actually you'll want the closest things to move faster than the furthest things. IE your furthest away background should only move 1 unit while your layer right behind the character may move 4. –  UnderscoreZero Sep 11 '13 at 15:57
    
Correct, but if you move the camera, you'll have to move the furthest away back together with the camera (i.e. the fastest). Essentially it appears reversed, but that's correct unless your camera is static (then you'd be right). –  Mario Sep 12 '13 at 9:37
    
Ah, that makes sense. I've always used a static camera. –  UnderscoreZero Sep 12 '13 at 15:29
add comment

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