Skip to main content

Transform

Struct Transform 

Source
#[repr(C)]
pub struct Transform { pub position: Vec3, pub rotation: Quat, pub scale: Vec3, }
Expand description

A 3D spatial transformation component.

Represents position, rotation, and scale in 3D space. This is the primary component for positioning entities in the game world.

§Memory Layout

The component is laid out as:

  • position: 3 x f32 (12 bytes)
  • rotation: 4 x f32 (16 bytes) - quaternion (x, y, z, w)
  • scale: 3 x f32 (12 bytes)
  • Total: 40 bytes

§Example

use goud_engine::ecs::components::Transform;
use goud_engine::core::math::Vec3;

// Create at origin with default rotation and scale
let mut transform = Transform::default();

// Or create with specific position
let transform = Transform::from_position(Vec3::new(10.0, 0.0, 0.0));

// Or with full control
let transform = Transform::new(
    Vec3::new(10.0, 5.0, 0.0),     // position
    Transform::IDENTITY_ROTATION,   // rotation (identity quaternion)
    Vec3::one(),                    // scale
);

Fields§

§position: Vec3

Position in world space (or local space if entity has a parent).

§rotation: Quat

Rotation as a quaternion (x, y, z, w).

The quaternion should be normalized. Use the rotation methods to ensure this invariant is maintained.

§scale: Vec3

Scale along each axis.

A scale of (1, 1, 1) means no scaling. Negative values flip the object along that axis.

Implementations§

Source§

impl Transform

Source

pub const IDENTITY_ROTATION: Quat = Quat::IDENTITY

Identity rotation (no rotation).

Source

pub const fn new(position: Vec3, rotation: Quat, scale: Vec3) -> Transform

Creates a new Transform with the specified position, rotation, and scale.

Source

pub fn from_position(position: Vec3) -> Transform

Creates a Transform at the specified position with default rotation and scale.

Source

pub fn from_rotation(rotation: Quat) -> Transform

Creates a Transform with the specified rotation and default position/scale.

Source

pub fn from_scale(scale: Vec3) -> Transform

Creates a Transform with the specified scale and default position/rotation.

Source

pub fn from_scale_uniform(scale: f32) -> Transform

Creates a Transform with uniform scale.

Source

pub fn from_position_rotation(position: Vec3, rotation: Quat) -> Transform

Creates a Transform with position and rotation.

Source

pub fn look_at(eye: Vec3, target: Vec3, up: Vec3) -> Transform

Creates a Transform looking at a target position.

§Arguments
  • eye - The position of the transform
  • target - The point to look at
  • up - The up direction (usually Vec3::unit_y())
§Example
use goud_engine::ecs::components::Transform;
use goud_engine::core::math::Vec3;

let transform = Transform::look_at(
    Vec3::new(0.0, 5.0, 10.0),  // eye position
    Vec3::zero(),               // looking at origin
    Vec3::unit_y(),             // up direction
);
Source

pub fn matrix(&self) -> Matrix4<f32>

Computes the 4x4 transformation matrix.

The matrix represents the combined transformation: Scale * Rotation * Translation (applied in that order when transforming points).

Source

pub fn matrix_inverse(&self) -> Matrix4<f32>

Computes the inverse transformation matrix.

Useful for view matrices or converting world-space to local-space.

Source§

impl Transform

Source

pub fn translate(&mut self, offset: Vec3)

Translates the transform by the given offset.

Source

pub fn translate_local(&mut self, offset: Vec3)

Translates the transform in local space.

The offset is rotated by the transform’s rotation before being applied.

Source

pub fn set_position(&mut self, position: Vec3)

Sets the position of the transform.

Source

pub fn rotate(&mut self, rotation: Quat)

Rotates the transform by the given quaternion.

The rotation is applied in world space (rotation is applied after the current rotation).

Source

pub fn rotate_x(&mut self, angle: f32)

Rotates around the X axis by the given angle in radians.

Source

pub fn rotate_y(&mut self, angle: f32)

Rotates around the Y axis by the given angle in radians.

Source

pub fn rotate_z(&mut self, angle: f32)

Rotates around the Z axis by the given angle in radians.

Source

pub fn rotate_axis(&mut self, axis: Vec3, angle: f32)

Rotates around an arbitrary axis by the given angle in radians.

Source

pub fn rotate_local_x(&mut self, angle: f32)

Rotates in local space around the X axis.

Source

pub fn rotate_local_y(&mut self, angle: f32)

Rotates in local space around the Y axis.

Source

pub fn rotate_local_z(&mut self, angle: f32)

Rotates in local space around the Z axis.

Source

pub fn set_rotation_euler(&mut self, pitch: f32, yaw: f32, roll: f32)

Sets the rotation from Euler angles (in radians).

Source

pub fn set_rotation(&mut self, rotation: Quat)

Sets the rotation.

Source

pub fn look_at_target(&mut self, target: Vec3, up: Vec3)

Makes the transform look at a target position.

Source

pub fn set_scale(&mut self, scale: Vec3)

Sets the scale of the transform.

Source

pub fn set_scale_uniform(&mut self, scale: f32)

Sets uniform scale on all axes.

Source

pub fn scale_by(&mut self, factors: Vec3)

Multiplies the current scale by the given factors.

Source

pub fn forward(&self) -> Vec3

Returns the forward direction vector (negative Z in local space).

Source

pub fn right(&self) -> Vec3

Returns the right direction vector (positive X in local space).

Source

pub fn up(&self) -> Vec3

Returns the up direction vector (positive Y in local space).

Source

pub fn back(&self) -> Vec3

Returns the back direction vector (positive Z in local space).

Source

pub fn left(&self) -> Vec3

Returns the left direction vector (negative X in local space).

Source

pub fn down(&self) -> Vec3

Returns the down direction vector (negative Y in local space).

Source

pub fn transform_point(&self, point: Vec3) -> Vec3

Transforms a point from local space to world space.

Source

pub fn transform_direction(&self, direction: Vec3) -> Vec3

Transforms a direction from local space to world space.

Unlike points, directions are not affected by translation.

Source

pub fn inverse_transform_point(&self, point: Vec3) -> Vec3

Transforms a point from world space to local space.

Source

pub fn inverse_transform_direction(&self, direction: Vec3) -> Vec3

Transforms a direction from world space to local space.

Source

pub fn lerp(self, other: Transform, t: f32) -> Transform

Linearly interpolates between two transforms.

Position and scale are linearly interpolated, rotation uses slerp.

Trait Implementations§

Source§

impl Clone for Transform

Source§

fn clone(&self) -> Transform

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Transform

Source§

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

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

impl Default for Transform

Source§

fn default() -> Transform

Returns a Transform at the origin with no rotation and unit scale.

Source§

impl<'de> Deserialize<'de> for Transform

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Transform, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<&Transform> for GlobalTransform

Source§

fn from(transform: &Transform) -> GlobalTransform

Converts to this type from the input type.
Source§

impl From<Transform> for GlobalTransform

Source§

fn from(transform: Transform) -> GlobalTransform

Converts a local Transform to a GlobalTransform.

This is used for root entities where local == global.

Source§

impl Mul<&Transform> for GlobalTransform

Source§

type Output = GlobalTransform

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Transform) -> GlobalTransform

Performs the * operation. Read more
Source§

impl Mul<Transform> for GlobalTransform

Source§

type Output = GlobalTransform

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Transform) -> GlobalTransform

Performs the * operation. Read more
Source§

impl PartialEq for Transform

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Transform

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Component for Transform

Source§

impl Copy for Transform

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<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
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> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

impl<T> QueryState for T
where T: Send + Sync + Clone + 'static,

Source§

impl<T> Resource for T
where T: Send + Sync + 'static,