Class Fx
16.16 fixed-point arithmetic for the platformer simulation.
Every position, speed and size the simulation keeps is an int holding a value
scaled by ONE (65536). Integer arithmetic gives the same answer on every CPU,
every runtime and on the server, which is what lets a run recorded on a phone be
replayed bit for bit by the backend. Nothing in this package uses float,
double or a clock.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic intapproach(int v, int target, int step) Moves a value toward a target by at most a step.static intclamp(int v, int lo, int hi) Clamps a value into a range.static intdiv(int a, int b) Divides two fixed-point values.static intfloor(int a) The largest whole number not greater than a fixed-point value.static intfloorDiv(int a, int b) Integer division that rounds toward negative infinity, as a tile index needs for a coordinate left of or above the map.static Stringformat(int v) Formats a fixed-point value as a decimal with up to four places, the inverse ofparseto the precision a level file needs.static intmul(int a, int b) Multiplies two fixed-point values.static intof(int i) Converts a whole number to fixed point.static intParses a decimal such as"2.75"or"-0.5"into fixed point, exactly and without floating point, so a level file reads the same everywhere.static intratio(int num, int den) Builds a fixed-point value from a fraction,num / den, without floating point.
-
Field Details
-
ONE
public static final int ONEThe value 1.0.- See Also:
-
HALF
public static final int HALFThe value 0.5.- See Also:
-
-
Method Details
-
of
public static int of(int i) Converts a whole number to fixed point.
Parameters
i: the whole number
Returns
iscaled byONE -
ratio
public static int ratio(int num, int den) Builds a fixed-point value from a fraction,
num / den, without floating point.Parameters
num: the numeratorden: the denominator, never zero
Returns
the fraction in fixed point
-
mul
public static int mul(int a, int b) Multiplies two fixed-point values.
Parameters
a: the first factorb: the second factor
Returns
a * bin fixed point -
div
public static int div(int a, int b) Divides two fixed-point values.
Parameters
a: the dividendb: the divisor, never zero
Returns
a / bin fixed point -
floor
public static int floor(int a) The largest whole number not greater than a fixed-point value.
Parameters
a: a fixed-point value
Returns
the floor of
aas a whole number -
floorDiv
public static int floorDiv(int a, int b) Integer division that rounds toward negative infinity, as a tile index needs for a coordinate left of or above the map.
Parameters
a: the dividendb: the divisor, never zero
Returns
the floor of
a / b -
clamp
public static int clamp(int v, int lo, int hi) Clamps a value into a range.
Parameters
v: the valuelo: the lower boundhi: the upper bound
Returns
vlimited to[lo, hi] -
approach
public static int approach(int v, int target, int step) Moves a value toward a target by at most a step.
Parameters
v: the current valuetarget: the value to approachstep: the largest change allowed, not negative
Returns
the new value
-
parse
Parses a decimal such as
"2.75"or"-0.5"into fixed point, exactly and without floating point, so a level file reads the same everywhere.Parameters
s: the decimal text
Returns
the value in fixed point
Throws
IllegalArgumentException: ifsis not a decimal number
-
format
Formats a fixed-point value as a decimal with up to four places, the inverse of
parseto the precision a level file needs.Parameters
v: the fixed-point value
Returns
decimal text
-