Skip to content

Commit 7db24a9

Browse files
committed
Simplify and fix Rect2/AABB get_support function
1 parent da5f398 commit 7db24a9

File tree

11 files changed

+97
-35
lines changed

11 files changed

+97
-35
lines changed

core/math/aabb.h

+13-10
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct [[nodiscard]] AABB {
8585
bool intersects_plane(const Plane &p_plane) const;
8686

8787
_FORCE_INLINE_ bool has_point(const Vector3 &p_point) const;
88-
_FORCE_INLINE_ Vector3 get_support(const Vector3 &p_normal) const;
88+
_FORCE_INLINE_ Vector3 get_support(const Vector3 &p_direction) const;
8989

9090
Vector3 get_longest_axis() const;
9191
int get_longest_axis_index() const;
@@ -212,15 +212,18 @@ inline bool AABB::encloses(const AABB &p_aabb) const {
212212
(src_max.z >= dst_max.z));
213213
}
214214

215-
Vector3 AABB::get_support(const Vector3 &p_normal) const {
216-
Vector3 half_extents = size * 0.5f;
217-
Vector3 ofs = position + half_extents;
218-
219-
return Vector3(
220-
(p_normal.x > 0) ? half_extents.x : -half_extents.x,
221-
(p_normal.y > 0) ? half_extents.y : -half_extents.y,
222-
(p_normal.z > 0) ? half_extents.z : -half_extents.z) +
223-
ofs;
215+
Vector3 AABB::get_support(const Vector3 &p_direction) const {
216+
Vector3 support = position;
217+
if (p_direction.x > 0.0f) {
218+
support.x += size.x;
219+
}
220+
if (p_direction.y > 0.0f) {
221+
support.y += size.y;
222+
}
223+
if (p_direction.z > 0.0f) {
224+
support.z += size.z;
225+
}
226+
return support;
224227
}
225228

226229
Vector3 AABB::get_endpoint(int p_point) const {

core/math/rect2.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,15 @@ struct [[nodiscard]] Rect2 {
285285
return Rect2(position.round(), size.round());
286286
}
287287

288-
Vector2 get_support(const Vector2 &p_normal) const {
289-
Vector2 half_extents = size * 0.5f;
290-
Vector2 ofs = position + half_extents;
291-
return Vector2(
292-
(p_normal.x > 0) ? -half_extents.x : half_extents.x,
293-
(p_normal.y > 0) ? -half_extents.y : half_extents.y) +
294-
ofs;
288+
Vector2 get_support(const Vector2 &p_direction) const {
289+
Vector2 support = position;
290+
if (p_direction.x > 0.0f) {
291+
support.x += size.x;
292+
}
293+
if (p_direction.y > 0.0f) {
294+
support.y += size.y;
295+
}
296+
return support;
295297
}
296298

297299
_FORCE_INLINE_ bool intersects_filled_polygon(const Vector2 *p_points, int p_point_count) const {

core/variant/variant_call.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,7 @@ static void _register_variant_builtin_methods_math() {
18491849
bind_method(Rect2, intersection, sarray("b"), varray());
18501850
bind_method(Rect2, merge, sarray("b"), varray());
18511851
bind_method(Rect2, expand, sarray("to"), varray());
1852+
bind_method(Rect2, get_support, sarray("direction"), varray());
18521853
bind_method(Rect2, grow, sarray("amount"), varray());
18531854
bind_methodv(Rect2, grow_side, &Rect2::grow_side_bind, sarray("side", "amount"), varray());
18541855
bind_method(Rect2, grow_individual, sarray("left", "top", "right", "bottom"), varray());
@@ -2185,7 +2186,7 @@ static void _register_variant_builtin_methods_misc() {
21852186
bind_method(AABB, merge, sarray("with"), varray());
21862187
bind_method(AABB, expand, sarray("to_point"), varray());
21872188
bind_method(AABB, grow, sarray("by"), varray());
2188-
bind_method(AABB, get_support, sarray("dir"), varray());
2189+
bind_method(AABB, get_support, sarray("direction"), varray());
21892190
bind_method(AABB, get_longest_axis, sarray(), varray());
21902191
bind_method(AABB, get_longest_axis_index, sarray(), varray());
21912192
bind_method(AABB, get_longest_axis_size, sarray(), varray());

doc/classes/AABB.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
</method>
207207
<method name="get_support" qualifiers="const">
208208
<return type="Vector3" />
209-
<param index="0" name="dir" type="Vector3" />
209+
<param index="0" name="direction" type="Vector3" />
210210
<description>
211211
Returns the vertex's position of this bounding box that's the farthest in the given direction. This point is commonly known as the support point in collision detection algorithms.
212212
</description>

doc/classes/Rect2.xml

+7
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@
112112
Returns the center point of the rectangle. This is the same as [code]position + (size / 2.0)[/code].
113113
</description>
114114
</method>
115+
<method name="get_support" qualifiers="const">
116+
<return type="Vector2" />
117+
<param index="0" name="direction" type="Vector2" />
118+
<description>
119+
Returns the vertex's position of this rect that's the farthest in the given direction. This point is commonly known as the support point in collision detection algorithms.
120+
</description>
121+
</method>
115122
<method name="grow" qualifiers="const">
116123
<return type="Rect2" />
117124
<param index="0" name="amount" type="float" />

drivers/gles3/rasterizer_canvas_gles3.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ void RasterizerCanvasGLES3::light_update_directional_shadow(RID p_rid, int p_sha
17341734

17351735
Vector2 center = p_clip_rect.get_center();
17361736

1737-
float to_edge_distance = ABS(light_dir.dot(p_clip_rect.get_support(light_dir)) - light_dir.dot(center));
1737+
float to_edge_distance = ABS(light_dir.dot(p_clip_rect.get_support(-light_dir)) - light_dir.dot(center));
17381738

17391739
Vector2 from_pos = center - light_dir * (to_edge_distance + p_cull_distance);
17401740
float distance = to_edge_distance * 2.0 + p_cull_distance;

modules/mono/glue/GodotSharp/GodotSharp/Core/Aabb.cs

+14-7
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,20 @@ public readonly real_t GetShortestAxisSize()
314314
/// <returns>A vector representing the support.</returns>
315315
public readonly Vector3 GetSupport(Vector3 dir)
316316
{
317-
Vector3 halfExtents = _size * 0.5f;
318-
Vector3 ofs = _position + halfExtents;
319-
320-
return ofs + new Vector3(
321-
dir.X > 0f ? halfExtents.X : -halfExtents.X,
322-
dir.Y > 0f ? halfExtents.Y : -halfExtents.Y,
323-
dir.Z > 0f ? halfExtents.Z : -halfExtents.Z);
317+
Vector3 support = _position;
318+
if (dir.X > 0.0f)
319+
{
320+
support.X += _size.X;
321+
}
322+
if (dir.Y > 0.0f)
323+
{
324+
support.Y += _size.Y;
325+
}
326+
if (dir.Z > 0.0f)
327+
{
328+
support.Z += _size.Z;
329+
}
330+
return support;
324331
}
325332

326333
/// <summary>

modules/mono/glue/GodotSharp/GodotSharp/Core/Rect2.cs

+20
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,26 @@ public readonly Vector2 GetCenter()
171171
return _position + (_size * 0.5f);
172172
}
173173

174+
/// <summary>
175+
/// Returns the support point in a given direction.
176+
/// This is useful for collision detection algorithms.
177+
/// </summary>
178+
/// <param name="direction">The direction to find support for.</param>
179+
/// <returns>A vector representing the support.</returns>
180+
public readonly Vector2 GetSupport(Vector2 direction)
181+
{
182+
Vector2 support = _position;
183+
if (direction.X > 0.0f)
184+
{
185+
support.X += _size.X;
186+
}
187+
if (direction.Y > 0.0f)
188+
{
189+
support.Y += _size.Y;
190+
}
191+
return support;
192+
}
193+
174194
/// <summary>
175195
/// Returns a copy of the <see cref="Rect2"/> grown by the specified amount
176196
/// on all sides.

servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ void RendererCanvasRenderRD::light_update_directional_shadow(RID p_rid, int p_sh
18231823

18241824
Vector2 center = p_clip_rect.get_center();
18251825

1826-
float to_edge_distance = ABS(light_dir.dot(p_clip_rect.get_support(light_dir)) - light_dir.dot(center));
1826+
float to_edge_distance = ABS(light_dir.dot(p_clip_rect.get_support(-light_dir)) - light_dir.dot(center));
18271827

18281828
Vector2 from_pos = center - light_dir * (to_edge_distance + p_cull_distance);
18291829
float distance = to_edge_distance * 2.0 + p_cull_distance;

tests/core/math/test_aabb.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -377,23 +377,23 @@ TEST_CASE("[AABB] Get longest/shortest axis") {
377377
TEST_CASE("[AABB] Get support") {
378378
const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
379379
CHECK_MESSAGE(
380-
aabb.get_support(Vector3(1, 0, 0)).is_equal_approx(Vector3(2.5, 2, -2.5)),
380+
aabb.get_support(Vector3(1, 0, 0)) == Vector3(2.5, 2, -2.5),
381381
"get_support() should return the expected value.");
382382
CHECK_MESSAGE(
383-
aabb.get_support(Vector3(0.5, 1, 0)).is_equal_approx(Vector3(2.5, 7, -2.5)),
383+
aabb.get_support(Vector3(0.5, 1, 1)) == Vector3(2.5, 7, 3.5),
384384
"get_support() should return the expected value.");
385385
CHECK_MESSAGE(
386-
aabb.get_support(Vector3(0.5, 1, -400)).is_equal_approx(Vector3(2.5, 7, -2.5)),
386+
aabb.get_support(Vector3(0.5, 1, -400)) == Vector3(2.5, 7, -2.5),
387387
"get_support() should return the expected value.");
388388
CHECK_MESSAGE(
389-
aabb.get_support(Vector3(0, -1, 0)).is_equal_approx(Vector3(-1.5, 2, -2.5)),
389+
aabb.get_support(Vector3(0, -1, 0)) == Vector3(-1.5, 2, -2.5),
390390
"get_support() should return the expected value.");
391391
CHECK_MESSAGE(
392-
aabb.get_support(Vector3(0, -0.1, 0)).is_equal_approx(Vector3(-1.5, 2, -2.5)),
392+
aabb.get_support(Vector3(0, -0.1, 0)) == Vector3(-1.5, 2, -2.5),
393393
"get_support() should return the expected value.");
394394
CHECK_MESSAGE(
395-
aabb.get_support(Vector3()).is_equal_approx(Vector3(-1.5, 2, -2.5)),
396-
"get_support() should return the expected value with a null vector.");
395+
aabb.get_support(Vector3()) == Vector3(-1.5, 2, -2.5),
396+
"get_support() should return the AABB position when given a zero vector.");
397397
}
398398

399399
TEST_CASE("[AABB] Grow") {

tests/core/math/test_rect2.h

+22
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,28 @@ TEST_CASE("[Rect2] Expanding") {
180180
"expand() with non-contained Vector2 should return the expected result.");
181181
}
182182

183+
TEST_CASE("[Rect2] Get support") {
184+
const Rect2 rect = Rect2(Vector2(-1.5, 2), Vector2(4, 5));
185+
CHECK_MESSAGE(
186+
rect.get_support(Vector2(1, 0)) == Vector2(2.5, 2),
187+
"get_support() should return the expected value.");
188+
CHECK_MESSAGE(
189+
rect.get_support(Vector2(0.5, 1)) == Vector2(2.5, 7),
190+
"get_support() should return the expected value.");
191+
CHECK_MESSAGE(
192+
rect.get_support(Vector2(0.5, 1)) == Vector2(2.5, 7),
193+
"get_support() should return the expected value.");
194+
CHECK_MESSAGE(
195+
rect.get_support(Vector2(0, -1)) == Vector2(-1.5, 2),
196+
"get_support() should return the expected value.");
197+
CHECK_MESSAGE(
198+
rect.get_support(Vector2(0, -0.1)) == Vector2(-1.5, 2),
199+
"get_support() should return the expected value.");
200+
CHECK_MESSAGE(
201+
rect.get_support(Vector2()) == Vector2(-1.5, 2),
202+
"get_support() should return the Rect2 position when given a zero vector.");
203+
}
204+
183205
TEST_CASE("[Rect2] Growing") {
184206
CHECK_MESSAGE(
185207
Rect2(0, 100, 1280, 720).grow(100).is_equal_approx(Rect2(-100, 0, 1480, 920)),

0 commit comments

Comments
 (0)