Skip to content

Commit

Permalink
library: validate shapes for dynamic rigid bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jun 18, 2024
1 parent ada8405 commit 9dbc006
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ public Shape getJoltShape() {
assert joltShape != null;
return joltShape;
}

/**
* Test whether the shape can be applied to a dynamic rigid body.
*
* @return true if non-moving, false otherwise
*/
public boolean isNonMoving() {
boolean result = joltShape.mustBeStatic();
return result;
}
// *************************************************************************
// new protected methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public PhysicsRigidBody(CollisionShape shape, float mass, Vec3d location,
Validate.nonNull(location, "location");
Validate.nonZero(orientation, "orientation");

if (mass != massForStatic) {
validateDynamicShape(shape);
}
super.setCollisionShape(shape);
this.mass = mass;
this.joltBody = createRigidBody(shape, mass, location, orientation);
Expand Down Expand Up @@ -409,6 +412,9 @@ public void setCollisionShape(CollisionShape desiredShape) {
if (desiredShape == getCollisionShape()) {
return;
}
if (isDynamic()) {
validateDynamicShape(desiredShape);
}
super.setCollisionShape(desiredShape);
rebuildRigidBody();
}
Expand Down Expand Up @@ -495,4 +501,18 @@ private static MutableBody createRigidBody(CollisionShape shape, float mass,

return result;
}

/**
* Validate a shape as suitable for a dynamic body.
*
* @param shape (not null, unaffected)
*/
private static void validateDynamicShape(CollisionShape shape) {
assert shape != null;

if (shape.isNonMoving()) {
throw new IllegalStateException(
"Dynamic rigid body can't have a non-moving shape!");
}
}
}

0 comments on commit 9dbc006

Please sign in to comment.