Floating Point Movements


Floating point values are values which can be represented as non-whole numbers. For instance, 9.532. But, in MMF2, all object co-ordinates are saved in integer form and any floating point value is rounded down to the nearest whole number.

This is because object co-ordinates are pixel co-ordinates and there is no space between two pixels. But, fear not my friend, objects CAN in fact easily be moved in non-whole number increments and you are about to learn how.

Now, please observe the following objects:

The first ship is moving at a speed of 1 pixel per frame, while the second ship is moving at a speed of 0.9 pixels per frame. The default framerate for MMF2 is 50 frames per second. Meaning, ship 1 is moving 50 pixels per second, while ship 2 is moving 45 pixels per second.

The trick is to store the floating point position in two alterable values, for example Float X and Float Y. This is because alterable values can store floating point values. To move your object, simply add to or subtract from these values. For example... This way, the actual floating point co-ordiantes are not lost when converting to pixel co-ordinates. Then, on every frame, update the X and Y co-ordinates of your object with the alterable values... Alternatively, you can use a third alterable value, Object Speed. This is the amount of pixels per frame your object will move in any given direction. Then to move your object, simply add or subtract the Object Speed from Float X and Float Y. This will make coding easier down the track. God knows why, but you can't set the default value of an alterable value to a floating point number. It must be done during runtime, most likely when the level starts... This is also a good time to load the actual object co-ordinates into the floating point alterable values. This way you can position the starting location of your object within the level editor... Below is an example demonstrating everything we have just talked about. Use the arrow keys to move the ship and press plus or minus to change the ship speed. You may download the example here.


Note: In MMF2, directions and angles are also converted to integers. By storing floating point values in alterable values you can maintain precision in your calculations.