Skip to content

Commit 6337fb3

Browse files
authored
Improve Rectangle and Cuboid consistency (#11434)
# Objective The `Rectangle` and `Cuboid` primitives currently use different representations: ```rust pub struct Rectangle { /// The half width of the rectangle pub half_width: f32, /// The half height of the rectangle pub half_height: f32, } pub struct Cuboid { /// Half of the width, height and depth of the cuboid pub half_extents: Vec3, } ``` The property names and helpers are also inconsistent. `Cuboid` has `half_extents`, but it also has a method called `from_size`. Most existing code also uses "size" instead of "extents". ## Solution Represent both `Rectangle` and `Cuboid` with `half_size` properties.
1 parent 320ac65 commit 6337fb3

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,20 @@ impl Bounded2d for Triangle2d {
189189

190190
impl Bounded2d for Rectangle {
191191
fn aabb_2d(&self, translation: Vec2, rotation: f32) -> Aabb2d {
192-
let half_size = Vec2::new(self.half_width, self.half_height);
193-
194192
// Compute the AABB of the rotated rectangle by transforming the half-extents
195193
// by an absolute rotation matrix.
196194
let (sin, cos) = rotation.sin_cos();
197195
let abs_rot_mat = Mat2::from_cols_array(&[cos.abs(), sin.abs(), sin.abs(), cos.abs()]);
198-
let half_extents = abs_rot_mat * half_size;
196+
let half_size = abs_rot_mat * self.half_size;
199197

200198
Aabb2d {
201-
min: translation - half_extents,
202-
max: translation + half_extents,
199+
min: translation - half_size,
200+
max: translation + half_size,
203201
}
204202
}
205203

206204
fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle {
207-
let radius = self.half_width.hypot(self.half_height);
205+
let radius = self.half_size.length();
208206
BoundingCircle::new(translation, radius)
209207
}
210208
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,19 @@ impl Bounded3d for Cuboid {
120120
rot_mat.y_axis.abs(),
121121
rot_mat.z_axis.abs(),
122122
);
123-
124-
let half_extents = abs_rot_mat * self.half_extents;
123+
let half_size = abs_rot_mat * self.half_size;
125124

126125
Aabb3d {
127-
min: translation - half_extents,
128-
max: translation + half_extents,
126+
min: translation - half_size,
127+
max: translation + half_size,
129128
}
130129
}
131130

132131
fn bounding_sphere(&self, translation: Vec3, _rotation: Quat) -> BoundingSphere {
133132
BoundingSphere {
134133
center: translation,
135134
sphere: Sphere {
136-
radius: self.half_extents.length(),
135+
radius: self.half_size.length(),
137136
},
138137
}
139138
}

crates/bevy_math/src/primitives/dim2.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,9 @@ impl Triangle2d {
311311
#[doc(alias = "Quad")]
312312
#[derive(Clone, Copy, Debug)]
313313
pub struct Rectangle {
314-
/// The half width of the rectangle
315-
pub half_width: f32,
316-
/// The half height of the rectangle
317-
pub half_height: f32,
314+
/// Half of the width and height of the rectangle
315+
pub half_size: Vec2,
318316
}
319-
impl Primitive2d for Rectangle {}
320317

321318
impl Rectangle {
322319
/// Create a rectangle from a full width and height
@@ -327,8 +324,7 @@ impl Rectangle {
327324
/// Create a rectangle from a given full size
328325
pub fn from_size(size: Vec2) -> Self {
329326
Self {
330-
half_width: size.x / 2.,
331-
half_height: size.y / 2.,
327+
half_size: size / 2.,
332328
}
333329
}
334330
}

crates/bevy_math/src/primitives/dim3.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl BoxedPolyline3d {
222222
#[derive(Clone, Copy, Debug)]
223223
pub struct Cuboid {
224224
/// Half of the width, height and depth of the cuboid
225-
pub half_extents: Vec3,
225+
pub half_size: Vec3,
226226
}
227227
impl Primitive3d for Cuboid {}
228228

@@ -235,7 +235,7 @@ impl Cuboid {
235235
/// Create a cuboid from a given full size
236236
pub fn from_size(size: Vec3) -> Self {
237237
Self {
238-
half_extents: size / 2.,
238+
half_size: size / 2.,
239239
}
240240
}
241241
}

0 commit comments

Comments
 (0)