pub struct LpVec3 {
pub x: f64,
pub y: f64,
pub z: f64,
}Expand description
Three doubles compressed into a shared-scale, low-precision vector.
LpVec3 normally occupies six bytes. The coordinates are divided by their
rounded-up maximum absolute value, quantized into three unsigned 15-bit
values, and packed with a shared scale factor:
X: 15 bits | Y: 15 bits | Z: 15 bits | continuation: 1 bit | scale: 2 bitsThe first two packed bytes are written in little-endian order, while the
remaining four bytes are written in big-endian order. If the scale factor
is greater than three, its upper bits follow as a VarInt. A vector whose
greatest absolute coordinate is below 1 / 32766, or which contains NaN,
is encoded as the single byte 0x00 and decodes as zero.
This format is used by the Java Edition Spawn Entity and
Set Entity Velocity packets.
§Examples
use mcproto_types::{TypeCodec, basic::LpVec3};
let value = LpVec3::new(10.0, 0.2, -5.0);
let mut encoded = Vec::new();
value.encode(&mut encoded)?;
assert_eq!(encoded, [0xf6, 0xff, 0x40, 0x01, 0x05, 0x1f, 0x02]);
let mut input = encoded.as_slice();
let decoded = LpVec3::decode(&mut input)?;
assert!((decoded.x - value.x).abs() < 0.001);
assert!((decoded.y - value.y).abs() < 0.001);
assert!((decoded.z - value.z).abs() < 0.001);
assert!(input.is_empty());Fields§
§x: f64The x coordinate.
y: f64The y coordinate.
z: f64The z coordinate.
Implementations§
Source§impl LpVec3
impl LpVec3
Sourcepub const MAX_QUANTIZED_VALUE: f64 = 32766.0
pub const MAX_QUANTIZED_VALUE: f64 = 32766.0
Greatest quantized coordinate value stored in a 15-bit field.
Sourcepub const ZERO_THRESHOLD: f64
pub const ZERO_THRESHOLD: f64
Coordinates below this absolute maximum use the one-byte zero form.
Sourcepub const MAX_SCALE_FACTOR: u64
pub const MAX_SCALE_FACTOR: u64
Greatest shared scale factor representable by two low bits and a 32-bit VarInt continuation.