-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Fixed Point Tutorial - By Harvey Cotton A tutorial written by a moron for morons Updated: 29/01/01 Mail: harvey.c@lineone.net Http: http://www.netyaroze-europe.com/~harveyc/ -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Fixed point mathematics is one of those things that is often misunderstood. No matter how hard you look at them or read about them, they can make no sense. I will attempt to explain them to you. Once you know how to use fixed point, you'll wonder how you ever got along without them! ------------ Introduction ------------ Q: Why Fixed Point? A: For getting precision without using floating point. Q: Whats wrong with float? A: Its bloody slow on the Playstation, which has NO floating point unit. Q: What is a fixed point number? A: You declare it as a long. Long is 32-bit like float. Q: How much faster is it to use fixed point? A: If you are making a game that relies on floats, the speed increase can be 200% to 400%. Q: Is it difficult to use fixed point? A: Nah. Once you start getting use to it, its a piece of cake. ------------------------ Representing fixed point ------------------------ - To represent a float as a fixed point, you simply multiply it by 4096. - This means 4096 is equivalent to 1. - For example the number 10 is equivalent of 40960 because 4096 * 10. - How do you get precision then? Simple. You can represent floats as well. For example you can represent 0.5 as 2048 because 0.5*4096=2048. - Thus, the smallest fraction you can represent is 0.000244140625 because 1/4096. -------------------- What is bitshifting? -------------------- - The value 1, bitshifted left 12 times = 4096. - 1<<12 is the equivalent of 1*4096. - 1>>12 is the equivalent of 1/4096. - 2^12 = 4096. - Bitshifting is fast. Faster than multiplying or dividing; - Bitshifting CANNOT be used with float. ---------------------- Maths with fixed point ---------------------- MATH HOW EXAMPLE Converting a float to fixed: float*4096=fixed 1.5*4096=6144 Converting a fixed to float: fixed>>12=float 4096>>12=1 Adding fixed to fixed: fixed+fixed 4096+4096=8192 Subtracing fixed from fixed: fixed-fixed 4096-4096=0 Multiplying fixed from fixed: (fixed*fixed)>>12 (4096*4096)>>12=4096 Dividing fixed by fixed: (fixed<<12)/fixed (4096<<12)/4096=4096 ------------------------------------------ Example of using fixed point in your games ------------------------------------------ GsSPRITE ship; // Player's sprite long x=100<<12,y=100<<12; // Position of your player at 100,100 represented as fixed point (409600,409600) x--; // Move your left player really, really slowly y-=4096; // Move player up 1 pixel ship.x=x>>12; // Translate your player's x position to sprite ship.y=y>>12; // Translate your player's y position to sprite DrawSprite(&ship); // Draw your ship (function not included!) at 100,99 Now say you want your ship to move even slower, 1/2 a pixel at a time. You change y-=4096; to y-=2048; Obviously theres no such thing as half a pixel, but you will percieve the ship moving at half the speed! Its like moving the ship at 0.5 pixel at a time, without using floating point numbers!!