Fix16
Floats/doubles are rarely used in GTA2. Instead, in most cases it uses a fixed point class which we called Fix16. It consists by a single 32 bits signed attribute called mValue.
The precision used in GTA2 for fixed points is 16384 = 0x4000. So a Fix16 with mValue = 16384 is equivalent to 1.0 in float.
Convertions
Very often the game converts Fix16 into u8, s32 or f32.
One example is when the game converts map coordinates (which are Fix16) into "block coordinates", which are integer numbers (like (x,y,z) = (145, 20, 3)).
Converting Fix16 into integer type
Since 16384 = 2^14 = 1 << 14, one of most used conversions is Fix16 -> s32:
or Fix16 -> u8:
One example of its usage is on the following snippet:
This is how it appears on IDA. In almost all cases it shows up as >> 14. The actual code is
The reason that the ToInt() method doesn't appear on IDA is because many of Fix16 methods are inlined on version 10.5 of GTA2.
Converting integer type into Fix16
The other very used conversion is the opposite operation: convert an integer into Fix16. Instead of >> 14, we find << 14. It is contained in one of Fix16 constructors:
It appears on IDA like this:
Note that we rarely use mValue directly. In this case, the actual code should be
Float convertion
In some cases, floats are necessary in the game (such as dealing with graphical render vertices). In these cases Fix16 can be converted into floats using this method:
Since 1/16384 = 0.000061035156, this method appears on IDA on this way:
Actually it is
Fixed point Math
Sum and subtraction between Fix16's are the ordinary operations between integers. So Fix16(1) + Fix16(2) = Fix16(3) and Fix16(3) - Fix16(1) = Fix16(2).
However, multiplication and division between Fix16 are little different. For multiplication, after multiplying their respective mValues, we need to divide the result by the precision, 16384:
Fix16 operator*(const Fix16& in) const
{
s32 value = (s32)((mValue * (__int64)in.mValue) >> 14);
return Fix16(value, 0);
}
For division operation, we multiply the numerator by the precision and then divide it by the denominator's mValue:
Fix16 operator/(const Fix16& in)
{
s32 value = (s32)(((__int64)mValue << 14) / in.mValue);
return Fix16(value, 0);
}
These subleties are justified by fixed point math. For example, since when multiplying two mValues we're carrying two 16384 factors, the result must be normalized dividing it by 16384, so the result is a Fix16.
[!IMPORTANT] If you sum (or subtract) a Fix16 with a integer type, the integer is converted to Fix16 before carrying the operation. So
Fix16(5) + 2is actuallyFix16(5) + Fix16(2).
For multiplication between Fix16 and a integer, the operation is the usual one. For example Fix16(4) * 2 is Fix16(8).
Useful methods
Besides conversion, we have other useful methods:
Absolute value
Square root
One can operate square root of Fix16 using the method below:
Other Fix16 constructors
If one wants to initialize a Fix16 with a raw value for mValue (i.e. without multiplying it by 16384), then they need to put another number (usually zero) as a second argument. The constructor method needed is
So, in this way, Fix16 var = Fix16(0x4000, 0) means that mValue assumes 0x4000 without any conversion.