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

[3.2] Fixes division by zero when 3d body does not have valid shape #46809

Merged
merged 1 commit into from
Mar 9, 2021
Merged
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
20 changes: 14 additions & 6 deletions servers/physics/body_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,44 +53,47 @@ void BodySW::_update_transform_dependant() {

void BodySW::update_inertias() {

//update shapes and motions
// Update shapes and motions.

switch (mode) {

case PhysicsServer::BODY_MODE_RIGID: {

//update tensor for all shapes, not the best way but should be somehow OK. (inspired from bullet)
// Update tensor for all shapes, not the best way but should be somehow OK. (inspired from bullet)
real_t total_area = 0;

for (int i = 0; i < get_shape_count(); i++) {

total_area += get_shape_area(i);
}

// We have to recompute the center of mass
// We have to recompute the center of mass.
center_of_mass_local.zero();

for (int i = 0; i < get_shape_count(); i++) {
real_t area = get_shape_area(i);

real_t mass = area * this->mass / total_area;

// NOTE: we assume that the shape origin is also its center of mass
// NOTE: we assume that the shape origin is also its center of mass.
center_of_mass_local += mass * get_shape_transform(i).origin;
}

center_of_mass_local /= mass;

// Recompute the inertia tensor
// Recompute the inertia tensor.
Basis inertia_tensor;
inertia_tensor.set_zero();
bool inertia_set = false;

for (int i = 0; i < get_shape_count(); i++) {

if (is_shape_disabled(i)) {
continue;
}

inertia_set = true;

const ShapeSW *shape = get_shape(i);

real_t area = get_shape_area(i);
Expand All @@ -108,7 +111,12 @@ void BodySW::update_inertias() {
inertia_tensor += shape_inertia_tensor + (Basis() * shape_origin.dot(shape_origin) - shape_origin.outer(shape_origin)) * mass;
}

// Compute the principal axes of inertia
// Set the inertia to a valid value when there are no valid shapes.
if (!inertia_set) {
inertia_tensor.set_diagonal(Vector3(1.0, 1.0, 1.0));
}

// Compute the principal axes of inertia.
principal_inertia_axes_local = inertia_tensor.diagonalize().transposed();
_inv_inertia = inertia_tensor.get_main_diagonal().inverse();

Expand Down