Sign in to follow this  
bugboy

Parallax Problem

Recommended Posts

In my game I want to be able to easily create different parallax layers by setting their scrolling ratio and having a function calculate how big their tile array needs to be in order to meet with the main tile array. Currently my function looks like this, it works for .5, but when the ratio is changed to .25 there is a small gap. void ParaLayer::setratio(float r,int NUX,int NUY, int sw, int sh) { ratio=r; NUMX=(NUX*ratio)+(((sw/2)/32))+1; NUMY=(NUY*ratio)+(((sh/2)/32))+1; tiledata.resize(NUMX); for(int i = 0; i < NUMX; i++){ tiledata[i].resize(NUMY);} } NUX & NUY are the width and height of the main tile array the one that the player interacts with. sw & sh are the screens width and height NUMX & NUMY are the layers tile array width and height You use the function like this: layers[1].setratio(.25,NUMX,NUMY,Engine.SCREENWIDTH,Engine.SCREENHEIGHT); Makes this layer scroll 1/4 as fast as the main tiles layers[0].setratio(.5,NUMX,NUMY,Engine.SCREENWIDTH,Engine.SCREENHEIGHT); Makes this layer scroll 1/2 as fast as the main tiles This is probably hard to understand but hopefully someone will be able to help me. Thanks in advance

Share this post


Link to post
Share on other sites
You are losing accuracy as you are using ints and so if the division is not a whole number, you are getting a rounding error which explains your gap.

A simple solution would be to round up always so you have an extra tile of columns eliminating that gap.

A even cleaner solution would be to not scale up the background parallax layers at all but to implement a scrolling system which when the end tile column is reached, it wraps around to the first tile column. This will then create infinite background parallax layers.

- Oscar [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this