Ben Morris
Ben Morris

Reputation: 81

2D Game Background Parallax Multiple Layers

I am having trouble wrapping my head around the following scenario for a parallax background system in a 2D sidescrolling game.

parallax scroll problem

Layer 1 is visible when the game starts and scrolls along with the camera at a factor of 0.5. Eventually, layer 1 will come to an end and layer 2 needs to be visible so there is no horizontal gap between the two layers. If both layers were moving at the same speed it would be simple, the start of layer 2 could be placed at the end of layer 1. However, in this scenario layer 2 moves slower than layer 1 and so will have to be positioned earlier on the X axis to prevent a gap between the two positions. All background layers are moving relative to the camera.

The system I am trying to create needs to be flexible and support different scroll factors and constantly alternating between layers. Can anyone provide a solution to the problem?

Upvotes: 0

Views: 926

Answers (1)

LouD
LouD

Reputation: 3844

If both layers were moving at the same time it would be simple

Do you mean "if both layers were moving at the same SPEED it would be simple"? Based on your drawing, here's the formula and some sample numbers:

w1 = width of layer 1 = ? (lets say 1000)
s1 = speed of scrolling for layer 1 = 0.5
s2 = speed of scrolling for layer 2 = 0.25
x2 = start x position of layer 2

x2 = (1 - (s2/s1)) * w1

500 = (1 - (0.25/0.5)) * 1000

If it moved 5 times slower

800 = (1 - (0.1/0.5)) * 1000 

Upvotes: 1

Related Questions