mardi 29 septembre 2015

Accurate scale formula for endless road

I'm making an endless straight road from top to bottom and using a single bitmapData for it that I use in multiple tiles. Those tiles are arranged correctly from top to bottom (let's say top to bottom of screen) by using a formula that calculates what their scale values should be. The scale value of the first tile is calculated according to its position then all other tile are calculated according to their previous tile. This works great except that when everything loops I can see the bottom tile (the first one) having its width getting slightly larger than it should (around 5 pixels too large). When that first tile goes out of screen it's sent back to the top of screen and the next tile takes the role of top tile, etc ... But because of the slight width enlargement the road has a slight jump in size when the transition occurs. So that means my formula is close but slightly wrong in calculating the scale of the first tile. What is wrong with the formula and what should be corrected?

var speedFactor:Number = speed / _length;//size of screen top to bottom 
var bitmap:RoadTile = tiles[0]; 
bitmap.y += speedFactor * bitmap.y; //scaled speed      
var distance:Number = _length - bitmap.y;//calculate position and determine scale           
if(distance < 0)
{
    distance = _length + Math.abs(distance);
}
else
{
    distance = bitmap.y;
}           
var scale:Number = (distance / _length);    
bitmap.scaleX = bitmap.scaleY = scale;  
//scale is calculated but is slightly wrong.

You can see the result here: http://ift.tt/1iYKP8v

Notice the bottom of the screen where the width of the road slightly changes and then jumps back to normal when the top tile is removed and sent back to the beginning. I now got a better result by applying a mask but that's really just a hack. I also got a better result by adjusting the scale of the top tile according to the width it needs to be at the bottom of the screen. Still not perfect though.

I think I know what I'm missing and I'm gonna work on that: adding in the formula the angle of the graphic. Here the tile side have a angle of 28 (the engine works with tile of different side angle). Basically the angle needs to be added to the formula so that when the top tile moves down, its new scale is calculated according to what the tile width needs to be at the bottom of the screen (that width needs to always be the same so no jump occur in the road display)

Aucun commentaire:

Enregistrer un commentaire