Skip to main content

Transform

Struct Transform 

Source
pub struct Transform {
    pub position: DVec3,
    pub rotation: DQuat,
}
Expand description

World-space transform for a physics entity.

This is the authoritative position used by the server simulation. The renderer receives a camera-relative transform derived from this.

Fields§

§position: DVec3

World-space position in metres.

§rotation: DQuat

World-space orientation (unit quaternion).

Implementations§

Source§

impl Transform

Source

pub const IDENTITY: Self

Identity transform at the origin.

Source

pub const ENCODED_LEN: usize

Byte length of the replication encoding written by Self::encode.

The encoding is position (3 × f64 LE) followed by rotation quaternion (4 × f64 LE, x/y/z/w) — 56 bytes.

Source

pub const WIRE_POSITION_EXTENT: f64 = 4096.0

Half the width of the range a replicated position axis is quantized over, in metres, centred on the sector’s origin.

Wire positions are sector-local, and today every snapshot is crcbl_net::SectorId::ZERO with physics never rebasing, so sector-local is world space. The range is bounded by the scenes that replicate, not by crcbl_core::SECTOR_SIZE: a whole sector at Self::WIRE_POSITION_BITS would be a sixteenth of a metre per step, too coarse to watch a crate settle. A body outside it replicates exactly (see Self::encode_wire).

Source

pub const WIRE_POSITION_BITS: u32 = 24

Bits per replicated position axis.

Source

pub const WIRE_POSITION: Fixed

A replicated position axis: Self::WIRE_POSITION_BITS of fixed point over ±Self::WIRE_POSITION_EXTENT. The width is a power of two, so the quantum is too (asserted in the tests) and a position on that grid replicates exactly.

Source

pub const WIRE_ROTATION_BITS: u32 = 18

Bits per sent component of a replicated rotation. With the index bits and the three position axes it fills Self::QUANTIZED_LEN with no padding (asserted below the impl), so the precision costs nothing the payload’s last byte was not already spending.

Source

pub const WIRE_ROTATION: SmallestThree

A replicated rotation: smallest-three at Self::WIRE_ROTATION_BITS.

Source

pub const WIRE_SCHEMA: &'static [Field]

The quantized replication schema: each position axis as Self::WIRE_POSITION fixed point, the rotation as Self::WIRE_ROTATION.

Source

pub const QUANTIZED_LEN: usize

Byte length of the quantized form Self::encode_wire writes.

Source

pub fn new(position: DVec3, rotation: DQuat) -> Self

Create a transform at position with the given rotation.

Source

pub fn from_position(position: DVec3) -> Self

Create a transform at position with identity rotation.

Source

pub fn forward(&self) -> DVec3

The forward direction (local -Z in right-handed coordinates, or local +Z depending on convention; matches glam’s default).

Source

pub fn right(&self) -> DVec3

The right direction (local +X).

Source

pub fn up(&self) -> DVec3

The up direction (local +Y).

Source

pub const fn encoded_len(&self) -> usize

Byte length of the replication encoding written by Self::encode.

Always Self::ENCODED_LEN — the encoding is fixed-width, so this does not depend on self. It takes a receiver only so call sites can write transform.encoded_len() next to transform.encode(..).

Source

pub fn encode(&self, out: &mut Vec<u8>)

Append the replication encoding to out (little-endian, platform- and run-deterministic).

Source

pub fn decode(data: &[u8]) -> Option<Self>

Decode a transform from the encoding written by Self::encode.

Returns None if data is not exactly Self::ENCODED_LEN bytes, if any component is not finite, or if the rotation is not (near enough) a unit quaternion.

This runs on bytes a peer sent us. A NaN or infinite position poisons every AABB it reaches and, through them, the whole BVH; a degenerate quaternion turns Self::forward / Self::right / Self::up into garbage. Neither is representable by Self::encode, so rejecting them here costs a well-behaved peer nothing.

Source

pub fn encode_wire(&self, out: &mut Vec<u8>)

Append the replicated form to out: Self::WIRE_SCHEMA’s Self::QUANTIZED_LEN bytes when the position is inside Self::WIRE_POSITION, otherwise the exact Self::encode form.

The fallback is what keeps an entity outside the declared range where it is: clamping it to the range’s edge would move it on every client and say nothing. The two lengths differ, which is how Self::decode_wire tells them apart.

Source

pub fn decode_wire(data: &[u8]) -> Option<Self>

Decode either form Self::encode_wire writes, telling them apart by length.

Returns None for any other length, and for what Self::decode refuses. A quantized payload always holds a unit quaternion and a finite position; one with a padding bit or a code the encoder never writes is refused.

Source

pub fn lerp(&self, other: &Self, alpha: f64) -> Self

Linearly interpolate between self and other at alpha ∈ [0, 1].

Position is lerped; rotation is glam’s nlerp (shortest-path corrected, renormalised). This is presentation-only smoothing for the client render path — simulation state is never interpolated.

Trait Implementations§

Source§

impl Clone for Transform

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Transform

Source§

impl Debug for Transform

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Transform

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Transform

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Quantized for Transform

The quantized wire form Transform::encode_wire prefers.

Source§

const SCHEMA: &'static [Field] = Self::WIRE_SCHEMA

The component’s fields, in wire order.
Source§

type Values = [f64; 7]

The values Self::SCHEMA consumes, in order: a [f64; N] with N its value_count.
Source§

fn to_values(&self) -> [f64; 7]

The component’s values, in schema order.
Source§

fn from_values(values: &[f64; 7]) -> Option<Self>

The component the decoded values describe, or None if they do not describe a valid one.
Source§

impl StructuralPartialEq for Transform

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.