Skip to content

Commit

Permalink
Merge pull request #65591 from MewPurPur/area-overlapping-function
Browse files Browse the repository at this point in the history
Implement `Area[2D/3D].has_overlapping_[bodies/areas]`
  • Loading branch information
akien-mga committed Sep 13, 2022
2 parents abdaa6d + ed4fe1e commit 3a82a13
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 2 deletions.
16 changes: 15 additions & 1 deletion doc/classes/Area2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,24 @@
<method name="get_overlapping_bodies" qualifiers="const">
<return type="Node2D[]" />
<description>
Returns a list of intersecting [PhysicsBody2D]s. The overlapping body's [member CollisionObject2D.collision_layer] must be part of this area's [member CollisionObject2D.collision_mask] in order to be detected.
Returns a list of intersecting [PhysicsBody2D]s and [TileMap]s. The overlapping body's [member CollisionObject2D.collision_layer] must be part of this area's [member CollisionObject2D.collision_mask] in order to be detected.
For performance reasons (collisions are all processed at the same time) this list is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.
</description>
</method>
<method name="has_overlapping_areas" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if intersecting any [Area2D]s, otherwise returns [code]false[/code]. The overlapping area's [member CollisionObject2D.collision_layer] must be part of this area's [member CollisionObject2D.collision_mask] in order to be detected.
For performance reasons (collisions are all processed at the same time) the list of overlapping areas is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.
</description>
</method>
<method name="has_overlapping_bodies" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if intersecting any [PhysicsBody2D]s or [TileMap]s, otherwise returns [code]false[/code]. The overlapping body's [member CollisionObject2D.collision_layer] must be part of this area's [member CollisionObject2D.collision_mask] in order to be detected.
For performance reasons (collisions are all processed at the same time) the list of overlapping bodies is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.
</description>
</method>
<method name="overlaps_area" qualifiers="const">
<return type="bool" />
<param index="0" name="area" type="Node" />
Expand Down
16 changes: 15 additions & 1 deletion doc/classes/Area3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,24 @@
<method name="get_overlapping_bodies" qualifiers="const">
<return type="Node3D[]" />
<description>
Returns a list of intersecting [PhysicsBody3D]s. The overlapping body's [member CollisionObject3D.collision_layer] must be part of this area's [member CollisionObject3D.collision_mask] in order to be detected.
Returns a list of intersecting [PhysicsBody3D]s and [GridMap]s. The overlapping body's [member CollisionObject3D.collision_layer] must be part of this area's [member CollisionObject3D.collision_mask] in order to be detected.
For performance reasons (collisions are all processed at the same time) this list is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.
</description>
</method>
<method name="has_overlapping_areas" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if intersecting any [Area3D]s, otherwise returns [code]false[/code]. The overlapping area's [member CollisionObject3D.collision_layer] must be part of this area's [member CollisionObject3D.collision_mask] in order to be detected.
For performance reasons (collisions are all processed at the same time) the list of overlapping areas is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.
</description>
</method>
<method name="has_overlapping_bodies" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if intersecting any [PhysicsBody3D]s or [GridMap]s, otherwise returns [code]false[/code]. The overlapping body's [member CollisionObject3D.collision_layer] must be part of this area's [member CollisionObject3D.collision_mask] in order to be detected.
For performance reasons (collisions are all processed at the same time) the list of overlapping bodies is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.
</description>
</method>
<method name="overlaps_area" qualifiers="const">
<return type="bool" />
<param index="0" name="area" type="Node" />
Expand Down
13 changes: 13 additions & 0 deletions scene/2d/area_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,16 @@ TypedArray<Area2D> Area2D::get_overlapping_areas() const {
return ret;
}

bool Area2D::has_overlapping_bodies() const {
ERR_FAIL_COND_V_MSG(!monitoring, false, "Can't find overlapping bodies when monitoring is off.");
return !body_map.is_empty();
}

bool Area2D::has_overlapping_areas() const {
ERR_FAIL_COND_V_MSG(!monitoring, false, "Can't find overlapping areas when monitoring is off.");
return !area_map.is_empty();
}

bool Area2D::overlaps_area(Node *p_area) const {
ERR_FAIL_NULL_V(p_area, false);
HashMap<ObjectID, AreaState>::ConstIterator E = area_map.find(p_area->get_instance_id());
Expand Down Expand Up @@ -578,6 +588,9 @@ void Area2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_overlapping_bodies"), &Area2D::get_overlapping_bodies);
ClassDB::bind_method(D_METHOD("get_overlapping_areas"), &Area2D::get_overlapping_areas);

ClassDB::bind_method(D_METHOD("has_overlapping_bodies"), &Area2D::has_overlapping_bodies);
ClassDB::bind_method(D_METHOD("has_overlapping_areas"), &Area2D::has_overlapping_areas);

ClassDB::bind_method(D_METHOD("overlaps_body", "body"), &Area2D::overlaps_body);
ClassDB::bind_method(D_METHOD("overlaps_area", "area"), &Area2D::overlaps_area);

Expand Down
3 changes: 3 additions & 0 deletions scene/2d/area_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ class Area2D : public CollisionObject2D {
TypedArray<Node2D> get_overlapping_bodies() const; //function for script
TypedArray<Area2D> get_overlapping_areas() const; //function for script

bool has_overlapping_bodies() const;
bool has_overlapping_areas() const;

bool overlaps_area(Node *p_area) const;
bool overlaps_body(Node *p_body) const;

Expand Down
13 changes: 13 additions & 0 deletions scene/3d/area_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,11 @@ TypedArray<Node3D> Area3D::get_overlapping_bodies() const {
return ret;
}

bool Area3D::has_overlapping_bodies() const {
ERR_FAIL_COND_V_MSG(!monitoring, false, "Can't find overlapping bodies when monitoring is off.");
return !body_map.is_empty();
}

void Area3D::set_monitorable(bool p_enable) {
ERR_FAIL_COND_MSG(locked || (is_inside_tree() && PhysicsServer3D::get_singleton()->is_flushing_queries()), "Function blocked during in/out signal. Use set_deferred(\"monitorable\", true/false).");

Expand Down Expand Up @@ -521,6 +526,11 @@ TypedArray<Area3D> Area3D::get_overlapping_areas() const {
return ret;
}

bool Area3D::has_overlapping_areas() const {
ERR_FAIL_COND_V_MSG(!monitoring, false, "Can't find overlapping areas when monitoring is off.");
return !area_map.is_empty();
}

bool Area3D::overlaps_area(Node *p_area) const {
ERR_FAIL_NULL_V(p_area, false);
HashMap<ObjectID, AreaState>::ConstIterator E = area_map.find(p_area->get_instance_id());
Expand Down Expand Up @@ -686,6 +696,9 @@ void Area3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_overlapping_bodies"), &Area3D::get_overlapping_bodies);
ClassDB::bind_method(D_METHOD("get_overlapping_areas"), &Area3D::get_overlapping_areas);

ClassDB::bind_method(D_METHOD("has_overlapping_bodies"), &Area3D::has_overlapping_bodies);
ClassDB::bind_method(D_METHOD("has_overlapping_areas"), &Area3D::has_overlapping_areas);

ClassDB::bind_method(D_METHOD("overlaps_body", "body"), &Area3D::overlaps_body);
ClassDB::bind_method(D_METHOD("overlaps_area", "area"), &Area3D::overlaps_area);

Expand Down
3 changes: 3 additions & 0 deletions scene/3d/area_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ class Area3D : public CollisionObject3D {
TypedArray<Node3D> get_overlapping_bodies() const;
TypedArray<Area3D> get_overlapping_areas() const; //function for script

bool has_overlapping_bodies() const;
bool has_overlapping_areas() const;

bool overlaps_area(Node *p_area) const;
bool overlaps_body(Node *p_body) const;

Expand Down

0 comments on commit 3a82a13

Please sign in to comment.