Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CollisionShape3D.make_convex_from_siblings() #41044

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/classes/CollisionShape3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<link>https://docs.godotengine.org/en/latest/tutorials/physics/physics_introduction.html</link>
</tutorials>
<methods>
<method name="make_convex_from_brothers">
<method name="make_convex_from_siblings">
<return type="void">
</return>
<description>
Expand Down
23 changes: 18 additions & 5 deletions scene/3d/collision_shape_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,36 @@

//TODO: Implement CylinderShape and HeightMapShape?

void CollisionShape3D::make_convex_from_brothers() {
void CollisionShape3D::make_convex_from_siblings() {
Node *p = get_parent();
if (!p) {
return;
}

Vector<Vector3> vertices;

for (int i = 0; i < p->get_child_count(); i++) {
Node *n = p->get_child(i);
MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(n);
if (mi) {
Ref<Mesh> m = mi->get_mesh();
if (m.is_valid()) {
Ref<Shape3D> s = m->create_convex_shape();
set_shape(s);
for (int j = 0; j < m->get_surface_count(); j++) {
Array a = m->surface_get_arrays(j);
if (!a.empty()) {
Vector<Vector3> v = a[RenderingServer::ARRAY_VERTEX];
for (int k = 0; k < v.size(); k++) {
vertices.append(mi->get_transform().xform(v[k]));
}
}
}
}
}
}

Ref<ConvexPolygonShape3D> shape = memnew(ConvexPolygonShape3D);
shape->set_points(vertices);
set_shape(shape);
}

void CollisionShape3D::_update_in_shape_owner(bool p_xform_only) {
Expand Down Expand Up @@ -137,8 +150,8 @@ void CollisionShape3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape3D::get_shape);
ClassDB::bind_method(D_METHOD("set_disabled", "enable"), &CollisionShape3D::set_disabled);
ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape3D::is_disabled);
ClassDB::bind_method(D_METHOD("make_convex_from_brothers"), &CollisionShape3D::make_convex_from_brothers);
ClassDB::set_method_flags("CollisionShape3D", "make_convex_from_brothers", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
ClassDB::bind_method(D_METHOD("make_convex_from_siblings"), &CollisionShape3D::make_convex_from_siblings);
ClassDB::set_method_flags("CollisionShape3D", "make_convex_from_siblings", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);

ClassDB::bind_method(D_METHOD("_update_debug_shape"), &CollisionShape3D::_update_debug_shape);

Expand Down
2 changes: 1 addition & 1 deletion scene/3d/collision_shape_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CollisionShape3D : public Node3D {
static void _bind_methods();

public:
void make_convex_from_brothers();
void make_convex_from_siblings();

void set_shape(const Ref<Shape3D> &p_shape);
Ref<Shape3D> get_shape() const;
Expand Down