Tuesday, February 26, 2013

Object Coordinates Trick

I just recently discovered out a trick around an old problem.  I used to use to do my object coordinates in 16.16 format.  (16-bit intergers, and 16-bit decimal)  This method made it easy to do basic moving, such as walking left or right and jumping, but it made more advance movement, such as rotation or chain physics, more complicated to program.

I found if you use 16.8 format (16-bit intergers, and 8-bit decimal) you can calculate movement entirely in 8.8 format, and extend it back to 16.8 format in the end, with these steps:

1) Copy the lower 2 bytes of coordinates, into another memory location.
2) Calculate all movement, using the secondary memory location.
3) Extend it back to 3 bytes, by subtracting it by the low 2 bytes of the 3 byte coordinates, and adding it back as a signed value.

This works because objects are usually within 128 pixels from the previous frame.