#[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: Vec2Position in world space (or local space if entity has a parent).
rotation: f32Rotation angle in radians (counter-clockwise).
scale: Vec2Scale along each axis.
A scale of (1, 1) means no scaling. Negative values flip the object along that axis.
Implementations§
Source§impl Transform2D
impl Transform2D
Sourcepub fn translate(&mut self, offset: Vec2)
pub fn translate(&mut self, offset: Vec2)
Translates the transform by the given offset in world space.
Sourcepub fn translate_local(&mut self, offset: Vec2)
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.
Sourcepub fn set_position(&mut self, position: Vec2)
pub fn set_position(&mut self, position: Vec2)
Sets the position of the transform.
Sourcepub fn rotate_degrees(&mut self, degrees: f32)
pub fn rotate_degrees(&mut self, degrees: f32)
Rotates the transform by the given angle in degrees.
Sourcepub fn set_rotation(&mut self, rotation: f32)
pub fn set_rotation(&mut self, rotation: f32)
Sets the rotation angle in radians.
Sourcepub fn set_rotation_degrees(&mut self, degrees: f32)
pub fn set_rotation_degrees(&mut self, degrees: f32)
Sets the rotation angle in degrees.
Sourcepub fn rotation_degrees(&self) -> f32
pub fn rotation_degrees(&self) -> f32
Returns the rotation angle in degrees.
Sourcepub fn look_at_target(&mut self, target: Vec2)
pub fn look_at_target(&mut self, target: Vec2)
Makes the transform look at a target position.
Sourcepub fn set_scale_uniform(&mut self, scale: f32)
pub fn set_scale_uniform(&mut self, scale: f32)
Sets uniform scale on both axes.
Sourcepub fn forward(&self) -> Vec2
pub fn forward(&self) -> Vec2
Returns the forward direction vector (positive X axis after rotation).
Sourcepub fn right(&self) -> Vec2
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.
Sourcepub fn backward(&self) -> Vec2
pub fn backward(&self) -> Vec2
Returns the backward direction vector (negative X axis after rotation).
Sourcepub fn matrix(&self) -> Mat3x3
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).
Sourcepub fn matrix_inverse(&self) -> Mat3x3
pub fn matrix_inverse(&self) -> Mat3x3
Computes the inverse transformation matrix.
Useful for converting world-space to local-space.
Sourcepub fn to_mat4(&self) -> [f32; 16]
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.
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.
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_transform_point(&self, point: Vec2) -> Vec2
pub fn inverse_transform_point(&self, point: Vec2) -> Vec2
Transforms a point from world space to local space.
Sourcepub fn inverse_transform_direction(&self, direction: Vec2) -> Vec2
pub fn inverse_transform_direction(&self, direction: Vec2) -> Vec2
Transforms a direction from world space to local space.
Sourcepub fn lerp(self, other: Transform2D, t: f32) -> Transform2D
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
impl Transform2D
Sourcepub const fn new(position: Vec2, rotation: f32, scale: Vec2) -> Transform2D
pub const fn new(position: Vec2, rotation: f32, scale: Vec2) -> Transform2D
Creates a new Transform2D with the specified position, rotation, and scale.
Sourcepub fn from_position(position: Vec2) -> Transform2D
pub fn from_position(position: Vec2) -> Transform2D
Creates a Transform2D at the specified position with default rotation and scale.
Sourcepub fn from_rotation(rotation: f32) -> Transform2D
pub fn from_rotation(rotation: f32) -> Transform2D
Creates a Transform2D at the origin with the specified rotation.
Sourcepub fn from_rotation_degrees(degrees: f32) -> Transform2D
pub fn from_rotation_degrees(degrees: f32) -> Transform2D
Creates a Transform2D with specified rotation in degrees.
Sourcepub fn from_scale(scale: Vec2) -> Transform2D
pub fn from_scale(scale: Vec2) -> Transform2D
Creates a Transform2D at the origin with the specified scale.
Sourcepub fn from_scale_uniform(scale: f32) -> Transform2D
pub fn from_scale_uniform(scale: f32) -> Transform2D
Creates a Transform2D with uniform scale.
Sourcepub fn from_position_rotation(position: Vec2, rotation: f32) -> Transform2D
pub fn from_position_rotation(position: Vec2, rotation: f32) -> Transform2D
Creates a Transform2D with position and rotation.
Sourcepub fn look_at(position: Vec2, target: Vec2) -> Transform2D
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
impl Clone for Transform2D
Source§fn clone(&self) -> Transform2D
fn clone(&self) -> Transform2D
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Transform2D
impl Debug for Transform2D
Source§impl Default for Transform2D
impl Default for Transform2D
Source§fn default() -> Transform2D
fn default() -> Transform2D
Returns a Transform2D at the origin with no rotation and unit scale.
Source§impl<'de> Deserialize<'de> for Transform2D
impl<'de> Deserialize<'de> for Transform2D
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Transform2D, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Transform2D, <__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<FfiTransform2D> for Transform2D
impl From<FfiTransform2D> for Transform2D
Source§fn from(t: FfiTransform2D) -> Transform2D
fn from(t: FfiTransform2D) -> Transform2D
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<&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 PartialEq for Transform2D
impl PartialEq for Transform2D
Source§impl Serialize for Transform2D
impl Serialize for Transform2D
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 Transform2D
impl Copy for Transform2D
impl StructuralPartialEq for Transform2D
Auto Trait Implementations§
impl Freeze for Transform2D
impl RefUnwindSafe for Transform2D
impl Send for Transform2D
impl Sync for Transform2D
impl Unpin for Transform2D
impl UnsafeUnpin for Transform2D
impl UnwindSafe for Transform2D
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().