Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 x and y property precision

In ActionScript 3 (and IIRC 2), the x and y properties on a display object are always stored as multiples of 0.05.

so something like obj.x = 66.6666 is the same as obj.x = 66.65

Most of the time, this doesn't matter. But sometimes I can end up with really slow-moving objects, eg 1 pixel per second. 1/60 (fps) = 0.017 pixels per frame. obj.x += 0.017 will never actually change the x value, since it gets rounded to the nearest 0.05.
This forces me to override the x & y properties of a DisplayObject so that they are not rounded.

I can understand rounding coordinates to the nearest integral value for rendering. With a more advanced renderer, I can even understand rounding to some fraction representable in binary (eg 0.25). But 0.05 cannot be represented exactly in binary.

So why might it be that the creators of Flash decided to round to the nearest .05? It just seems like such an arbitrary number to me.

like image 597
Ponkadoodle Avatar asked Oct 30 '10 02:10

Ponkadoodle


2 Answers

From the Wikipedia article on Twips:

Flash internally calculates anything that uses pixels with twips (or 1/20 of a pixel). Sprites, movie clips and any other object on the stage are positioned with twips. As a result, the coordinates of (for example) sprites are always multiples of 0.05 (i.e. 1/20).(i.e. 1/20).

A workaround would be to store the co-ordinates in a Number, and then assign it to the displayobject's properties.

like image 80
Gray Avatar answered Nov 15 '22 09:11

Gray


Even better, precision is different for other properties of DisplayObject... consider following code:

var tmp:Number = 1/17;
var s:Sprite = new Sprite();
s.scaleX = tmp;
s.scaleY = tmp;
s.x = tmp;
s.y = tmp;
s.rotation = tmp;
trace(tmp);
trace(s.scaleX);
trace(s.scaleY);
trace(s.x);
trace(s.y);
trace(s.rotation);

it will output

0.058823529411764705
0.0588235294117647
0.0588235294117647
0.05
0.05
0.058823529411764705

which means that rotation is stored in Number precision, x and y in twips (multiple of 0.05) and scaleX and scaleY are stored in precision that is close to Number but a bit less (maybe exactly one bit less?)

like image 20
doobd Avatar answered Nov 15 '22 08:11

doobd