Skip to content

Commit

Permalink
Fix floor snapping for CSG shapes with collision
Browse files Browse the repository at this point in the history
Add get_collision_rid function to CSGShape3D.

Add additional checks when gathering nodes to exclude from raycast made
when using the Snap Object to Floor feature of the 3D editor so that it
finds and includes CSGShape3Ds that have collision enabled.
  • Loading branch information
stevenjt committed Mar 24, 2024
1 parent fe01776 commit cad6e68
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
#include "scene/resources/packed_scene.h"
#include "scene/resources/surface_tool.h"

#include "modules/modules_enabled.gen.h" // For csg.
#ifdef MODULE_CSG_ENABLED
#include "modules/csg/csg_shape.h"
#endif

constexpr real_t DISTANCE_DEFAULT = 4;

constexpr real_t GIZMO_ARROW_SIZE = 0.35;
Expand Down Expand Up @@ -7435,6 +7440,18 @@ HashSet<RID> _get_physics_bodies_rid(Node *node) {
for (const PhysicsBody3D *I : child_nodes) {
rids.insert(I->get_rid());
}
#ifdef MODULE_CSG_ENABLED
CSGShape3D *csg_shape = Node::cast_to<CSGShape3D>(node);
if (csg_shape && csg_shape->is_using_collision()) {
rids.insert(csg_shape->get_collision_rid());
}
HashSet<CSGShape3D *> csg_child_nodes = _get_child_nodes<CSGShape3D>(node);
for (const CSGShape3D *I : csg_child_nodes) {
if (I->is_using_collision()) {
rids.insert(I->get_collision_rid());
}
}
#endif

return rids;
}
Expand Down
5 changes: 5 additions & 0 deletions modules/csg/csg_shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ bool CSGShape3D::is_using_collision() const {
return use_collision;
}

RID CSGShape3D::get_collision_rid() const {
return root_collision_instance;
}

void CSGShape3D::set_collision_layer(uint32_t p_layer) {
collision_layer = p_layer;
if (root_collision_instance.is_valid()) {
Expand Down Expand Up @@ -678,6 +682,7 @@ void CSGShape3D::_bind_methods() {

ClassDB::bind_method(D_METHOD("set_use_collision", "operation"), &CSGShape3D::set_use_collision);
ClassDB::bind_method(D_METHOD("is_using_collision"), &CSGShape3D::is_using_collision);
ClassDB::bind_method(D_METHOD("get_collision_rid"), &CSGShape3D::get_collision_rid);

ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CSGShape3D::set_collision_layer);
ClassDB::bind_method(D_METHOD("get_collision_layer"), &CSGShape3D::get_collision_layer);
Expand Down
1 change: 1 addition & 0 deletions modules/csg/csg_shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class CSGShape3D : public GeometryInstance3D {

void set_use_collision(bool p_enable);
bool is_using_collision() const;
RID get_collision_rid() const;

void set_collision_layer(uint32_t p_layer);
uint32_t get_collision_layer() const;
Expand Down
6 changes: 6 additions & 0 deletions modules/csg/doc_classes/CSGShape3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
Returns whether or not the specified layer of the [member collision_mask] is enabled, given a [param layer_number] between 1 and 32.
</description>
</method>
<method name="get_collision_rid" qualifiers="const">
<return type="RID" />
<description>
Returns the collision object's [RID] if [member use_collision] is enabled.
</description>
</method>
<method name="get_meshes" qualifiers="const">
<return type="Array" />
<description>
Expand Down

0 comments on commit cad6e68

Please sign in to comment.