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
Transform2Dfor setting local position/rotation/scale - Use
GlobalTransform2Dfor 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
impl GlobalTransform2D
Sourcepub fn mul_transform(&self, other: &GlobalTransform2D) -> GlobalTransform2D
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);Sourcepub fn transform_by(&self, local: &Transform2D) -> GlobalTransform2D
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);Sourcepub fn transform_point(&self, point: Vec2) -> Vec2
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);Sourcepub fn transform_direction(&self, direction: Vec2) -> Vec2
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.
Sourcepub fn inverse(&self) -> Option<GlobalTransform2D>
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).
Sourcepub fn forward(&self) -> Vec2
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.
Sourcepub fn backward(&self) -> Vec2
pub fn backward(&self) -> Vec2
Returns the backward direction vector (negative Y in local space).
Sourcepub fn lerp(&self, other: &GlobalTransform2D, t: f32) -> GlobalTransform2D
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
impl GlobalTransform2D
Sourcepub const IDENTITY: GlobalTransform2D
pub const IDENTITY: GlobalTransform2D
Identity global transform (no transformation).
Sourcepub const fn from_matrix(matrix: Mat3x3) -> GlobalTransform2D
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);Sourcepub fn from_translation(translation: Vec2) -> GlobalTransform2D
pub fn from_translation(translation: Vec2) -> GlobalTransform2D
Sourcepub fn from_translation_rotation_scale(
translation: Vec2,
rotation: f32,
scale: Vec2,
) -> GlobalTransform2D
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 positionrotation- World-space rotation angle in radiansscale- 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(),
);Sourcepub fn matrix(&self) -> Mat3x3
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();Sourcepub fn matrix_ref(&self) -> &Mat3x3
pub fn matrix_ref(&self) -> &Mat3x3
Returns a reference to the underlying matrix.
Sourcepub fn to_cols_array(&self) -> [f32; 9]
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); // m00Sourcepub fn to_mat4_array(&self) -> [f32; 16]
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).
Sourcepub fn translation(&self) -> Vec2
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);Sourcepub fn scale(&self) -> Vec2
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);Sourcepub fn rotation(&self) -> f32
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);Sourcepub fn rotation_degrees(&self) -> f32
pub fn rotation_degrees(&self) -> f32
Extracts the rotation component as degrees.
Sourcepub fn decompose(&self) -> (Vec2, f32, Vec2)
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);Sourcepub fn to_transform(&self) -> Transform2D
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
impl Clone for GlobalTransform2D
Source§fn clone(&self) -> GlobalTransform2D
fn clone(&self) -> GlobalTransform2D
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for GlobalTransform2D
impl Debug for GlobalTransform2D
Source§impl Default for GlobalTransform2D
impl Default for GlobalTransform2D
Source§fn default() -> GlobalTransform2D
fn default() -> GlobalTransform2D
Source§impl<'de> Deserialize<'de> for GlobalTransform2D
impl<'de> Deserialize<'de> for GlobalTransform2D
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<GlobalTransform2D, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<GlobalTransform2D, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl From<&Transform2D> for GlobalTransform2D
impl From<&Transform2D> for GlobalTransform2D
Source§fn from(transform: &Transform2D) -> GlobalTransform2D
fn from(transform: &Transform2D) -> GlobalTransform2D
Source§impl From<Mat3x3> for GlobalTransform2D
impl From<Mat3x3> for GlobalTransform2D
Source§fn from(matrix: Mat3x3) -> GlobalTransform2D
fn from(matrix: Mat3x3) -> GlobalTransform2D
Source§impl From<Transform2D> for GlobalTransform2D
impl From<Transform2D> for GlobalTransform2D
Source§fn from(transform: Transform2D) -> GlobalTransform2D
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
impl Mul<&GlobalTransform2D> for GlobalTransform2D
Source§type Output = GlobalTransform2D
type Output = GlobalTransform2D
* operator.Source§fn mul(self, rhs: &GlobalTransform2D) -> GlobalTransform2D
fn mul(self, rhs: &GlobalTransform2D) -> GlobalTransform2D
* operation. Read moreSource§impl Mul<&Transform2D> for GlobalTransform2D
impl Mul<&Transform2D> for GlobalTransform2D
Source§type Output = GlobalTransform2D
type Output = GlobalTransform2D
* operator.Source§fn mul(self, rhs: &Transform2D) -> GlobalTransform2D
fn mul(self, rhs: &Transform2D) -> GlobalTransform2D
* operation. Read moreSource§impl Mul<Transform2D> for GlobalTransform2D
impl Mul<Transform2D> for GlobalTransform2D
Source§type Output = GlobalTransform2D
type Output = GlobalTransform2D
* operator.Source§fn mul(self, rhs: Transform2D) -> GlobalTransform2D
fn mul(self, rhs: Transform2D) -> GlobalTransform2D
* operation. Read moreSource§impl Mul for GlobalTransform2D
impl Mul for GlobalTransform2D
Source§type Output = GlobalTransform2D
type Output = GlobalTransform2D
* operator.Source§fn mul(self, rhs: GlobalTransform2D) -> GlobalTransform2D
fn mul(self, rhs: GlobalTransform2D) -> GlobalTransform2D
* operation. Read moreSource§impl PartialEq for GlobalTransform2D
impl PartialEq for GlobalTransform2D
Source§impl Serialize for GlobalTransform2D
impl Serialize for GlobalTransform2D
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Component for GlobalTransform2D
impl Copy for GlobalTransform2D
impl StructuralPartialEq for GlobalTransform2D
Auto Trait Implementations§
impl Freeze for GlobalTransform2D
impl RefUnwindSafe for GlobalTransform2D
impl Send for GlobalTransform2D
impl Sync for GlobalTransform2D
impl Unpin for GlobalTransform2D
impl UnsafeUnpin for GlobalTransform2D
impl UnwindSafe for GlobalTransform2D
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().