Problem with parallax scrolling

Aug 6, 2012 at 11:48 PM
Edited Aug 7, 2012 at 6:13 AM

I am trying to build a platformer game using xtile + farseer physics, but when using layers (with different sizes for parallax), i can't sync xna's camera with xtile's viewport. I made a video showing this:

http://www.youtube.com/watch?v=EGj0eKUHTgE

Can anyone help?

Thanks in advance.

EDIT:

  if (state.IsKeyDown(Keys.Right) & !state.IsKeyDown(Keys.Left))
            {
                if (_cameraPosition.X*-1+_graphics.PreferredBackBufferWidth+2 <=  _map.DisplayWidth)
                {
                    _cameraPosition.X -= 2;
                    _viewport.X += 2;
                }
            }

            if (state.IsKeyDown(Keys.Left) & !state.IsKeyDown(Keys.Right))
            {
                if (_cameraPosition.X*-1 >= 2)
                {
                    _cameraPosition.X += 2;
                    _viewport.X -= 2;
                }
            }

//This is the part of my code that handles the "cameras". If using just one layer, it works flawlessly,
//but with more than one layer (with different sizes), the problem shown in the video above happens.

 

Coordinator
Aug 7, 2012 at 9:45 AM

The bottom layer in your map appears to be the widest. The width of a map is determined by the widest layer (similarly for height) and hence the viewport will synch with this instead of your topmost layer. For a proper parallax effect, the layers should be ordered from topmost to bottommost in decreasing order of size. If you wish to synch with a layer other than the largest, I'm afraid you will need to scale the viewport's position accordingly by using Layer.ConvertMapToLayerLocation(...), for instance.

On a related note, this has got me thinking about the possibility of letting the developer choose the viewport's affinity to a layer other than the largest. To preserve back compatibility, this affinity would by default be set to the largest layer. This feature would be ideal when the designer wants the player to walk on a middle layer whilst still having objects pass by in the foreground, and without having to scale the viewport position as I suggested above. The only possible back compatibility issue is that currently the viewport synchs with the larger width and larger height of all the layers - which may not necessarily belong to the same layer. I cannot think of any reason why a level designer would choose such a map configuration other than to create some weird parallax effect that behaves differently in the horizontal and vertical directions.

Aug 7, 2012 at 2:13 PM

Thanks a lot for your response. That did manage to solve my problem. =D