Skip to content

Commit 0a53448

Browse files
committed
Follow-up fixes
1 parent 2eeff84 commit 0a53448

File tree

3 files changed

+121
-107
lines changed

3 files changed

+121
-107
lines changed

godot-core/src/builtin/aabb.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ impl Aabb {
3838
/// Create a new `Aabb` with the first corner at `position` and opposite corner at `end`.
3939
#[inline]
4040
pub fn from_corners(position: Vector3, end: Vector3) -> Self {
41-
Self {
42-
position,
43-
size: position + end,
44-
}
41+
// Cannot use floating point arithmetic in const functions.
42+
Self::new(position, end - position)
4543
}
4644

47-
/// Returns an AABB with equivalent position and size,
48-
/// modified so that the most-negative corner is the origin and the size is positive.
45+
/// Returns an AABB with the same geometry, with most-negative corner as `position` and non-negative `size`.
46+
#[inline]
4947
pub fn abs(&self) -> Self {
5048
Aabb {
5149
position: self.position + self.size.coord_min(Vector3::ZERO),
@@ -141,6 +139,7 @@ impl Aabb {
141139
}
142140

143141
/// Returns true if the AABB has a volume, and false if the AABB is flat, linear, empty, or has a negative size.
142+
#[inline]
144143
pub fn has_volume(&self) -> bool {
145144
self.size.x > 0.0 && self.size.y > 0.0 && self.size.z > 0.0
146145
}
@@ -168,6 +167,7 @@ impl Aabb {
168167
}
169168

170169
/// Returns `true` if this AABB is finite, by calling `@GlobalScope.is_finite` on each component.
170+
#[inline]
171171
pub fn is_finite(&self) -> bool {
172172
self.position.is_finite() && self.size.is_finite()
173173
}
@@ -187,6 +187,7 @@ impl Aabb {
187187
}
188188

189189
/// Returns the normalized longest axis of the AABB.
190+
#[inline]
190191
pub fn longest_axis(&self) -> Vector3 {
191192
match self.longest_axis_index() {
192193
Vector3Axis::X => Vector3::RIGHT,
@@ -196,16 +197,19 @@ impl Aabb {
196197
}
197198

198199
/// Returns the index of the longest axis of the AABB (according to Vector3's AXIS_* constants).
200+
#[inline]
199201
pub fn longest_axis_index(&self) -> Vector3Axis {
200202
self.size.max_axis_index()
201203
}
202204

203205
/// Returns the scalar length of the longest axis of the AABB.
206+
#[inline]
204207
pub fn longest_axis_size(&self) -> real {
205208
self.size.x.max(self.size.y.max(self.size.z))
206209
}
207210

208211
/// Returns the normalized shortest axis of the AABB.
212+
#[inline]
209213
pub fn shortest_axis(&self) -> Vector3 {
210214
match self.shortest_axis_index() {
211215
Vector3Axis::X => Vector3::RIGHT,
@@ -215,16 +219,19 @@ impl Aabb {
215219
}
216220

217221
/// Returns the index of the shortest axis of the AABB (according to Vector3::AXIS* enum).
222+
#[inline]
218223
pub fn shortest_axis_index(&self) -> Vector3Axis {
219224
self.size.min_axis_index()
220225
}
221226

222227
/// Returns the scalar length of the shortest axis of the AABB.
228+
#[inline]
223229
pub fn shortest_axis_size(&self) -> real {
224230
self.size.x.min(self.size.y.min(self.size.z))
225231
}
226232

227233
/// Returns the support point in a given direction. This is useful for collision detection algorithms.
234+
#[inline]
228235
pub fn support(&self, dir: Vector3) -> Vector3 {
229236
let half_extents = self.size * 0.5;
230237
let relative_center_point = self.position + half_extents;
@@ -238,9 +245,12 @@ impl Aabb {
238245
half_extents * signs + relative_center_point
239246
}
240247

241-
/// Returns `true` if the AABB overlaps with `b` (i.e. they have at least one point in common).
248+
/// Checks whether two AABBs have at least one point in common.
242249
///
243-
/// _Godot equivalent: `AABB.intersects(AABB b, bool include_borders = false)`_
250+
/// Also returns `true` if the AABBs only touch each other (share a point/edge/face).
251+
/// See [`intersects_exclude_borders`][Self::intersects_exclude_borders] if you want to return `false` in that case.
252+
///
253+
/// _Godot equivalent: `AABB.intersects(AABB b, bool include_borders = true)`_
244254
#[inline]
245255
pub fn intersects(&self, b: &Aabb) -> bool {
246256
let end = self.end();
@@ -253,9 +263,13 @@ impl Aabb {
253263
&& self.position.z <= end_b.z
254264
}
255265

256-
/// Returns `true` if the AABB overlaps with `b` (i.e. they have at least one inner point in common).
266+
/// Checks whether two AABBs have at least one _inner_ point in common (not on the borders).
257267
///
258-
/// _Godot equivalent: `AABB.intersects(AABB b, bool include_borders = true)`_
268+
/// Returns `false` if the AABBs only touch each other (share a point/edge/face).
269+
/// See [`intersects`][Self::intersects] if you want to return `true` in that case.
270+
///
271+
/// _Godot equivalent: `AABB.intersects(AABB b, bool include_borders = false)`_
272+
#[inline]
259273
pub fn intersects_exclude_borders(&self, &b: &Aabb) -> bool {
260274
let end = self.end();
261275
let end_b = b.end();
@@ -269,6 +283,7 @@ impl Aabb {
269283
}
270284

271285
/// Returns `true` if the AABB is on both sides of a plane.
286+
#[inline]
272287
pub fn intersects_plane(&self, plane: &Plane) -> bool {
273288
// The set of the edges of the AABB.
274289
let points = [
@@ -301,6 +316,7 @@ impl Aabb {
301316
///
302317
/// # Panics
303318
/// If `self.size` is negative.
319+
#[inline]
304320
pub fn intersects_ray(&self, from: Vector3, dir: Vector3) -> bool {
305321
self.assert_nonnegative();
306322

@@ -320,6 +336,7 @@ impl Aabb {
320336
///
321337
/// # Panics
322338
/// If `self.size` is negative.
339+
#[inline]
323340
pub fn intersects_segment(&self, from: Vector3, to: Vector3) -> bool {
324341
self.assert_nonnegative();
325342

godot-core/src/builtin/rect2.rs

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ impl Rect2 {
3535
Self { position, size }
3636
}
3737

38+
/// Create a new `Rect2` with the first corner at `position` and the opposite corner at `end`.
39+
#[inline]
40+
pub fn from_corners(position: Vector2, end: Vector2) -> Self {
41+
// Cannot use floating point arithmetic in const functions.
42+
Self::new(position, end - position)
43+
}
44+
3845
/// Create a new `Rect2` from four reals representing position `(x,y)` and size `(width,height)`.
3946
///
4047
/// _Godot equivalent: `Rect2(float x, float y, float width, float height)`_
@@ -46,6 +53,17 @@ impl Rect2 {
4653
}
4754
}
4855

56+
/// Create a new `Rect2` from a `Rect2i`, using `as` for `i32` to `real` conversions.
57+
///
58+
/// _Godot equivalent: `Rect2(Rect2i from)`_
59+
#[inline]
60+
pub const fn from_rect2i(rect: Rect2i) -> Self {
61+
Self {
62+
position: Vector2::from_vector2i(rect.position),
63+
size: Vector2::from_vector2i(rect.size),
64+
}
65+
}
66+
4967
/// Returns a rectangle with the same geometry, with top-left corner as `position` and non-negative size.
5068
///
5169
/// _Godot equivalent: `Rect2.abs()`_
@@ -180,7 +198,7 @@ impl Rect2 {
180198
/// _Godot equivalent: `Rect2.intersection(Rect2 b)`_
181199
#[inline]
182200
pub fn intersection(&self, b: Self) -> Option<Self> {
183-
if !self.intersects(b, true) {
201+
if !self.intersects(b) {
184202
return None;
185203
}
186204

@@ -194,54 +212,46 @@ impl Rect2 {
194212
Some(rect)
195213
}
196214

197-
/// Returns `true` if the Rect2 overlaps with `b` (i.e. they have at least one point in common).
215+
/// Checks whether two rectangles have at least one point in common.
198216
///
199-
/// If `include_borders` is `true`, they will also be considered overlapping if their borders touch, even without intersection.
217+
/// Also returns `true` if the rects only touch each other (share a point/edge).
218+
/// See [`intersects_exclude_borders`][Self::intersects_exclude_borders] if you want to return `false` in that case.
200219
///
201-
/// _Godot equivalent: `Rect2.intersects(Rect2 b, bool include_borders)`_
220+
/// _Godot equivalent: `Rect2.intersects(Rect2 b, bool include_borders = true)`_
202221
#[inline]
203-
pub fn intersects(&self, b: Self, include_borders: bool) -> bool {
222+
pub fn intersects(&self, b: Self) -> bool {
204223
let end = self.end();
205224
let end_b = b.end();
206225

207-
if include_borders {
208-
self.position.x <= end_b.x
209-
&& end.x >= b.position.x
210-
&& self.position.y <= end_b.y
211-
&& end.y >= b.position.y
212-
} else {
213-
self.position.x < end_b.x
214-
&& end.x > b.position.x
215-
&& self.position.y < end_b.y
216-
&& end.y > b.position.y
217-
}
226+
self.position.x <= end_b.x
227+
&& end.x >= b.position.x
228+
&& self.position.y <= end_b.y
229+
&& end.y >= b.position.y
218230
}
219231

220-
/// Returns `true` if this Rect2 is finite, by calling `@GlobalScope.is_finite` on each component.
232+
/// Checks whether two rectangles have at least one _inner_ point in common (not on the borders).
221233
///
222-
/// _Godot equivalent: `Rect2.is_finite()`_
223-
pub fn is_finite(&self) -> bool {
224-
self.position.is_finite() && self.size.is_finite()
225-
}
226-
227-
/// Create a new `Rect2` from a `Rect2i`, using `as` for `i32` to `real` conversions.
234+
/// Returns `false` if the rects only touch each other (share a point/edge).
235+
/// See [`intersects`][Self::intersects] if you want to return `true` in that case.
228236
///
229-
/// _Godot equivalent: `Rect2(Rect2i from)`_
237+
/// _Godot equivalent: `Rect2.intersects(AABB b, bool include_borders = false)`_
230238
#[inline]
231-
pub const fn from_rect2i(rect: Rect2i) -> Self {
232-
Self {
233-
position: Vector2::from_vector2i(rect.position),
234-
size: Vector2::from_vector2i(rect.size),
235-
}
239+
pub fn intersects_exclude_borders(&self, b: Self) -> bool {
240+
let end = self.end();
241+
let end_b = b.end();
242+
243+
self.position.x < end_b.x
244+
&& end.x > b.position.x
245+
&& self.position.y < end_b.y
246+
&& end.y > b.position.y
236247
}
237248

238-
/// Create a new `Rect2` with the first corner at `position` and the opposite corner at `end`.
249+
/// Returns `true` if this Rect2 is finite, by calling `@GlobalScope.is_finite` on each component.
250+
///
251+
/// _Godot equivalent: `Rect2.is_finite()`_
239252
#[inline]
240-
pub fn from_corners(position: Vector2, end: Vector2) -> Self {
241-
Self {
242-
position,
243-
size: end - position,
244-
}
253+
pub fn is_finite(&self) -> bool {
254+
self.position.is_finite() && self.size.is_finite()
245255
}
246256

247257
/// The end of the `Rect2` calculated as `position + size`.

0 commit comments

Comments
 (0)