# Parallax Problem

This topic is 5407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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.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 on other sites
Are NUX and NUY evenly divisable by 2 but not 4? That would cause a rounding error when you convert to an int.

What is the +(((sw/2)/32)) term there for?

##### 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]

• 18
• 29
• 11
• 21
• 16