Class Fx

java.lang.Object
com.codename1.gaming.platformer.Fx

public final class Fx extends Object

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
    Modifier and Type
    Field
    Description
    static final int
    The value 0.5.
    static final int
    The value 1.0.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    approach(int v, int target, int step)
    Moves a value toward a target by at most a step.
    static int
    clamp(int v, int lo, int hi)
    Clamps a value into a range.
    static int
    div(int a, int b)
    Divides two fixed-point values.
    static int
    floor(int a)
    The largest whole number not greater than a fixed-point value.
    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.
    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.
    static int
    mul(int a, int b)
    Multiplies two fixed-point values.
    static int
    of(int i)
    Converts a whole number to fixed point.
    static int
    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.
    static int
    ratio(int num, int den)
    Builds a fixed-point value from a fraction, num / den, without floating point.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • of

      public static int of(int i)

      Converts a whole number to fixed point.

      Parameters
      • i: 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: the numerator
      • den: 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 factor
      • b: 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: the dividend
      • b: 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: 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: the dividend
      • b: 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 value
      • lo: the lower bound
      • hi: 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: the current value
      • target: the value to approach
      • step: 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: 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: the fixed-point value
      Returns

      decimal text