Skip to content

Commit 7329d60

Browse files
Sigma-devSigma-devalice-i-cecile
authored andcommitted
Improved the global transform api to access rotation and scale (#16211)
# Objective GlobalTransform's current methods make it unintuitive, long and clunky to access just the rotation or just the scale. ## Solution Dedicated just_rotation() and scale() methods to access just these properties. I'm not sure about the naming, I chose just_rotation() to show that try to indicate there is a waste since it also computes the other fields. ## Testing - Did you test these changes? If so, how? I tried logging the methods with a rotating and scaling cube and the values were correct. - Are there any parts that need more testing? My methods are based on existing bevy/glam methods so should be correct from the getgo. - How can other people (reviewers) test your changes? Is there anything specific they need to know? Probably the easiest is using the 3d_rotations example, adding scaling to it and then logging the methods I added --- ## Showcase ```rust fn log(gt_query: Query<&GlobalTransform>) { for global_transform in gt_query().iter() { println!("{} {}", global_transform.just_rotation(), global_transform.scale()); } } ``` --------- Co-authored-by: Sigma-dev <[email protected]> Co-authored-by: Alice Cecile <[email protected]>
1 parent efd9d22 commit 7329d60

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

crates/bevy_transform/src/components/global_transform.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,36 @@ impl GlobalTransform {
211211
self.0.translation
212212
}
213213

214+
/// Get the rotation as a [`Quat`].
215+
///
216+
/// The transform is expected to be non-degenerate and without shearing, or the output will be invalid.
217+
///
218+
/// # Warning
219+
///
220+
/// This is calculated using `to_scale_rotation_translation`, meaning that you
221+
/// should probably use it directly if you also need translation or scale.
222+
#[inline]
223+
pub fn rotation(&self) -> Quat {
224+
self.to_scale_rotation_translation().1
225+
}
226+
227+
/// Get the scale as a [`Vec3`].
228+
///
229+
/// The transform is expected to be non-degenerate and without shearing, or the output will be invalid.
230+
///
231+
/// Some of the computations overlap with `to_scale_rotation_translation`, which means you should use
232+
/// it instead if you also need rotation.
233+
#[inline]
234+
pub fn scale(&self) -> Vec3 {
235+
//Formula based on glam's implementation https://github.com/bitshifter/glam-rs/blob/2e4443e70c709710dfb25958d866d29b11ed3e2b/src/f32/affine3a.rs#L290
236+
let det = self.0.matrix3.determinant();
237+
Vec3::new(
238+
self.0.matrix3.x_axis.length() * det.signum(),
239+
self.0.matrix3.y_axis.length(),
240+
self.0.matrix3.z_axis.length(),
241+
)
242+
}
243+
214244
/// Get an upper bound of the radius from the given `extents`.
215245
#[inline]
216246
pub fn radius_vec3a(&self, extents: Vec3A) -> f32 {
@@ -363,4 +393,18 @@ mod test {
363393
t1_prime.compute_transform(),
364394
);
365395
}
396+
397+
#[test]
398+
fn scale() {
399+
let test_values = [-42.42, 0., 42.42];
400+
for x in test_values {
401+
for y in test_values {
402+
for z in test_values {
403+
let scale = Vec3::new(x, y, z);
404+
let gt = GlobalTransform::from_scale(scale);
405+
assert_eq!(gt.scale(), gt.to_scale_rotation_translation().0);
406+
}
407+
}
408+
}
409+
}
366410
}

0 commit comments

Comments
 (0)