Skip to content

Commit 2c3d20d

Browse files
authored
Fix rotate_by implementation for Aabb2d (#19015)
# Objective Fixes #18969 ## Solution Also updated `Aabb3d` implementation for consistency. ## Testing Added tests for `Aabb2d` and `Aabb3d` to verify correct rotation behavior for angles greater than 90 degrees.
1 parent e05e74a commit 2c3d20d

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

crates/bevy_math/src/bounding/bounded2d/mod.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,9 @@ impl BoundingVolume for Aabb2d {
243243
/// and consider storing the original AABB and rotating that every time instead.
244244
#[inline(always)]
245245
fn rotate_by(&mut self, rotation: impl Into<Self::Rotation>) {
246-
let rotation: Rot2 = rotation.into();
247-
let abs_rot_mat = Mat2::from_cols(
248-
Vec2::new(rotation.cos, rotation.sin),
249-
Vec2::new(rotation.sin, rotation.cos),
250-
);
251-
let half_size = abs_rot_mat * self.half_size();
252-
*self = Self::new(rotation * self.center(), half_size);
246+
let rot_mat = Mat2::from(rotation.into());
247+
let half_size = rot_mat.abs() * self.half_size();
248+
*self = Self::new(rot_mat * self.center(), half_size);
253249
}
254250
}
255251

@@ -274,6 +270,8 @@ impl IntersectsVolume<BoundingCircle> for Aabb2d {
274270

275271
#[cfg(test)]
276272
mod aabb2d_tests {
273+
use approx::assert_relative_eq;
274+
277275
use super::Aabb2d;
278276
use crate::{
279277
bounding::{BoundingCircle, BoundingVolume, IntersectsVolume},
@@ -394,6 +392,17 @@ mod aabb2d_tests {
394392
assert!(scaled.contains(&a));
395393
}
396394

395+
#[test]
396+
fn rotate() {
397+
let a = Aabb2d {
398+
min: Vec2::new(-2.0, -2.0),
399+
max: Vec2::new(2.0, 2.0),
400+
};
401+
let rotated = a.rotated_by(core::f32::consts::PI);
402+
assert_relative_eq!(rotated.min, a.min);
403+
assert_relative_eq!(rotated.max, a.max);
404+
}
405+
397406
#[test]
398407
fn transform() {
399408
let a = Aabb2d {

crates/bevy_math/src/bounding/bounded3d/mod.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,7 @@ impl BoundingVolume for Aabb3d {
250250
#[inline(always)]
251251
fn rotate_by(&mut self, rotation: impl Into<Self::Rotation>) {
252252
let rot_mat = Mat3::from_quat(rotation.into());
253-
let abs_rot_mat = Mat3::from_cols(
254-
rot_mat.x_axis.abs(),
255-
rot_mat.y_axis.abs(),
256-
rot_mat.z_axis.abs(),
257-
);
258-
let half_size = abs_rot_mat * self.half_size();
253+
let half_size = rot_mat.abs() * self.half_size();
259254
*self = Self::new(rot_mat * self.center(), half_size);
260255
}
261256
}
@@ -279,6 +274,8 @@ impl IntersectsVolume<BoundingSphere> for Aabb3d {
279274

280275
#[cfg(test)]
281276
mod aabb3d_tests {
277+
use approx::assert_relative_eq;
278+
282279
use super::Aabb3d;
283280
use crate::{
284281
bounding::{BoundingSphere, BoundingVolume, IntersectsVolume},
@@ -398,6 +395,19 @@ mod aabb3d_tests {
398395
assert!(scaled.contains(&a));
399396
}
400397

398+
#[test]
399+
fn rotate() {
400+
use core::f32::consts::PI;
401+
let a = Aabb3d {
402+
min: Vec3A::new(-2.0, -2.0, -2.0),
403+
max: Vec3A::new(2.0, 2.0, 2.0),
404+
};
405+
let rotation = Quat::from_euler(glam::EulerRot::XYZ, PI, PI, 0.0);
406+
let rotated = a.rotated_by(rotation);
407+
assert_relative_eq!(rotated.min, a.min);
408+
assert_relative_eq!(rotated.max, a.max);
409+
}
410+
401411
#[test]
402412
fn transform() {
403413
let a = Aabb3d {

0 commit comments

Comments
 (0)