Struct amethyst_core::transform::components::Transform
source · pub struct Transform { /* private fields */ }Expand description
Local position, rotation, and scale (from parent if it exists).
Used for rendering position and orientation.
The transforms are preformed in this order: scale, then rotation, then translation.
Implementations
sourceimpl Transform
impl Transform
sourcepub fn new(
position: Translation3<f32>,
rotation: UnitQuaternion<f32>,
scale: Vector3<f32>
) -> Self
pub fn new(
position: Translation3<f32>,
rotation: UnitQuaternion<f32>,
scale: Vector3<f32>
) -> Self
Create a new Transform.
Examples
let position = Translation3::new(0.0, 2.0, 4.0);
let rotation = UnitQuaternion::from_euler_angles(0.4, 0.2, 0.0);
let scale = Vector3::new(1.0, 1.0, 1.0);
let t = Transform::new(position, rotation, scale);
assert_eq!(t.translation().y, 2.0);sourcepub fn face_towards(
&mut self,
target: Vector3<f32>,
up: Vector3<f32>
) -> &mut Self
pub fn face_towards(
&mut self,
target: Vector3<f32>,
up: Vector3<f32>
) -> &mut Self
Makes the entity point towards position.
up says which direction the entity should be ‘rolled’ to once it is pointing at
position. If up is parallel to the direction the entity is looking, the result will be
garbage.
This function only works with respect to the coordinate system of its parent, so when used with an object that’s not a sibling it will not do what you expect.
Examples
let mut t = Transform::default();
// No rotation by default
assert_eq!(*t.rotation().quaternion(), Quaternion::identity());
// look up with up pointing backwards
t.face_towards(Vector3::new(0.0, 1.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
// our rotation should match the angle from straight ahead to straight up
let rotation = UnitQuaternion::rotation_between(
&Vector3::new(0.0, 1.0, 0.0),
&Vector3::new(0.0, 0.0, 1.0),
).unwrap();
assert_eq!(*t.rotation(), rotation);
// now if we move forwards by 1.0, we'll end up at the point we are facing
// (modulo some floating point error)
t.move_forward(1.0);
assert!((*t.translation() - Vector3::new(0.0, 1.0, 0.0)).magnitude() <= 0.0001);sourcepub fn matrix(&self) -> Matrix4<f32>
pub fn matrix(&self) -> Matrix4<f32>
Returns the local object matrix for the transform.
Combined with the parent’s GlobalTransform component it gives
the global (or world) matrix for the current entity.
sourcepub fn translation(&self) -> &Vector3<f32>
pub fn translation(&self) -> &Vector3<f32>
Returns a reference to the translation vector.
sourcepub fn translation_mut(&mut self) -> &mut Vector3<f32>
pub fn translation_mut(&mut self) -> &mut Vector3<f32>
Returns a mutable reference to the translation vector.
sourcepub fn rotation(&self) -> &UnitQuaternion<f32>
pub fn rotation(&self) -> &UnitQuaternion<f32>
Returns a reference to the rotation quaternion.
sourcepub fn rotation_mut(&mut self) -> &mut UnitQuaternion<f32>
pub fn rotation_mut(&mut self) -> &mut UnitQuaternion<f32>
Returns a mutable reference to the rotation quaternion.
sourcepub fn scale_mut(&mut self) -> &mut Vector3<f32>
pub fn scale_mut(&mut self) -> &mut Vector3<f32>
Returns a mutable reference to the scale vector.
sourcepub fn isometry(&self) -> &Isometry3<f32>
pub fn isometry(&self) -> &Isometry3<f32>
Returns a reference to the isometry of the transform (translation and rotation combined).
sourcepub fn isometry_mut(&mut self) -> &mut Isometry3<f32>
pub fn isometry_mut(&mut self) -> &mut Isometry3<f32>
Returns a mutable reference to the isometry of the transform (translation and rotation combined).
sourcepub fn move_global(&mut self, translation: Vector3<f32>) -> &mut Self
pub fn move_global(&mut self, translation: Vector3<f32>) -> &mut Self
Move relatively to its current position.
sourcepub fn move_local(&mut self, translation: Vector3<f32>) -> &mut Self
pub fn move_local(&mut self, translation: Vector3<f32>) -> &mut Self
Move relatively to its current position and orientation.
Equivalent to rotating the translation before applying.
sourcepub fn move_along_global(
&mut self,
direction: Unit<Vector3<f32>>,
distance: f32
) -> &mut Self
pub fn move_along_global(
&mut self,
direction: Unit<Vector3<f32>>,
distance: f32
) -> &mut Self
Move a distance along an axis.
It will not move in the case where the axis is zero, for any distance.
sourcepub fn move_along_local(
&mut self,
direction: Unit<Vector3<f32>>,
distance: f32
) -> &mut Self
pub fn move_along_local(
&mut self,
direction: Unit<Vector3<f32>>,
distance: f32
) -> &mut Self
Move a distance along an axis.
It will not move in the case where the axis is zero, for any distance.
sourcepub fn move_forward(&mut self, amount: f32) -> &mut Self
pub fn move_forward(&mut self, amount: f32) -> &mut Self
Move forward relative to current position and orientation.
sourcepub fn move_backward(&mut self, amount: f32) -> &mut Self
pub fn move_backward(&mut self, amount: f32) -> &mut Self
Move backward relative to current position and orientation.
sourcepub fn move_right(&mut self, amount: f32) -> &mut Self
pub fn move_right(&mut self, amount: f32) -> &mut Self
Move right relative to current position and orientation.
sourcepub fn move_left(&mut self, amount: f32) -> &mut Self
pub fn move_left(&mut self, amount: f32) -> &mut Self
Move left relative to current position and orientation.
sourcepub fn move_up(&mut self, amount: f32) -> &mut Self
pub fn move_up(&mut self, amount: f32) -> &mut Self
Move up relative to current position and orientation.
sourcepub fn move_down(&mut self, amount: f32) -> &mut Self
pub fn move_down(&mut self, amount: f32) -> &mut Self
Move down relative to current position and orientation.
sourcepub fn translate_x(&mut self, amount: f32) -> &mut Self
pub fn translate_x(&mut self, amount: f32) -> &mut Self
Adds the specified amount to the translation vector’s x component.
sourcepub fn translate_y(&mut self, amount: f32) -> &mut Self
pub fn translate_y(&mut self, amount: f32) -> &mut Self
Adds the specified amount to the translation vector’s y component.
sourcepub fn translate_z(&mut self, amount: f32) -> &mut Self
pub fn translate_z(&mut self, amount: f32) -> &mut Self
Adds the specified amount to the translation vector’s z component.
sourcepub fn set_x(&mut self, value: f32) -> &mut Self
pub fn set_x(&mut self, value: f32) -> &mut Self
Sets the translation vector’s x component to the specified value.
sourcepub fn set_y(&mut self, value: f32) -> &mut Self
pub fn set_y(&mut self, value: f32) -> &mut Self
Sets the translation vector’s y component to the specified value.
sourcepub fn set_z(&mut self, value: f32) -> &mut Self
pub fn set_z(&mut self, value: f32) -> &mut Self
Sets the translation vector’s z component to the specified value.
sourcepub fn pitch_global(&mut self, angle: f32) -> &mut Self
pub fn pitch_global(&mut self, angle: f32) -> &mut Self
Pitch relatively to the world. angle is specified in radians.
sourcepub fn pitch_local(&mut self, angle: f32) -> &mut Self
pub fn pitch_local(&mut self, angle: f32) -> &mut Self
Pitch relatively to its own rotation. angle is specified in radians.
sourcepub fn yaw_global(&mut self, angle: f32) -> &mut Self
pub fn yaw_global(&mut self, angle: f32) -> &mut Self
Yaw relatively to the world. angle is specified in radians.
sourcepub fn yaw_local(&mut self, angle: f32) -> &mut Self
pub fn yaw_local(&mut self, angle: f32) -> &mut Self
Yaw relatively to its own rotation. angle is specified in radians.
sourcepub fn roll_global(&mut self, angle: f32) -> &mut Self
pub fn roll_global(&mut self, angle: f32) -> &mut Self
Roll relatively to the world. angle is specified in radians.
sourcepub fn roll_local(&mut self, angle: f32) -> &mut Self
pub fn roll_local(&mut self, angle: f32) -> &mut Self
Roll relatively to its own rotation. angle is specified in radians.
sourcepub fn rotate_global(&mut self, axis: Unit<Vector3<f32>>, angle: f32) -> &mut Self
pub fn rotate_global(&mut self, axis: Unit<Vector3<f32>>, angle: f32) -> &mut Self
Rotate relatively to the world. angle is specified in radians.
sourcepub fn rotate_local(&mut self, axis: Unit<Vector3<f32>>, angle: f32) -> &mut Self
pub fn rotate_local(&mut self, axis: Unit<Vector3<f32>>, angle: f32) -> &mut Self
Rotate relatively to the current orientation. angle is specified in radians.
sourcepub fn set_position(&mut self, position: Vector3<f32>) -> &mut Self
pub fn set_position(&mut self, position: Vector3<f32>) -> &mut Self
Set the position.
sourcepub fn translate_xyz(&mut self, x: f32, y: f32, z: f32) -> &mut Self
pub fn translate_xyz(&mut self, x: f32, y: f32, z: f32) -> &mut Self
Adds the specified amounts to the translation vector.
sourcepub fn set_xyz(&mut self, x: f32, y: f32, z: f32) -> &mut Self
pub fn set_xyz(&mut self, x: f32, y: f32, z: f32) -> &mut Self
Sets the specified values of the translation vector.
sourcepub fn set_rotation(&mut self, rotation: UnitQuaternion<f32>) -> &mut Self
pub fn set_rotation(&mut self, rotation: UnitQuaternion<f32>) -> &mut Self
Sets the rotation of the transform.
sourcepub fn set_scale(&mut self, x: f32, y: f32, z: f32) -> &mut Self
pub fn set_scale(&mut self, x: f32, y: f32, z: f32) -> &mut Self
Sets the scale of the transform.
sourcepub fn set_rotation_euler(&mut self, x: f32, y: f32, z: f32) -> &mut Self
pub fn set_rotation_euler(&mut self, x: f32, y: f32, z: f32) -> &mut Self
Set the rotation using Euler x, y, z.
All angles are specified in radians. Euler order is roll → pitch → yaw.
Arguments
- x - The angle to apply around the x axis. Also known as the roll.
- y - The angle to apply around the y axis. Also known as the pitch.
- z - The angle to apply around the z axis. Also known as the yaw.
let mut transform = Transform::default();
transform.set_rotation_euler(1.0, 0.0, 0.0);
assert_eq!(transform.rotation().euler_angles().0, 1.0);sourcepub fn concat(&mut self, other: &Self) -> &mut Self
pub fn concat(&mut self, other: &Self) -> &mut Self
Concatenates another transform onto self.
Concatenating is roughly equivalent to doing matrix multiplication except for the fact that
it’s done on Transform which is decomposed.
sourcepub fn view_matrix(&self) -> Matrix4<f32>
pub fn view_matrix(&self) -> Matrix4<f32>
Calculates the inverse of this transform, which we need to render.
We can exploit the extra information we have to perform this inverse faster than O(n^3).
Trait Implementations
sourceimpl Component for Transform
impl Component for Transform
type Storage = FlaggedStorage<Transform, DenseVecStorage<Transform>>
type Storage = FlaggedStorage<Transform, DenseVecStorage<Transform>>
sourceimpl<'de> Deserialize<'de> for Transform
impl<'de> Deserialize<'de> for Transform
sourcefn deserialize<D>(deserializer: D) -> Result<Transform, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Transform, D::Error>where
D: Deserializer<'de>,
sourceimpl From<Matrix<f32, U3, U1, <DefaultAllocator as Allocator<f32, U3, U1>>::Buffer>> for Transform
impl From<Matrix<f32, U3, U1, <DefaultAllocator as Allocator<f32, U3, U1>>::Buffer>> for Transform
Creates a Transform using the Vector3 as the translation vector.
let transform = Transform::from(Vector3::new(100.0, 200.0, 300.0));
assert_eq!(transform.translation().x, 100.0);impl StructuralPartialEq for Transform
Auto Trait Implementations
impl RefUnwindSafe for Transform
impl Send for Transform
impl Sync for Transform
impl Unpin for Transform
impl UnwindSafe for Transform
Blanket Implementations
impl<T> Any for Twhere
T: Any,
impl<T> Any for Twhere
T: Any,
fn get_type_id(&self) -> TypeId
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> Pointable for T
impl<T> Pointable for T
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read morefn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.sourceimpl<T> TryDefault for Twhere
T: Default,
impl<T> TryDefault for Twhere
T: Default,
sourcefn try_default() -> Result<T, String>
fn try_default() -> Result<T, String>
sourcefn unwrap_default() -> Self
fn unwrap_default() -> Self
try_default and panics on an error case.