Skip to main content

GlobalTransform2D

Struct GlobalTransform2D 

Source
pub struct GlobalTransform2D { /* private fields */ }
Expand description

A 2D world-space transformation component.

This component caches the computed world-space transformation matrix for 2D entities in a hierarchy. It is computed by the 2D transform propagation system based on the entity’s local Transform2D and its parent’s GlobalTransform2D.

§When to Use

  • Use Transform2D for setting local position/rotation/scale
  • Use GlobalTransform2D for reading world-space values (rendering, physics)

§Do Not Modify Directly

This component is managed by the transform propagation system. Modifying it directly will cause desynchronization with the entity hierarchy.

§Example

use goud_engine::ecs::components::{Transform2D, GlobalTransform2D};
use goud_engine::core::math::Vec2;

// For root entities, global equals local
let transform = Transform2D::from_position(Vec2::new(100.0, 50.0));
let global = GlobalTransform2D::from(transform);

let position = global.translation();
assert!((position - Vec2::new(100.0, 50.0)).length() < 0.001);

Implementations§

Source§

impl GlobalTransform2D

Source

pub fn mul_transform(&self, other: &GlobalTransform2D) -> GlobalTransform2D

Multiplies this transform with another.

This combines two transformations: self * other applies self first, then other.

§Example
use goud_engine::ecs::components::GlobalTransform2D;
use goud_engine::core::math::Vec2;

let parent = GlobalTransform2D::from_translation(Vec2::new(100.0, 0.0));
let child_local = GlobalTransform2D::from_translation(Vec2::new(50.0, 0.0));

let child_global = parent.mul_transform(&child_local);
let pos = child_global.translation();
assert!((pos.x - 150.0).abs() < 0.001);
Source

pub fn transform_by(&self, local: &Transform2D) -> GlobalTransform2D

Multiplies this transform by a local Transform2D.

This is the primary method used by 2D transform propagation.

§Example
use goud_engine::ecs::components::{GlobalTransform2D, Transform2D};
use goud_engine::core::math::Vec2;

let parent_global = GlobalTransform2D::from_translation(Vec2::new(100.0, 0.0));
let child_local = Transform2D::from_position(Vec2::new(50.0, 0.0));

let child_global = parent_global.transform_by(&child_local);
let pos = child_global.translation();
assert!((pos.x - 150.0).abs() < 0.001);
Source

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

Transforms a point from local space to world space.

§Example
use goud_engine::ecs::components::GlobalTransform2D;
use goud_engine::core::math::Vec2;

let global = GlobalTransform2D::from_translation(Vec2::new(100.0, 0.0));
let local_point = Vec2::new(50.0, 0.0);
let world_point = global.transform_point(local_point);

assert!((world_point.x - 150.0).abs() < 0.001);
Source

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

Transforms a direction from local space to world space.

Unlike points, directions are not affected by translation.

Source

pub fn inverse(&self) -> Option<GlobalTransform2D>

Returns the inverse of this transform.

The inverse transforms from world space back to local space. Returns None if the matrix is not invertible (e.g., has zero scale).

Source

pub fn forward(&self) -> Vec2

Returns the forward direction vector (positive Y in local space for 2D).

Note: In 2D, “forward” is typically the positive Y direction.

Source

pub fn right(&self) -> Vec2

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

Source

pub fn backward(&self) -> Vec2

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

Source

pub fn left(&self) -> Vec2

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

Source

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

Linearly interpolates between two global transforms.

This decomposes both transforms, interpolates components separately (lerp for angle), then recomposes.

§Example
use goud_engine::ecs::components::GlobalTransform2D;
use goud_engine::core::math::Vec2;

let a = GlobalTransform2D::from_translation(Vec2::zero());
let b = GlobalTransform2D::from_translation(Vec2::new(100.0, 0.0));

let mid = a.lerp(&b, 0.5);
let pos = mid.translation();
assert!((pos.x - 50.0).abs() < 0.001);
Source§

impl GlobalTransform2D

Source

pub const IDENTITY: GlobalTransform2D

Identity global transform (no transformation).

Source

pub const fn from_matrix(matrix: Mat3x3) -> GlobalTransform2D

Creates a GlobalTransform2D from a 3x3 transformation matrix.

§Arguments
  • matrix - The world-space transformation matrix
§Example
use goud_engine::ecs::components::GlobalTransform2D;
use goud_engine::ecs::components::Mat3x3;

let matrix = Mat3x3::translation(100.0, 50.0);
let global = GlobalTransform2D::from_matrix(matrix);
Source

pub fn from_translation(translation: Vec2) -> GlobalTransform2D

Creates a GlobalTransform2D from translation only.

§Arguments
  • translation - World-space position
§Example
use goud_engine::ecs::components::GlobalTransform2D;
use goud_engine::core::math::Vec2;

let global = GlobalTransform2D::from_translation(Vec2::new(100.0, 50.0));
Source

pub fn from_translation_rotation_scale( translation: Vec2, rotation: f32, scale: Vec2, ) -> GlobalTransform2D

Creates a GlobalTransform2D from translation, rotation, and scale.

§Arguments
  • translation - World-space position
  • rotation - World-space rotation angle in radians
  • scale - World-space scale
§Example
use goud_engine::ecs::components::GlobalTransform2D;
use goud_engine::core::math::Vec2;

let global = GlobalTransform2D::from_translation_rotation_scale(
    Vec2::new(100.0, 0.0),
    0.0,
    Vec2::one(),
);
Source

pub fn matrix(&self) -> Mat3x3

Returns the underlying 3x3 transformation matrix.

This matrix is column-major and can be used directly for rendering.

§Example
use goud_engine::ecs::components::GlobalTransform2D;

let global = GlobalTransform2D::IDENTITY;
let matrix = global.matrix();
Source

pub fn matrix_ref(&self) -> &Mat3x3

Returns a reference to the underlying matrix.

Source

pub fn to_cols_array(&self) -> [f32; 9]

Returns the matrix as a flat column-major array.

This is useful for FFI and sending to GPU shaders.

§Example
use goud_engine::ecs::components::GlobalTransform2D;

let global = GlobalTransform2D::IDENTITY;
let cols: [f32; 9] = global.to_cols_array();

// First column (x-axis)
assert_eq!(cols[0], 1.0); // m00
Source

pub fn to_mat4_array(&self) -> [f32; 16]

Returns the 2D matrix as a 4x4 matrix array for 3D rendering APIs.

The Z components are set to identity (z=0, w=1).

Source

pub fn translation(&self) -> Vec2

Extracts the translation (position) component.

This is from the third column of the matrix.

§Example
use goud_engine::ecs::components::GlobalTransform2D;
use goud_engine::core::math::Vec2;

let global = GlobalTransform2D::from_translation(Vec2::new(100.0, 50.0));
let pos = global.translation();

assert!((pos.x - 100.0).abs() < 0.001);
Source

pub fn scale(&self) -> Vec2

Extracts the scale component.

This is computed from the lengths of the first two column vectors. Note: This does not handle negative scales correctly.

§Example
use goud_engine::ecs::components::GlobalTransform2D;
use goud_engine::core::math::Vec2;

let global = GlobalTransform2D::from_translation_rotation_scale(
    Vec2::zero(),
    0.0,
    Vec2::new(2.0, 3.0),
);

let scale = global.scale();
assert!((scale.x - 2.0).abs() < 0.001);
Source

pub fn rotation(&self) -> f32

Extracts the rotation component as an angle in radians.

This removes scale from the rotation matrix, then extracts the angle. Note: This may have issues with negative scales.

§Example
use goud_engine::ecs::components::GlobalTransform2D;
use goud_engine::core::math::Vec2;
use std::f32::consts::PI;

let global = GlobalTransform2D::from_translation_rotation_scale(
    Vec2::zero(),
    PI / 4.0,
    Vec2::one(),
);

let rotation = global.rotation();
assert!((rotation - PI / 4.0).abs() < 0.001);
Source

pub fn rotation_degrees(&self) -> f32

Extracts the rotation component as degrees.

Source

pub fn decompose(&self) -> (Vec2, f32, Vec2)

Decomposes the transform into translation, rotation, and scale.

Returns (translation, rotation, scale).

§Example
use goud_engine::ecs::components::GlobalTransform2D;
use goud_engine::core::math::Vec2;

let global = GlobalTransform2D::from_translation_rotation_scale(
    Vec2::new(100.0, 50.0),
    0.0,
    Vec2::new(2.0, 2.0),
);

let (translation, rotation, scale) = global.decompose();
assert!((translation.x - 100.0).abs() < 0.001);
assert!((scale.x - 2.0).abs() < 0.001);
Source

pub fn to_transform(&self) -> Transform2D

Converts this GlobalTransform2D to a local Transform2D.

This is useful for extracting a Transform2D that would produce this GlobalTransform2D when applied from the origin.

Trait Implementations§

Source§

impl Clone for GlobalTransform2D

Source§

fn clone(&self) -> GlobalTransform2D

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 GlobalTransform2D

Source§

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

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

impl Default for GlobalTransform2D

Source§

fn default() -> GlobalTransform2D

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

impl<'de> Deserialize<'de> for GlobalTransform2D

Source§

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

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

impl From<&Transform2D> for GlobalTransform2D

Source§

fn from(transform: &Transform2D) -> GlobalTransform2D

Converts to this type from the input type.
Source§

impl From<Mat3x3> for GlobalTransform2D

Source§

fn from(matrix: Mat3x3) -> GlobalTransform2D

Converts to this type from the input type.
Source§

impl From<Transform2D> for GlobalTransform2D

Source§

fn from(transform: Transform2D) -> GlobalTransform2D

Converts a local Transform2D to a GlobalTransform2D.

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

Source§

impl Mul<&GlobalTransform2D> for GlobalTransform2D

Source§

type Output = GlobalTransform2D

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &GlobalTransform2D) -> GlobalTransform2D

Performs the * operation. Read more
Source§

impl Mul<&Transform2D> for GlobalTransform2D

Source§

type Output = GlobalTransform2D

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Transform2D) -> GlobalTransform2D

Performs the * operation. Read more
Source§

impl Mul<Transform2D> for GlobalTransform2D

Source§

type Output = GlobalTransform2D

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Transform2D) -> GlobalTransform2D

Performs the * operation. Read more
Source§

impl Mul for GlobalTransform2D

Source§

type Output = GlobalTransform2D

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: GlobalTransform2D) -> GlobalTransform2D

Performs the * operation. Read more
Source§

impl PartialEq for GlobalTransform2D

Source§

fn eq(&self, other: &GlobalTransform2D) -> 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 GlobalTransform2D

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 GlobalTransform2D

Source§

impl Copy for GlobalTransform2D

Source§

impl StructuralPartialEq for GlobalTransform2D

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,