Skip to content

Commit df42396

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

File tree

3 files changed

+119
-138
lines changed

3 files changed

+119
-138
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: 43 additions & 66 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,9 +53,18 @@ impl Rect2 {
4653
}
4754
}
4855

49-
/// Returns a rectangle with the same geometry, with top-left corner as `position` and non-negative size.
56+
/// Create a new `Rect2` from a `Rect2i`, using `as` for `i32` to `real` conversions.
5057
///
51-
/// _Godot equivalent: `Rect2.abs()`_
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+
67+
/// Returns a rectangle with the same geometry, with top-left corner as `position` and non-negative size.
5268
#[inline]
5369
pub fn abs(&self) -> Self {
5470
Self {
@@ -58,8 +74,6 @@ impl Rect2 {
5874
}
5975

6076
/// Whether `self` covers at least the entire area of `b` (and possibly more).
61-
///
62-
/// _Godot equivalent: `Rect2.encloses(Rect2 b)`_
6377
#[inline]
6478
pub fn encloses(&self, b: Rect2) -> bool {
6579
let end = self.end();
@@ -75,8 +89,6 @@ impl Rect2 {
7589
///
7690
/// Note: This method is not reliable for `Rect2` with a negative size. Use [`abs`][Self::abs]
7791
/// to get a positive sized equivalent rectangle for expanding.
78-
///
79-
/// _Godot equivalent: `Rect2.expand(Vector2 to)`_
8092
#[inline]
8193
pub fn expand(&self, to: Vector2) -> Self {
8294
self.merge(Rect2::new(to, Vector2::ZERO))
@@ -86,8 +98,6 @@ impl Rect2 {
8698
///
8799
/// Note: This method is not reliable for `Rect2` with a negative size. Use [`abs`][Self::abs]
88100
/// to get a positive sized equivalent rectangle for merging.
89-
///
90-
/// _Godot equivalent: `Rect2.merge(Rect2 b)`_
91101
#[inline]
92102
pub fn merge(&self, b: Self) -> Self {
93103
let position = self.position.coord_min(b.position);
@@ -97,26 +107,18 @@ impl Rect2 {
97107
}
98108

99109
/// Returns the area of the rectangle.
100-
///
101-
/// _Godot equivalent: `Rect2.get_area()`_
102-
#[doc(alias = "get_area")]
103110
#[inline]
104111
pub fn area(&self) -> real {
105112
self.size.x * self.size.y
106113
}
107114

108115
/// Returns the center of the Rect2, which is equal to `position + (size / 2)`.
109-
///
110-
/// _Godot equivalent: `Rect2.get_center()`_
111-
#[doc(alias = "get_center")]
112116
#[inline]
113117
pub fn center(&self) -> Vector2 {
114118
self.position + (self.size / 2.0)
115119
}
116120

117121
/// Returns a copy of the Rect2 grown by the specified `amount` on all sides.
118-
///
119-
/// _Godot equivalent: `Rect2.grow(float amount)`_
120122
#[inline]
121123
#[must_use]
122124
pub fn grow(&self, amount: real) -> Self {
@@ -127,8 +129,6 @@ impl Rect2 {
127129
}
128130

129131
/// Returns a copy of the Rect2 grown by the specified amount on each side individually.
130-
///
131-
/// _Godot equivalent: `Rect2.grow_individual(float left, float top, float right, float bottom)`_
132132
#[inline]
133133
pub fn grow_individual(&self, left: real, top: real, right: real, bottom: real) -> Self {
134134
Self::from_components(
@@ -143,8 +143,6 @@ impl Rect2 {
143143
///
144144
/// `amount` may be negative, but care must be taken: If the resulting `size` has
145145
/// negative components the computation may be incorrect.
146-
///
147-
/// _Godot equivalent: `Rect2.grow_side(int side, float amount)`_
148146
#[inline]
149147
pub fn grow_side(&self, side: RectSide, amount: real) -> Self {
150148
match side {
@@ -156,8 +154,6 @@ impl Rect2 {
156154
}
157155

158156
/// Returns `true` if the Rect2 has area, and `false` if the Rect2 is linear, empty, or has a negative size. See also `get_area`.
159-
///
160-
/// _Godot equivalent: `Rect2.has_area()`_
161157
#[inline]
162158
pub fn has_area(&self) -> bool {
163159
self.size.x > 0.0 && self.size.y > 0.0
@@ -166,8 +162,6 @@ impl Rect2 {
166162
/// Returns `true` if the Rect2 contains a point. By convention, the right and bottom edges of the Rect2 are considered exclusive, so points on these edges are not included.
167163
///
168164
/// Note: This method is not reliable for Rect2 with a negative size. Use `abs` to get a positive sized equivalent rectangle to check for contained points.
169-
///
170-
/// _Godot equivalent: `Rect2.has_area()`_
171165
#[inline]
172166
pub fn has_point(&self, point: Vector2) -> bool {
173167
let point = point - self.position;
@@ -176,11 +170,9 @@ impl Rect2 {
176170
}
177171

178172
/// Returns the intersection of this Rect2 and `b`. If the rectangles do not intersect, an empty Rect2 is returned.
179-
///
180-
/// _Godot equivalent: `Rect2.intersection(Rect2 b)`_
181173
#[inline]
182174
pub fn intersection(&self, b: Self) -> Option<Self> {
183-
if !self.intersects(b, true) {
175+
if !self.intersects(b) {
184176
return None;
185177
}
186178

@@ -194,68 +186,53 @@ impl Rect2 {
194186
Some(rect)
195187
}
196188

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

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-
}
200+
self.position.x <= end_b.x
201+
&& end.x >= b.position.x
202+
&& self.position.y <= end_b.y
203+
&& end.y >= b.position.y
218204
}
219205

220-
/// Returns `true` if this Rect2 is finite, by calling `@GlobalScope.is_finite` on each component.
206+
/// Checks whether two rectangles have at least one _inner_ point in common (not on the borders).
221207
///
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.
208+
/// Returns `false` if the rects only touch each other (share a point/edge).
209+
/// See [`intersects`][Self::intersects] if you want to return `true` in that case.
228210
///
229-
/// _Godot equivalent: `Rect2(Rect2i from)`_
211+
/// _Godot equivalent: `Rect2.intersects(AABB b, bool include_borders = false)`_
230212
#[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-
}
213+
pub fn intersects_exclude_borders(&self, b: Self) -> bool {
214+
let end = self.end();
215+
let end_b = b.end();
216+
217+
self.position.x < end_b.x
218+
&& end.x > b.position.x
219+
&& self.position.y < end_b.y
220+
&& end.y > b.position.y
236221
}
237222

238-
/// Create a new `Rect2` with the first corner at `position` and the opposite corner at `end`.
223+
/// Returns `true` if this Rect2 is finite, by calling `@GlobalScope.is_finite` on each component.
239224
#[inline]
240-
pub fn from_corners(position: Vector2, end: Vector2) -> Self {
241-
Self {
242-
position,
243-
size: end - position,
244-
}
225+
pub fn is_finite(&self) -> bool {
226+
self.position.is_finite() && self.size.is_finite()
245227
}
246228

247229
/// The end of the `Rect2` calculated as `position + size`.
248-
///
249-
/// _Godot equivalent: `Rect2.size` property_
250-
#[doc(alias = "size")]
251230
#[inline]
252231
pub fn end(&self) -> Vector2 {
253232
self.position + self.size
254233
}
255234

256235
/// Set size based on desired end-point.
257-
///
258-
/// _Godot equivalent: `Rect2.size` property_
259236
#[inline]
260237
pub fn set_end(&mut self, end: Vector2) {
261238
self.size = end - self.position

0 commit comments

Comments
 (0)