lundi 28 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.

Aucun commentaire:

Enregistrer un commentaire