Skip to main content

Transform2D

Struct Transform2D 

Source
#[repr(C)]
pub struct Transform2D { pub position: Vec2, pub rotation: f32, pub scale: Vec2, }
Expand description

A 2D spatial transformation component.

Represents position, rotation, and scale in 2D space. This is the primary component for positioning entities in 2D games.

§Memory Layout

The component is laid out as:

  • position: 2 x f32 (8 bytes)
  • rotation: f32 (4 bytes)
  • scale: 2 x f32 (8 bytes)
  • Total: 20 bytes

§Example

use goud_engine::ecs::components::Transform2D;
use goud_engine::core::math::Vec2;

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

// Or create with specific position
let transform = Transform2D::from_position(Vec2::new(100.0, 50.0));

// Or with full control
let transform = Transform2D::new(
    Vec2::new(100.0, 50.0),  // position
    0.0,                      // rotation (radians)
    Vec2::one(),              // scale
);

Fields§

§position: Vec2

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

§rotation: f32

Rotation angle in radians (counter-clockwise).

§scale: Vec2

Scale along each axis.

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

Implementations§

Source§

impl Transform2D

Source

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

Translates the transform by the given offset in world space.

Source

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

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: Vec2)

Sets the position of the transform.

Source

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

Rotates the transform by the given angle in radians.

Source

pub fn rotate_degrees(&mut self, degrees: f32)

Rotates the transform by the given angle in degrees.

Source

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

Sets the rotation angle in radians.

Source

pub fn set_rotation_degrees(&mut self, degrees: f32)

Sets the rotation angle in degrees.

Source

pub fn rotation_degrees(&self) -> f32

Returns the rotation angle in degrees.

Source

pub fn look_at_target(&mut self, target: Vec2)

Makes the transform look at a target position.

Source

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

Sets the scale of the transform.

Source

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

Sets uniform scale on both axes.

Source

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

Multiplies the current scale by the given factors.

Source

pub fn forward(&self) -> Vec2

Returns the forward direction vector (positive X axis after rotation).

Source

pub fn right(&self) -> Vec2

Returns the right direction vector (positive Y axis after rotation).

This is perpendicular to forward, rotated 90 degrees counter-clockwise.

Source

pub fn backward(&self) -> Vec2

Returns the backward direction vector (negative X axis after rotation).

Source

pub fn left(&self) -> Vec2

Returns the left direction vector (negative Y axis after rotation).

Source

pub fn matrix(&self) -> Mat3x3

Computes the 3x3 transformation matrix.

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

Source

pub fn matrix_inverse(&self) -> Mat3x3

Computes the inverse transformation matrix.

Useful for converting world-space to local-space.

Source

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

Converts to a 4x4 matrix for use with 3D rendering APIs.

The result places the 2D transform in the XY plane at Z=0.

Source

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

Transforms a point from local space to world space.

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_transform_point(&self, point: Vec2) -> Vec2

Transforms a point from world space to local space.

Source

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

Transforms a direction from world space to local space.

Source

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

Linearly interpolates between two transforms.

Position and scale are linearly interpolated, rotation uses shortest-path angle interpolation.

Source§

impl Transform2D

Source

pub const fn new(position: Vec2, rotation: f32, scale: Vec2) -> Transform2D

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

Source

pub fn from_position(position: Vec2) -> Transform2D

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

Source

pub fn from_rotation(rotation: f32) -> Transform2D

Creates a Transform2D at the origin with the specified rotation.

Source

pub fn from_rotation_degrees(degrees: f32) -> Transform2D

Creates a Transform2D with specified rotation in degrees.

Source

pub fn from_scale(scale: Vec2) -> Transform2D

Creates a Transform2D at the origin with the specified scale.

Source

pub fn from_scale_uniform(scale: f32) -> Transform2D

Creates a Transform2D with uniform scale.

Source

pub fn from_position_rotation(position: Vec2, rotation: f32) -> Transform2D

Creates a Transform2D with position and rotation.

Source

pub fn look_at(position: Vec2, target: Vec2) -> Transform2D

Creates a Transform2D looking at a target position.

The transform’s forward direction (positive X after rotation) will point towards the target.

Trait Implementations§

Source§

impl Clone for Transform2D

Source§

fn clone(&self) -> Transform2D

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 Transform2D

Source§

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

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

impl Default for Transform2D

Source§

fn default() -> Transform2D

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

Source§

impl<'de> Deserialize<'de> for Transform2D

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Transform2D, <__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<FfiTransform2D> for Transform2D

Source§

fn from(t: FfiTransform2D) -> Transform2D

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<&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 PartialEq for Transform2D

Source§

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

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 Transform2D

Source§

impl Copy for Transform2D

Source§

impl StructuralPartialEq for Transform2D

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,