Skip to content

Commit

Permalink
Add constants. Deprecate identity().
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-blackbird committed Jul 16, 2022
1 parent f531a94 commit b3d2bee
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
8 changes: 6 additions & 2 deletions crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ macro_rules! impl_local_axis {
}

impl GlobalTransform {
/// An identity [`GlobalTransform`] that maps all points in space to themselves.
pub const IDENTITY: Self = Self(Affine3A::IDENTITY);

#[doc(hidden)]
#[inline]
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
Expand Down Expand Up @@ -107,8 +110,9 @@ impl GlobalTransform {

/// Creates a new identity [`GlobalTransform`], that maps all points in space to themselves.
#[inline]
#[deprecated = "Use `GlobalTransform::IDENTITY` instead."]
pub const fn identity() -> Self {
Self(Affine3A::IDENTITY)
GlobalTransform::IDENTITY
}

impl_local_axis!(right, left, X);
Expand Down Expand Up @@ -154,7 +158,7 @@ impl GlobalTransform {

impl Default for GlobalTransform {
fn default() -> Self {
Self::identity()
Self::IDENTITY
}
}

Expand Down
22 changes: 13 additions & 9 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ pub struct Transform {
}

impl Transform {
/// An identity [`Transform`] with no translation, rotation, and a scale of 1 on all axes.
pub const IDENTITY: Self = Transform {
translation: Vec3::ZERO,
rotation: Quat::IDENTITY,
scale: Vec3::ONE,
};

/// Creates a new [`Transform`] at the position `(x, y, z)`. In 2d, the `z` component
/// is used for z-ordering elements: higher `z`-value will be in front of lower
/// `z`-value.
Expand All @@ -49,12 +56,9 @@ impl Transform {
/// Creates a new identity [`Transform`], with no translation, rotation, and a scale of 1 on
/// all axes.
#[inline]
#[deprecated = "Use `Transform::IDENTITY` instead."]
pub const fn identity() -> Self {
Transform {
translation: Vec3::ZERO,
rotation: Quat::IDENTITY,
scale: Vec3::ONE,
}
Transform::IDENTITY
}

/// Extracts the translation, rotation, and scale from `matrix`. It must be a 3d affine
Expand All @@ -76,7 +80,7 @@ impl Transform {
pub const fn from_translation(translation: Vec3) -> Self {
Transform {
translation,
..Self::identity()
..Self::IDENTITY
}
}

Expand All @@ -86,7 +90,7 @@ impl Transform {
pub const fn from_rotation(rotation: Quat) -> Self {
Transform {
rotation,
..Self::identity()
..Self::IDENTITY
}
}

Expand All @@ -96,7 +100,7 @@ impl Transform {
pub const fn from_scale(scale: Vec3) -> Self {
Transform {
scale,
..Self::identity()
..Self::IDENTITY
}
}

Expand Down Expand Up @@ -335,7 +339,7 @@ impl Transform {

impl Default for Transform {
fn default() -> Self {
Self::identity()
Self::IDENTITY
}
}

Expand Down
15 changes: 9 additions & 6 deletions crates/bevy_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub struct TransformBundle {
}

impl TransformBundle {
/// An identity [`TransformBundle`] with no translation, rotation, and a scale of 1 on all axes.
pub const IDENTITY: Self = Self {
local: Transform::IDENTITY,
global: GlobalTransform::IDENTITY,
};

/// Creates a new [`TransformBundle`] from a [`Transform`].
///
/// This initializes [`GlobalTransform`] as identity, to be updated later by the
Expand All @@ -54,19 +60,16 @@ impl TransformBundle {
pub const fn from_transform(transform: Transform) -> Self {
TransformBundle {
local: transform,
// Note: `..Default::default()` cannot be used here, because it isn't const
..Self::identity()
..Self::IDENTITY
}
}

/// Creates a new identity [`TransformBundle`], with no translation, rotation, and a scale of 1
/// on all axes.
#[inline]
#[deprecated = "Use `TransformBundle::IDENTITY` instead."]
pub const fn identity() -> Self {
TransformBundle {
local: Transform::identity(),
global: GlobalTransform::identity(),
}
TransformBundle::IDENTITY
}
}

Expand Down

0 comments on commit b3d2bee

Please sign in to comment.