# Question About An Implementation Of Parallax Scrolling In C++/SDL/OpenGL

I been working in a project with a team for a Software Engineering class and we think that using the parallax scrolling will help our game to look really nice but we are not really sure if our idea for implementation it's the correct one, so I hope that someon will give us some guidance about our plan.

First, we have three classes, Level, Tileset, Layer, the first one has two vectors of Layers and Tilesets, so our idea is load all the data from a TMX file of the first level in a vector>, but we only draw the part of the map that it's currently in camera, so inside a cycle we draw every layer, but we're not sure how defined a velocity for each layer so the parallax scrolling works like it supposed.

Thanks for the attention and help.

Good day....

• Maybe I don't really understand the problem. But each Parallax layer is really just a float for speed and a series of drawables attached to it with an offset. So you just do if(LayerSpeed*DrawableOffset is inside screen rect){draw drawable;}? Or is it something else that you're having trouble with? Commented Sep 3, 2011 at 21:28
• My problem is how move each layer at different velocity based on the position of the main character. Thanks for the reply... Commented Sep 3, 2011 at 21:38
• Simple an object's position is the object's offset from the left * the speed of the layer * the offset of the character from the left (here left is the start of the world). Commented Sep 3, 2011 at 21:59
• but how I determine how many tiles of the map moved when the translation occur...?? Commented Sep 6, 2011 at 0:10
• Sorry I don't get that question. Maybe you should read up a little more on parallax scrolling. See virtualrealm.com.au/Blog/tabid/62/EntryId/296/… for example (not C++ but might get the message across). Commented Sep 6, 2011 at 8:44

Parallax scrolling is just using a set of layers moving with different speed. each layer can be a tilemap, just a sprite, or anything else. normally the most front layer is a tilemap and you only check collisions and other gameplay features with that layer. just keep in mind parallax scrolling is going to simulate 3d projection using only 2d objects, so objects back in the scene should move with slower speed than those in the front.

to ease your work,, you can keep the character(player) in some fixed position. then give negate of player velocity to your parallax node. I suggest for a parallax node you only change setPosition function, in that function instead of moving parallax node itself, you have to move all it's children, respecting their depth value. for example you can use some code like this one:

void move(float deltaX,float deltaY)
{
for(unsigned i=0;i<children.size();i++)
{
children[i]->move(deltaX / child.depth,deltaY / child.depth);
}
}


all the other functions of the parallax node is same as normal node. you don't need to worry about anything else.

• I didn't understand the notion of parallax node?...Can you specify a little bit?? Commented Sep 6, 2011 at 0:09
• I guess you can find enough information in wiki page Commented Sep 6, 2011 at 8:29
• @oscar : i've also added some more explanation to my answer, hope you get what you need. Commented Sep 7, 2011 at 5:30
• Thanks Gajet, that extra information will help me a lot in the future... Commented Sep 8, 2011 at 0:16

we're not sure how defined a velocity for each layer so the parallax scrolling works like it supposed.

Two floats. I'd recommend one for X and one for Y, but maybe your game happens in another coordinate system.

• Yes, I understand that I must define a velocity value in each coordinate for the movement, my question is, how is the best way to use those variables for move the layers, because I have a matrix of each layer, and I draw every layer composed of tiles, but my main problem it's the way of move the layers... Commented Sep 3, 2011 at 22:20

I've implemented it simply by having the lower background layer scroll at a certain speed, and having the upper background layer scroll at half the speed of the lower background.