Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Rectangle and Cuboid consistency #11434

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,20 @@ impl Bounded2d for Triangle2d {

impl Bounded2d for Rectangle {
fn aabb_2d(&self, translation: Vec2, rotation: f32) -> Aabb2d {
let half_size = Vec2::new(self.half_width, self.half_height);

// Compute the AABB of the rotated rectangle by transforming the half-extents
// by an absolute rotation matrix.
let (sin, cos) = rotation.sin_cos();
let abs_rot_mat = Mat2::from_cols_array(&[cos.abs(), sin.abs(), sin.abs(), cos.abs()]);
let half_extents = abs_rot_mat * half_size;
let half_size = abs_rot_mat * self.half_size;

Aabb2d {
min: translation - half_extents,
max: translation + half_extents,
min: translation - half_size,
max: translation + half_size,
}
}

fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle {
let radius = self.half_width.hypot(self.half_height);
let radius = self.half_size.length();
BoundingCircle::new(translation, radius)
}
}
Expand Down
9 changes: 4 additions & 5 deletions crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,19 @@ impl Bounded3d for Cuboid {
rot_mat.y_axis.abs(),
rot_mat.z_axis.abs(),
);

let half_extents = abs_rot_mat * self.half_extents;
let half_size = abs_rot_mat * self.half_size;

Aabb3d {
min: translation - half_extents,
max: translation + half_extents,
min: translation - half_size,
max: translation + half_size,
}
}

fn bounding_sphere(&self, translation: Vec3, _rotation: Quat) -> BoundingSphere {
BoundingSphere {
center: translation,
sphere: Sphere {
radius: self.half_extents.length(),
radius: self.half_size.length(),
},
}
}
Expand Down
10 changes: 3 additions & 7 deletions crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,9 @@ impl Triangle2d {
#[doc(alias = "Quad")]
#[derive(Clone, Copy, Debug)]
pub struct Rectangle {
/// The half width of the rectangle
pub half_width: f32,
/// The half height of the rectangle
pub half_height: f32,
/// Half of the width and height of the rectangle
pub half_size: Vec2,
}
impl Primitive2d for Rectangle {}

impl Rectangle {
/// Create a rectangle from a full width and height
Expand All @@ -327,8 +324,7 @@ impl Rectangle {
/// Create a rectangle from a given full size
pub fn from_size(size: Vec2) -> Self {
Self {
half_width: size.x / 2.,
half_height: size.y / 2.,
half_size: size / 2.,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl BoxedPolyline3d {
#[derive(Clone, Copy, Debug)]
pub struct Cuboid {
/// Half of the width, height and depth of the cuboid
pub half_extents: Vec3,
pub half_size: Vec3,
}
impl Primitive3d for Cuboid {}

Expand All @@ -235,7 +235,7 @@ impl Cuboid {
/// Create a cuboid from a given full size
pub fn from_size(size: Vec3) -> Self {
Self {
half_extents: size / 2.,
half_size: size / 2.,
}
}
}
Expand Down