Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public final class Fx
- Object
- 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.
Fields
public static final int ONE = 65536 | The value 1.0. |
public static final int HALF = 32768 | The value 0.5. |
Methods
public static int of(int i) | Converts a whole number to fixed point. |
public static int ratio(int num, int den) | Builds a fixed-point value from a fraction, num / den, without floating point. |
public static int mul(int a, int b) | Multiplies two fixed-point values. |
public static int div(int a, int b) | Divides two fixed-point values. |
public static int floor(int a) | The largest whole number not greater than a fixed-point value. |
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. |
public static int clamp(int v, int lo, int hi) | Clamps a value into a range. |
public static int approach(int v, int target, int step) | Moves a value toward a target by at most a step. |
public static int parse(String s) | 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. |
public static String format(int v) | Formats a fixed-point value as a decimal with up to four places, the inverse of parse to the precision a level file needs. |
Inherited methods
Field details
ONE
public static final int ONE = 65536The value 1.0.
HALF
public static final int HALF = 32768The value 0.5.
Method details
of
public static int of(int i)Converts a whole number to fixed point.
Parameters
iint- the whole number
Returns
i scaled by ONEratio
public static int ratio(int num, int den)Builds a fixed-point value from a fraction,
num / den, without floating point.Parameters
numint- the numerator
denint- 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
aint- the first factor
bint- the second factor
Returns
a * b in fixed pointdiv
public static int div(int a, int b)Divides two fixed-point values.
Parameters
aint- the dividend
bint- the divisor, never zero
Returns
a / b in fixed pointfloor
public static int floor(int a)The largest whole number not greater than a fixed-point value.
Parameters
aint- a fixed-point value
Returns
the floor of
a as a whole numberfloorDiv
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
aint- the dividend
bint- the divisor, never zero
Returns
the floor of
a / bclamp
public static int clamp(int v, int lo, int hi)Clamps a value into a range.
Parameters
vint- the value
loint- the lower bound
hiint- the upper bound
Returns
v limited 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
vint- the current value
targetint- the value to approach
stepint- the largest change allowed, not negative
Returns
the new value
parse
public static int parse(String s)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
sString- the decimal text
Returns
the value in fixed point
Throws
IllegalArgumentException- if
sis not a decimal number
format
public static String format(int v)Formats a fixed-point value as a decimal with up to four places, the inverse
of
parse to the precision a level file needs.Parameters
vint- the fixed-point value
Returns
decimal text