|
Lets start with explaining a Parallax before moving into how its created.
Parallax is a displacement or difference in the apparent position of an object viewed along two different lines of sight, and is measured by the angle or semi-angle of inclination between those two lines. – Wikipedia
As you can see in the picture the object overlap each other depending on what the cameras position in relation to the objects.
And now here is an overview on how my layers are. (Sorry for the blinking image, it does not look this way in the game itself)

So how is it that this kind of optical illusion can be created in a flat 2D game. The way that I create this effect is by sorting out the Game Level into different layers, at this moment I am using a grand total of 5 layers.
LOWESTBG
This layer is a fixed background, being the same size as the gamescreen itself, and will always move with the player.
SECONDLOWESTBG
This is where the Parallax starts to take effect this, starting out this layer also moves with the player, but not entirely. It has a formula that uses the player position in order to get this layer to move around 20 % of the player position on the X axis, while on the Y axis it is slowed down even further to 10% of the position.
HIGHESTBG
After already having one layer of parallax, adding this second parallax layer gave it a even more deeper sense of the background actually. For the parallax illusion to work this layer needs to move in yet another diffrent speed. This layer is ontop of the previous layer, so it should be one step closer to the player, so its only fair that this layer moves fast than the previous layer. this being set to 30% on X and 15% on the Y axis.
MIDDLEGROUND
On this layer most of the game objects are located, such as the player, enemies, and walls. player movement is on this layer, so speed here is always 100%
FOREGROUND
This layer does not yet actually do anything, but the idea is when something shall apear on the screen in from of the player. as its then closer to the camera, it should move faster than the player itself, so the speed would be above 100% on both X and Y axis.
So quick recap, In order to create a parallax in a 2D environment, you use multiple layers moving in different speeds.
When the parallax is in place in the game, and set to correct speeds in relation to the player, it will give a good depth in the game even though it is still just a flat 2D game it will give a completely different feel to it. Here is an example of the result of this artifact.


|