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
impl Transform2d
Sourcepub const IDENTITY: Self
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.
.
Sourcepub fn from_xy(x: f32, y: f32) -> Self
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.
.
Sourcepub fn from_xyz(x: f32, y: f32, z: f32) -> Self
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.
Sourcepub fn from_translation(translation: Vec2) -> Self
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.
.
Sourcepub fn from_translation_3d(_: Vec3) -> Self
pub fn from_translation_3d(_: Vec3) -> Self
Creates a new Transform2d
with translation
.
Rotation will be 0.
and scale will be Vec2::ONE
Sourcepub fn from_rotation(rotation: f32) -> Self
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.
.
Sourcepub fn from_scale(scale: impl IntoScale) -> Self
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.
Sourcepub fn with_translation(self, translation: Vec2) -> Self
pub fn with_translation(self, translation: Vec2) -> Self
Returns this Transform2d
with a new translation.
Sourcepub fn with_rotation(self, rotation: f32) -> Self
pub fn with_rotation(self, rotation: f32) -> Self
Returns this Transform2d
with a new rotation.
Sourcepub fn with_scale(self, scale: Vec2) -> Self
pub fn with_scale(self, scale: Vec2) -> Self
Returns this Transform2d
with a new scale.
Sourcepub fn with_z_translation(self, z_translation: f32) -> Self
pub fn with_z_translation(self, z_translation: f32) -> Self
Returns this Transform2d
with a new Z translation.
Sourcepub fn pointed_to(self, direction: Vec2, target_direction: Vec2) -> Self
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.
Sourcepub fn pointed_at(self, direction: Vec2, target_position: Vec2) -> Self
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.
Sourcepub fn point_to(&mut self, direction: Vec2, target_direction: Vec2)
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.
Sourcepub fn point_at(&mut self, direction: Vec2, target_position: Vec2)
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.
Sourcepub fn left(&self) -> Vec2
pub fn left(&self) -> Vec2
Equivalent to -local_x()
Sourcepub fn down(&self) -> Vec2
pub fn down(&self) -> Vec2
Equivalent to -local_y()
Sourcepub fn rotation_matrix(&self) -> Mat2
pub fn rotation_matrix(&self) -> Mat2
Returns the rotation matrix from this transforms rotation.
Sourcepub fn compute_matrix(&self) -> Mat3
pub fn compute_matrix(&self) -> Mat3
Computes the affine transformation matrix of this transform.
Sourcepub fn compute_affine(&self) -> Affine2
pub fn compute_affine(&self) -> Affine2
Computes the affine transform of this transform.
Sourcepub fn translate_around(&mut self, point: Vec2, angle: f32)
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.
Sourcepub fn rotate_around(&mut self, point: Vec2, angle: f32)
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.
Sourcepub fn transform_point(&self, point: Vec2) -> Vec2
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.
Sourcepub fn mul_transform(&self, transform: Transform2d) -> Self
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
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 Component for Transform2d
impl Component for Transform2d
Source§type Storage = TableStorage
type Storage = TableStorage
TableStorage
or SparseStorage
.Source§impl Debug for Transform2d
impl Debug for Transform2d
Source§impl Default for Transform2d
impl Default for Transform2d
Source§impl From<Transform> for Transform2d
impl From<Transform> for Transform2d
Source§impl From<Transform2d> for Spatial2dBundle
impl From<Transform2d> for Spatial2dBundle
Source§fn from(transform: Transform2d) -> Self
fn from(transform: Transform2d) -> Self
Source§impl From<Transform2d> for Transform
impl From<Transform2d> for Transform
Source§fn from(transform2d: Transform2d) -> Self
fn from(transform2d: Transform2d) -> Self
Source§impl From<Transform2d> for Transform2dBundle
impl From<Transform2d> for Transform2dBundle
Source§fn from(transform: Transform2d) -> Self
fn from(transform: Transform2d) -> Self
Source§impl FromReflect for Transform2d
impl FromReflect for Transform2d
Source§fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
Self
from a reflected value.Source§fn take_from_reflect(
reflect: Box<dyn Reflect>,
) -> Result<Self, Box<dyn Reflect>>
fn take_from_reflect( reflect: Box<dyn Reflect>, ) -> Result<Self, Box<dyn Reflect>>
Self
using,
constructing the value using from_reflect
if that fails. Read moreSource§impl GetTypeRegistration for Transform2d
impl GetTypeRegistration for Transform2d
Source§impl PartialEq for Transform2d
impl PartialEq for Transform2d
Source§impl Reflect for Transform2d
impl Reflect for Transform2d
Source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
Source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
&mut dyn Any
.Source§fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
Source§fn as_reflect(&self) -> &dyn Reflect
fn as_reflect(&self) -> &dyn Reflect
Source§fn as_reflect_mut(&mut self) -> &mut dyn Reflect
fn as_reflect_mut(&mut self) -> &mut dyn Reflect
Source§fn clone_value(&self) -> Box<dyn Reflect>
fn clone_value(&self) -> Box<dyn Reflect>
Reflect
trait object. Read moreSource§fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
Source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
Source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
Source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
Source§fn reflect_owned(self: Box<Self>) -> ReflectOwned
fn reflect_owned(self: Box<Self>) -> ReflectOwned
Source§fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
Source§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
Source§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Source§fn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
Source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
Source§impl Struct for Transform2d
impl Struct for Transform2d
Source§fn field(&self, name: &str) -> Option<&dyn Reflect>
fn field(&self, name: &str) -> Option<&dyn Reflect>
name
as a &dyn Reflect
.Source§fn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>
fn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>
name
as a
&mut dyn Reflect
.Source§fn field_at(&self, index: usize) -> Option<&dyn Reflect>
fn field_at(&self, index: usize) -> Option<&dyn Reflect>
index
as a
&dyn Reflect
.Source§fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>
fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>
index
as a &mut dyn Reflect
.Source§fn name_at(&self, index: usize) -> Option<&str>
fn name_at(&self, index: usize) -> Option<&str>
index
.Source§fn iter_fields(&self) -> FieldIter<'_>
fn iter_fields(&self) -> FieldIter<'_>
Source§fn clone_dynamic(&self) -> DynamicStruct
fn clone_dynamic(&self) -> DynamicStruct
DynamicStruct
.Source§impl TypePath for Transform2d
impl TypePath for Transform2d
Source§fn type_path() -> &'static str
fn type_path() -> &'static str
Source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
Source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
Source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
Source§impl Typed for Transform2d
impl Typed 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 UnwindSafe for Transform2d
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.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<C> Bundle for Cwhere
C: Component,
impl<C> Bundle for Cwhere
C: Component,
fn component_ids( components: &mut Components, storages: &mut Storages, ids: &mut impl FnMut(ComponentId), )
unsafe fn from_components<T, F>(ctx: &mut T, func: &mut F) -> C
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSync for T
Source§impl<C> DynamicBundle for Cwhere
C: Component,
impl<C> DynamicBundle for Cwhere
C: Component,
fn get_components(self, func: &mut impl FnMut(StorageType, OwningPtr<'_>))
Source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
Source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path
.Source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
Source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident
.Source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name
.Source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.Source§impl<S> GetField for Swhere
S: Struct,
impl<S> GetField for Swhere
S: Struct,
Source§impl<T> GetPath for T
impl<T> GetPath for T
Source§fn reflect_path<'p>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path<'p>( &self, path: impl ReflectPath<'p>, ) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moreSource§fn reflect_path_mut<'p>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p>, ) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moreSource§fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
path
. Read moreSource§fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
path
. Read more