Skip to content

Commit 09fcaa7

Browse files
committed
Fixed crash when rendering a soft body 3d
When a mesh is provided that has vertices that are not referenced by any face, these vertices will be discarded. In the internal 'mesh_to_physics' map, this led to uninitialized data which could result in a crash. Now we initialize the map with -1 and report an error when users try to manipulate these vertices. Fixes #109883
1 parent 3defc85 commit 09fcaa7

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

modules/jolt_physics/objects/jolt_soft_body_3d.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ bool JoltSoftBody3D::_ref_shared_data() {
157157
const int mesh_index_count = mesh_indices.size();
158158

159159
mesh_to_physics.resize(mesh_vertex_count);
160+
for (int &index : mesh_to_physics) {
161+
index = -1;
162+
}
160163
physics_vertices.reserve(mesh_vertex_count);
161164
vertex_to_physics.reserve(mesh_vertex_count);
162165

@@ -409,7 +412,8 @@ void JoltSoftBody3D::apply_vertex_impulse(int p_index, const Vector3 &p_impulse)
409412

410413
ERR_FAIL_NULL(shared);
411414
ERR_FAIL_INDEX(p_index, (int)shared->mesh_to_physics.size());
412-
const size_t physics_index = (size_t)shared->mesh_to_physics[p_index];
415+
const int physics_index = shared->mesh_to_physics[p_index];
416+
ERR_FAIL_COND_MSG(physics_index < 0, vformat("Soft body vertex %d was not used by a face and has been omitted for '%s'. No impulse can be applied.", p_index, to_string()));
413417
ERR_FAIL_COND_MSG(pinned_vertices.has(physics_index), vformat("Failed to apply impulse to point at index %d for '%s'. Point was found to be pinned.", static_cast<int>(physics_index), to_string()));
414418

415419
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
@@ -672,12 +676,13 @@ void JoltSoftBody3D::update_rendering_server(PhysicsServer3DRenderingServerHandl
672676

673677
for (int i = 0; i < mesh_vertex_count; ++i) {
674678
const int physics_index = shared->mesh_to_physics[i];
679+
if (physics_index >= 0) {
680+
const Vector3 vertex = to_godot(physics_vertices[(size_t)physics_index].mPosition);
681+
const Vector3 normal = normals[(uint32_t)physics_index];
675682

676-
const Vector3 vertex = to_godot(physics_vertices[(size_t)physics_index].mPosition);
677-
const Vector3 normal = normals[(uint32_t)physics_index];
678-
679-
p_rendering_server_handler->set_vertex(i, vertex);
680-
p_rendering_server_handler->set_normal(i, normal);
683+
p_rendering_server_handler->set_vertex(i, vertex);
684+
p_rendering_server_handler->set_normal(i, normal);
685+
}
681686
}
682687

683688
p_rendering_server_handler->set_aabb(get_bounds());
@@ -688,7 +693,8 @@ Vector3 JoltSoftBody3D::get_vertex_position(int p_index) {
688693

689694
ERR_FAIL_NULL_V(shared, Vector3());
690695
ERR_FAIL_INDEX_V(p_index, (int)shared->mesh_to_physics.size(), Vector3());
691-
const size_t physics_index = (size_t)shared->mesh_to_physics[p_index];
696+
const int physics_index = shared->mesh_to_physics[p_index];
697+
ERR_FAIL_COND_V_MSG(physics_index < 0, Vector3(), vformat("Soft body vertex %d was not used by a face and has been omitted for '%s'. Position cannot be returned.", p_index, to_string()));
692698

693699
const JPH::SoftBodyMotionProperties &motion_properties = static_cast<const JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
694700
const JPH::Array<JPH::SoftBodyVertex> &physics_vertices = motion_properties.GetVertices();
@@ -702,7 +708,8 @@ void JoltSoftBody3D::set_vertex_position(int p_index, const Vector3 &p_position)
702708

703709
ERR_FAIL_NULL(shared);
704710
ERR_FAIL_INDEX(p_index, (int)shared->mesh_to_physics.size());
705-
const size_t physics_index = (size_t)shared->mesh_to_physics[p_index];
711+
const int physics_index = shared->mesh_to_physics[p_index];
712+
ERR_FAIL_COND_MSG(physics_index < 0, vformat("Soft body vertex %d was not used by a face and has been omitted for '%s'. Position cannot be set.", p_index, to_string()));
706713

707714
JPH::SoftBodyMotionProperties &motion_properties = static_cast<JPH::SoftBodyMotionProperties &>(*jolt_body->GetMotionPropertiesUnchecked());
708715
JPH::Array<JPH::SoftBodyVertex> &physics_vertices = motion_properties.GetVertices();

0 commit comments

Comments
 (0)