Thought o’ the Day: Flash AS3′s DisplayObject Position Precision

Today I discovered two things:

#1: Flash AS3 stores a DisplayObject’s x and y position in a Number variable, which uses double-precision (64 bits). To use the x and y number under this assumption, though, is a serious (but understandable?) error. In fact, changes to these values seem only to be taken to the precision of 0.05. For example, if you set the position of your MovieClip, Sprite, etc. to x = 100.032, y = 0.051. Flash will instead set your position as x = 100, y = 0.05, even though the Number data type has room for many, many more digits. Whether this is because the actual storage values for x and y are lower precision or if Flash enforces this discretization for other reasons, got me. If you’re going to be doing any sort of physical simulation worth its salt, you’re gonna have to use your own position variables (I’m using px and py now), which aint so bad, except that…

#2: It would (and did) take me 3 hours to figure that out myself. Goodbye, Saturday afternoon :-) . Couldn’t figure out why my simple explicit gravitational sim was getting crazier with SHORTER timesteps (the answer is that positional updates were being overestimated by the precision weirdness on each iteration, and the # of iterations per frame is inversely proportional to the timestep size).

Ah well… I’m more knowledgeable for it. Thought I’d put this up here for posterity. For the record, I’m compiling using FlashDevelop 3.0.2; not sure if that affects the result.

Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.