Skip to content

Commit

Permalink
Merge pull request #54616 from briansemrau/aabb-area-to-volume
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Nov 5, 2021
2 parents 70c82d9 + dc11e73 commit 7538c05
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion core/math/aabb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "core/string/print_string.h"
#include "core/variant/variant.h"

real_t AABB::get_area() const {
real_t AABB::get_volume() const {
return size.x * size.y * size.z;
}

Expand Down
4 changes: 2 additions & 2 deletions core/math/aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class AABB {
Vector3 position;
Vector3 size;

real_t get_area() const; /// get area
_FORCE_INLINE_ bool has_no_area() const {
real_t get_volume() const;
_FORCE_INLINE_ bool has_no_volume() const {
return (size.x <= 0 || size.y <= 0 || size.z <= 0);
}

Expand Down
4 changes: 2 additions & 2 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1748,8 +1748,8 @@ static void _register_variant_builtin_methods() {

bind_method(AABB, abs, sarray(), varray());
bind_method(AABB, get_center, sarray(), varray());
bind_method(AABB, get_area, sarray(), varray());
bind_method(AABB, has_no_area, sarray(), varray());
bind_method(AABB, get_volume, sarray(), varray());
bind_method(AABB, has_no_volume, sarray(), varray());
bind_method(AABB, has_no_surface, sarray(), varray());
bind_method(AABB, has_point, sarray("point"), varray());
bind_method(AABB, is_equal_approx, sarray("aabb"), varray());
Expand Down
20 changes: 10 additions & 10 deletions doc/classes/AABB.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@
Returns this [AABB] expanded to include a given point.
</description>
</method>
<method name="get_area" qualifiers="const">
<return type="float" />
<description>
Returns the volume of the [AABB].
</description>
</method>
<method name="get_center" qualifiers="const">
<return type="Vector3" />
<description>
Expand Down Expand Up @@ -119,23 +113,29 @@
Returns the support point in a given direction. This is useful for collision detection algorithms.
</description>
</method>
<method name="get_volume" qualifiers="const">
<return type="float" />
<description>
Returns the volume of the [AABB].
</description>
</method>
<method name="grow" qualifiers="const">
<return type="AABB" />
<argument index="0" name="by" type="float" />
<description>
Returns a copy of the [AABB] grown a given amount of units towards all the sides.
</description>
</method>
<method name="has_no_area" qualifiers="const">
<method name="has_no_surface" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if the [AABB] is flat or empty.
Returns [code]true[/code] if the [AABB] is empty.
</description>
</method>
<method name="has_no_surface" qualifiers="const">
<method name="has_no_volume" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if the [AABB] is empty.
Returns [code]true[/code] if the [AABB] is flat or empty.
</description>
</method>
<method name="has_point" qualifiers="const">
Expand Down
2 changes: 1 addition & 1 deletion servers/physics_3d/godot_collision_object_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void GodotCollisionObject3D::_update_shapes() {
s.aabb_cache = shape_aabb;

Vector3 scale = xform.get_basis().get_scale();
s.area_cache = s.shape->get_area() * scale.x * scale.y * scale.z;
s.area_cache = s.shape->get_volume() * scale.x * scale.y * scale.z;

if (s.bpid == 0) {
s.bpid = space->get_broadphase()->create(this, i, shape_aabb, _static);
Expand Down
14 changes: 7 additions & 7 deletions servers/physics_3d/godot_shape_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class GodotShape3D {
FEATURE_CIRCLE,
};

virtual real_t get_area() const { return aabb.get_area(); }
virtual real_t get_volume() const { return aabb.get_volume(); }

_FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
_FORCE_INLINE_ RID get_self() const { return self; }
Expand Down Expand Up @@ -120,7 +120,7 @@ class GodotWorldBoundaryShape3D : public GodotShape3D {
public:
Plane get_plane() const;

virtual real_t get_area() const override { return INFINITY; }
virtual real_t get_volume() const override { return INFINITY; }
virtual PhysicsServer3D::ShapeType get_type() const override { return PhysicsServer3D::SHAPE_WORLD_BOUNDARY; }
virtual void project_range(const Vector3 &p_normal, const Transform3D &p_transform, real_t &r_min, real_t &r_max) const override;
virtual Vector3 get_support(const Vector3 &p_normal) const override;
Expand All @@ -147,7 +147,7 @@ class GodotSeparationRayShape3D : public GodotShape3D {
real_t get_length() const;
bool get_slide_on_slope() const;

virtual real_t get_area() const override { return 0.0; }
virtual real_t get_volume() const override { return 0.0; }
virtual PhysicsServer3D::ShapeType get_type() const override { return PhysicsServer3D::SHAPE_SEPARATION_RAY; }
virtual void project_range(const Vector3 &p_normal, const Transform3D &p_transform, real_t &r_min, real_t &r_max) const override;
virtual Vector3 get_support(const Vector3 &p_normal) const override;
Expand All @@ -173,7 +173,7 @@ class GodotSphereShape3D : public GodotShape3D {
public:
real_t get_radius() const;

virtual real_t get_area() const override { return 4.0 / 3.0 * Math_PI * radius * radius * radius; }
virtual real_t get_volume() const override { return 4.0 / 3.0 * Math_PI * radius * radius * radius; }

virtual PhysicsServer3D::ShapeType get_type() const override { return PhysicsServer3D::SHAPE_SPHERE; }

Expand All @@ -198,7 +198,7 @@ class GodotBoxShape3D : public GodotShape3D {

public:
_FORCE_INLINE_ Vector3 get_half_extents() const { return half_extents; }
virtual real_t get_area() const override { return 8 * half_extents.x * half_extents.y * half_extents.z; }
virtual real_t get_volume() const override { return 8 * half_extents.x * half_extents.y * half_extents.z; }

virtual PhysicsServer3D::ShapeType get_type() const override { return PhysicsServer3D::SHAPE_BOX; }

Expand Down Expand Up @@ -227,7 +227,7 @@ class GodotCapsuleShape3D : public GodotShape3D {
_FORCE_INLINE_ real_t get_height() const { return height; }
_FORCE_INLINE_ real_t get_radius() const { return radius; }

virtual real_t get_area() const override { return 4.0 / 3.0 * Math_PI * radius * radius * radius + (height - radius * 2.0) * Math_PI * radius * radius; }
virtual real_t get_volume() const override { return 4.0 / 3.0 * Math_PI * radius * radius * radius + (height - radius * 2.0) * Math_PI * radius * radius; }

virtual PhysicsServer3D::ShapeType get_type() const override { return PhysicsServer3D::SHAPE_CAPSULE; }

Expand Down Expand Up @@ -256,7 +256,7 @@ class GodotCylinderShape3D : public GodotShape3D {
_FORCE_INLINE_ real_t get_height() const { return height; }
_FORCE_INLINE_ real_t get_radius() const { return radius; }

virtual real_t get_area() const override { return height * Math_PI * radius * radius; }
virtual real_t get_volume() const override { return height * Math_PI * radius * radius; }

virtual PhysicsServer3D::ShapeType get_type() const override { return PhysicsServer3D::SHAPE_CYLINDER; }

Expand Down
30 changes: 15 additions & 15 deletions tests/test_aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,38 +90,38 @@ TEST_CASE("[AABB] Basic setters") {
"set_size() should result in the expected AABB.");
}

TEST_CASE("[AABB] Area getters") {
TEST_CASE("[AABB] Volume getters") {
AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
CHECK_MESSAGE(
Math::is_equal_approx(aabb.get_area(), 120),
"get_area() should return the expected value with positive size.");
Math::is_equal_approx(aabb.get_volume(), 120),
"get_volume() should return the expected value with positive size.");
CHECK_MESSAGE(
!aabb.has_no_area(),
"Non-empty volumetric AABB should have an area.");
!aabb.has_no_volume(),
"Non-empty volumetric AABB should have a volume.");

aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(-4, 5, 6));
CHECK_MESSAGE(
Math::is_equal_approx(aabb.get_area(), -120),
"get_area() should return the expected value with negative size (1 component).");
Math::is_equal_approx(aabb.get_volume(), -120),
"get_volume() should return the expected value with negative size (1 component).");

aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(-4, -5, 6));
CHECK_MESSAGE(
Math::is_equal_approx(aabb.get_area(), 120),
"get_area() should return the expected value with negative size (2 components).");
Math::is_equal_approx(aabb.get_volume(), 120),
"get_volume() should return the expected value with negative size (2 components).");

aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(-4, -5, -6));
CHECK_MESSAGE(
Math::is_equal_approx(aabb.get_area(), -120),
"get_area() should return the expected value with negative size (3 components).");
Math::is_equal_approx(aabb.get_volume(), -120),
"get_volume() should return the expected value with negative size (3 components).");

aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 0, 6));
CHECK_MESSAGE(
aabb.has_no_area(),
"Non-empty flat AABB should not have an area.");
aabb.has_no_volume(),
"Non-empty flat AABB should not have a volume.");

CHECK_MESSAGE(
AABB().has_no_area(),
"Empty AABB should not have an area.");
AABB().has_no_volume(),
"Empty AABB should not have a volume.");
}

TEST_CASE("[AABB] Surface getters") {
Expand Down

0 comments on commit 7538c05

Please sign in to comment.