JFLib

game/src/net/quant.rs#

Source: lib/game/src/net/quant.rs

quant -- quantisation: the netcode keeps what it predicts and sends on fixed grids, so the server's
state, what it sends and what a client rebuilds are the same integers

  POS_STEPS        steps a metre (1024: 0.98 mm, exact in f64 because it is a power of two)
  pos_q / pos_f    a coordinate to its step and back (round half away from 0); vec_q / vec_f for three
  ANGLE_STEPS      a turn in 65 536 steps (0.0055 degrees); angle_q (wraps), angle_f in (-pi, pi]
  pitch_q / pitch_f  a pitch in i16 steps of the same size (+-90 degrees fit)
  axis_q / axis_f  an axis value -1 ..= 1 as -127 ..= 127
  rot_q / rot_f    a rotation by its smallest three components (which one is left out, 2 bits, and three
                   of ROT_BITS each): at most about 0.07 degrees at ROT_BITS = 12
  round_i64        rounding without std (core has no f64::round)

A grid of 2^-10 m reaches +-2 097 km in an i32.  Whatever a client predicts (the movement state of its
figure) stays on the grid on both sides after every step (docs/NETCODE.md, "Bitgleich"), so the state a
snapshot carries is the server's state exactly, not an approximation of it.
/// steps a metre of the position grid
POS_STEPS: f64 = 1024.0
/// steps a full turn of an angle
ANGLE_STEPS: f64 = 65536.0
/// bits of each of the three components of rot_q
ROT_BITS: u32 = 12
  • fn round_i64(x: f64) -> i64 x rounded to the nearest whole number, halves away from 0 (saturating at the ends of i64; NaN is 0)

  • fn pos_q(x: f64) -> i32 a coordinate in steps of POS_STEPS (saturating at the ends of i32)

  • fn pos_f(q: i32) -> f64 the coordinate of a step

  • fn vec_q(v: Vec3) -> [i32; 3] a vector in steps

  • fn vec_f(q: [i32; 3]) -> Vec3 the vector of steps

  • fn angle_q(a: f64) -> u16 an angle in radians as a step of ANGLE_STEPS a turn (any angle: it wraps)

  • fn angle_f(q: u16) -> f64 the angle of a step in (-pi, pi]

  • fn pitch_q(p: f64) -> i16 a pitch in radians as an i16 step of the same size as angle_q (clamped to +-pi/2)

  • fn pitch_f(q: i16) -> f64 the pitch of a step

  • fn axis_q(v: f64) -> i8 an axis value -1 ..= 1 as -127 ..= 127

  • fn axis_f(q: i8) -> f64 the axis value of a step (-128 counts as -127)

  • fn rot_q(q: Quat) -> [i32; 4] a rotation as (index of the largest component, its three others in ROT_BITS each, signed so that the largest is positive); a quaternion and its negative give the same

  • fn rot_f(r: [i32; 4]) -> Quat the rotation of rot_q (the left-out component from the unit length); a bad index gives the identity

Source: docs/API.md in the JFLib repository.