For my game, a 2D platformer, I decided to add parallaxing backgrounds to create more depth. For this approach, I created an object for each pane in the background and foreground, having the following traits:
- Movement factor (scale): The proportion to the Camera's position that it moves at (ie: 3/4 of the Camera's speed)
- Position (x,y): Pretty obvious. For example, x = scale * Camera.x;
- Layer Image (img): The image used for the plane object.
As far as rendering the image at the correct location, I have that taken care of. What I struggle with is how to determine the exact image size needed to line up with the room correctly. By that, I mean: given a room's dimensions, a camera's dimensions, and a movement factor, how can I calculate the dimensions of the image, so that when the Camera moves to the edge of a room, all the layers line up?
Currently, the best formula I could come up with for the image size is (|scale| * room_dimension) - (scale * camera_dimension), but it only works on a few cases (mostly with foreground layers)
As a side note, the scale variable is a float data type with the domain of (-Infinity,1]. Any layer with a value from 1.0 to 0.0 inclusive is considered a background layer. Any layer with a negative scale value is considered foreground.
Any help is greatly appreciated