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

  1. Object
  2. 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 = 65536The value 1.0.
public static final int HALF = 32768The 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 = 65536
The value 1.0.

HALF

public static final int HALF = 32768
The value 0.5.

Method details

of

public static int of(int i)
Converts a whole number to fixed point.

Parameters

i int
the whole number

Returns

i scaled by ONE

ratio

public static int ratio(int num, int den)
Builds a fixed-point value from a fraction, num / den, without floating point.

Parameters

num int
the numerator
den int
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 int
the first factor
b int
the second factor

Returns

a * b in fixed point

div

public static int div(int a, int b)
Divides two fixed-point values.

Parameters

a int
the dividend
b int
the divisor, never zero

Returns

a / b in fixed point

floor

public static int floor(int a)
The largest whole number not greater than a fixed-point value.

Parameters

a int
a fixed-point value

Returns

the floor of a as 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 int
the dividend
b int
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 int
the value
lo int
the lower bound
hi int
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

v int
the current value
target int
the value to approach
step int
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

s String
the decimal text

Returns

the value in fixed point

Throws

IllegalArgumentException
if s is 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

v int
the fixed-point value

Returns

decimal text