Struct Transform2d

Source
pub struct Transform2d {
    pub translation: Vec2,
    pub rotation: f32,
    pub scale: Vec2,
    pub z_translation: f32,
}
Expand description

Describes the position of an Entity in 2d space.

This component acts as a proxy to the Transform component, and thus requires that both a Transform and GlobalTransform are present to function.

If this Transform2d has a Parent, then it’s relative to the Transform2d or Transform of the Parent.

Fields§

§translation: Vec2

The translation along the X and Y axes.

§rotation: f32

The rotation in radians. Positive values rotate anti-clockwise.

§scale: Vec2

The scale along the X and Y axes.

§z_translation: f32

The translation along the Z axis.

You might be surprised that 2D entities have a translation along the Z axis, but this third dimension is used when rendering to decide what should appear in front or behind. A higher translation on the Z axis puts the entity closer to the camera, and thus in front of entities with a lower Z translation.

Keep in mind that this is relative to the Parent’s z_translation. The other fields on Transform2d don’t affect this because they are strictly 2D.

Implementations§

Source§

impl Transform2d

Source

pub const IDENTITY: Self

Creates a new identity Transform2d, with no translation, rotation, and a scale of 1 on all axes.

Translation is Vec2::ZERO, rotation is 0., scale is Vec2::ONE and z_translation is 0..

Source

pub fn from_xy(x: f32, y: f32) -> Self

Creates a new Transform2d at the position (x, y).

Rotation will be 0., scale will be Vec2::ONE and z_translation will be 0..

Source

pub fn from_xyz(x: f32, y: f32, z: f32) -> Self

Creates a new Transform2d at the position (x, y, z). The z component is used for z-ordering elements: higher z-value will be in front of lower z-value.

Source

pub fn from_translation(translation: Vec2) -> Self

Creates a new Transform2d with translation.

Rotation will be 0., scale will be Vec2::ONE and z_translation will be 0..

Source

pub fn from_translation_3d(_: Vec3) -> Self

Creates a new Transform2d with translation.

Rotation will be 0. and scale will be Vec2::ONE

Source

pub fn from_rotation(rotation: f32) -> Self

Creates a new Transform2d with rotation.

Translation will be Vec2::ZERO, scale will be Vec2::ONE and z_translation will be 0..

Source

pub fn from_scale(scale: impl IntoScale) -> Self

Creates a new Transform2d with scale.

Translation will be Vec2::ZERO, rotation will be 0. and z_translation will be 0.

Source

pub fn with_translation(self, translation: Vec2) -> Self

Returns this Transform2d with a new translation.

Source

pub fn with_rotation(self, rotation: f32) -> Self

Returns this Transform2d with a new rotation.

Source

pub fn with_scale(self, scale: Vec2) -> Self

Returns this Transform2d with a new scale.

Source

pub fn with_z_translation(self, z_translation: f32) -> Self

Returns this Transform2d with a new Z translation.

Source

pub fn pointed_to(self, direction: Vec2, target_direction: Vec2) -> Self

Returns this Transform2d rotated so the local direction points in the given target_direction.

§Example
// Create a transform rotated so that up/local_y points to the right.
let transform = Transform2d::IDENTITY.pointed_to(Vec2::Y, Vec2::X);

approx::assert_abs_diff_eq!(transform.up(), Vec2::X);

If this Transform2d has a parent, the point is relative to the Transform2d of the parent.

Source

pub fn pointed_at(self, direction: Vec2, target_position: Vec2) -> Self

Returns this Transform2d rotated so the local direction points at the given target_position.

§Example
// Create a transform that is translated to Vec2::ONE and then rotated so that up/local_y points to the origin.
let transform = Transform2d::from_translation(Vec2::ONE)
    .pointed_at(Vec2::Y, Vec2::ZERO);

approx::assert_abs_diff_eq!(transform.up(), Vec2::NEG_ONE.normalize());

If this Transform2d has a parent, the point is relative to the Transform2d of the parent.

Source

pub fn point_to(&mut self, direction: Vec2, target_direction: Vec2)

Rotates this Transform2d so the local direction points in the given target_direction.

§Example
let mut transform = Transform2d::IDENTITY;

// Rotate the transform so that up/local_y points to the right.
transform.point_to(Vec2::Y, Vec2::X);

approx::assert_abs_diff_eq!(transform.up(), Vec2::X);

If this Transform2d has a parent, the point is relative to the Transform2d of the parent.

Source

pub fn point_at(&mut self, direction: Vec2, target_position: Vec2)

Rotates this Transform2d so the local direction points at the given target_position.

§Example
let mut transform = Transform2d::from_translation(Vec2::ONE);

// Rotate the transform so that up/local_y points to the origin.
transform.point_at(Vec2::Y, Vec2::ZERO);

approx::assert_abs_diff_eq!(transform.up(), Vec2::NEG_ONE.normalize());

If this Transform2d has a parent, the point is relative to the Transform2d of the parent.

Source

pub fn local_x(&self) -> Vec2

Get the unit vector in the local X direction.

Source

pub fn left(&self) -> Vec2

Equivalent to -local_x()

Source

pub fn right(&self) -> Vec2

Equivalent to local_x()

Source

pub fn local_y(&self) -> Vec2

Get the unit vector in the local Y direction.

Source

pub fn up(&self) -> Vec2

Equivalent to local_y()

Source

pub fn down(&self) -> Vec2

Equivalent to -local_y()

Source

pub fn rotation_matrix(&self) -> Mat2

Returns the rotation matrix from this transforms rotation.

Source

pub fn compute_matrix(&self) -> Mat3

Computes the affine transformation matrix of this transform.

Source

pub fn compute_affine(&self) -> Affine2

Computes the affine transform of this transform.

Source

pub fn translate_around(&mut self, point: Vec2, angle: f32)

Translates this Transform2d around a point in space.

If this Transform2d has a parent, the point is relative to the Transform2d or Transform of the parent.

Source

pub fn rotate_around(&mut self, point: Vec2, angle: f32)

Rotates this Transform2d around a point in space.

If this Transform2d has a parent, the point is relative to the Transform2d or Transform of the parent.

Source

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

Transforms the given point, applying scale, rotation and translation. z_translation is ignored.

If this Transform2d has a parent, this will transform a point that is relative to the parent’s Transform2d into one relative to this Transform2d.

If this Transform2d does not have a parent, this will transform a point that is in global space into one relative to this Transform2d.

If you want to transform a point in global space to the local space of this Transform2d, consider using GlobalTransform2d::transform_point() instead.

Source

pub fn mul_transform(&self, transform: Transform2d) -> Self

Multiplies self with transform component by component, returning the resulting Transform2d

Trait Implementations§

Source§

impl Clone for Transform2d

Source§

fn clone(&self) -> Transform2d

Returns a copy 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 Component for Transform2d
where Self: Send + Sync + 'static,

Source§

type Storage = TableStorage

A marker type indicating the storage type used for this component. This must be either TableStorage or SparseStorage.
Source§

impl Debug for Transform2d

Source§

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

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

impl Default for Transform2d

Source§

fn default() -> Self

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

impl From<Transform> for Transform2d

Source§

fn from(transform_3d: Transform) -> Self

Converts to this type from the input type.
Source§

impl From<Transform2d> for Spatial2dBundle

Source§

fn from(transform: Transform2d) -> Self

Converts to this type from the input type.
Source§

impl From<Transform2d> for Transform

Source§

fn from(transform2d: Transform2d) -> Self

Converts to this type from the input type.
Source§

impl From<Transform2d> for Transform2dBundle

Source§

fn from(transform: Transform2d) -> Self

Converts to this type from the input type.
Source§

impl FromReflect for Transform2d

Source§

fn from_reflect(reflect: &dyn Reflect) -> Option<Self>

Constructs a concrete instance of Self from a reflected value.
Source§

fn take_from_reflect( reflect: Box<dyn Reflect>, ) -> Result<Self, Box<dyn Reflect>>

Attempts to downcast the given value to Self using, constructing the value using from_reflect if that fails. Read more
Source§

impl GetTypeRegistration for Transform2d

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 Reflect for Transform2d

Source§

fn get_represented_type_info(&self) -> Option<&'static TypeInfo>

Returns the TypeInfo of the type represented by this value. Read more
Source§

fn into_any(self: Box<Self>) -> Box<dyn Any>

Returns the value as a Box<dyn Any>.
Source§

fn as_any(&self) -> &dyn Any

Returns the value as a &dyn Any.
Source§

fn as_any_mut(&mut self) -> &mut dyn Any

Returns the value as a &mut dyn Any.
Source§

fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>

Casts this type to a boxed reflected value.
Source§

fn as_reflect(&self) -> &dyn Reflect

Casts this type to a reflected value.
Source§

fn as_reflect_mut(&mut self) -> &mut dyn Reflect

Casts this type to a mutable reflected value.
Source§

fn clone_value(&self) -> Box<dyn Reflect>

Clones the value as a Reflect trait object. Read more
Source§

fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>

Performs a type-checked assignment of a reflected value to this value. Read more
Source§

fn apply(&mut self, value: &dyn Reflect)

Applies a reflected value to this value. Read more
Source§

fn reflect_kind(&self) -> ReflectKind

Returns a zero-sized enumeration of “kinds” of type. Read more
Source§

fn reflect_ref(&self) -> ReflectRef<'_>

Returns an immutable enumeration of “kinds” of type. Read more
Source§

fn reflect_mut(&mut self) -> ReflectMut<'_>

Returns a mutable enumeration of “kinds” of type. Read more
Source§

fn reflect_owned(self: Box<Self>) -> ReflectOwned

Returns an owned enumeration of “kinds” of type. Read more
Source§

fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>

Returns a “partial equality” comparison result. Read more
Source§

fn reflect_hash(&self) -> Option<u64>

Returns a hash of the value (which includes the type). Read more
Source§

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

Debug formatter for the value. Read more
Source§

fn serializable(&self) -> Option<Serializable<'_>>

Returns a serializable version of the value. Read more
Source§

fn is_dynamic(&self) -> bool

Indicates whether or not this type is a dynamic type. Read more
Source§

impl Struct for Transform2d

Source§

fn field(&self, name: &str) -> Option<&dyn Reflect>

Returns a reference to the value of the field named name as a &dyn Reflect.
Source§

fn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>

Returns a mutable reference to the value of the field named name as a &mut dyn Reflect.
Source§

fn field_at(&self, index: usize) -> Option<&dyn Reflect>

Returns a reference to the value of the field with index index as a &dyn Reflect.
Source§

fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>

Returns a mutable reference to the value of the field with index index as a &mut dyn Reflect.
Source§

fn name_at(&self, index: usize) -> Option<&str>

Returns the name of the field with index index.
Source§

fn field_len(&self) -> usize

Returns the number of fields in the struct.
Source§

fn iter_fields(&self) -> FieldIter<'_>

Returns an iterator over the values of the reflectable fields for this struct.
Source§

fn clone_dynamic(&self) -> DynamicStruct

Clones the struct into a DynamicStruct.
Source§

impl TypePath for Transform2d
where Self: Any + Send + Sync,

Source§

fn type_path() -> &'static str

Returns the fully qualified path of the underlying type. Read more
Source§

fn short_type_path() -> &'static str

Returns a short, pretty-print enabled path to the type. Read more
Source§

fn type_ident() -> Option<&'static str>

Returns the name of the type, or None if it is anonymous. Read more
Source§

fn crate_name() -> Option<&'static str>

Returns the name of the crate the type is in, or None if it is anonymous. Read more
Source§

fn module_path() -> Option<&'static str>

Returns the path to the module the type is in, or None if it is anonymous. Read more
Source§

impl Typed for Transform2d

Source§

fn type_info() -> &'static TypeInfo

Returns the compile-time info for the underlying type.
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, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

Source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
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<C> Bundle for C
where C: Component,

Source§

fn component_ids( components: &mut Components, storages: &mut Storages, ids: &mut impl FnMut(ComponentId), )

Source§

unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> C
where F: for<'a> FnMut(&'a mut T) -> OwningPtr<'a>,

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

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<C> DynamicBundle for C
where C: Component,

Source§

fn get_components(self, func: &mut impl FnMut(StorageType, OwningPtr<'_>))

Source§

impl<T> DynamicTypePath for T
where T: TypePath,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using data from the given World.
Source§

impl<S> GetField for S
where S: Struct,

Source§

fn get_field<T>(&self, name: &str) -> Option<&T>
where T: Reflect,

Returns a reference to the value of the field named name, downcast to T.
Source§

fn get_field_mut<T>(&mut self, name: &str) -> Option<&mut T>
where T: Reflect,

Returns a mutable reference to the value of the field named name, downcast to T.
Source§

impl<T> GetPath for T
where T: Reflect + ?Sized,

Source§

fn reflect_path<'p>( &self, path: impl ReflectPath<'p>, ) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>

Returns a reference to the value specified by path. Read more
Source§

fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p>, ) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>

Returns a mutable reference to the value specified by path. Read more
Source§

fn path<'p, T>( &self, path: impl ReflectPath<'p>, ) -> Result<&T, ReflectPathError<'p>>
where T: Reflect,

Returns a statically typed reference to the value specified by path. Read more
Source§

fn path_mut<'p, T>( &mut self, path: impl ReflectPath<'p>, ) -> Result<&mut T, ReflectPathError<'p>>
where T: Reflect,

Returns a statically typed mutable reference to the value specified by path. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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> 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> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,