Skip to content

Commit

Permalink
library: disallow GImpact shapes with zero triangles
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Sep 12, 2024
1 parent 6a4d135 commit d59d424
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,36 +87,46 @@ protected GImpactCollisionShape() {
/**
* Instantiate a shape based on the specified CompoundMesh and offset.
*
* @param mesh the mesh on which to base the shape (not null, unaffected)
* @param mesh the mesh on which to base the shape (not null, must contain
* at least one triangle, unaffected)
* @param offset the offset to add to the vertex positions (not null,
* unaffected)
*/
public GImpactCollisionShape(CompoundMesh mesh, Vector3f offset) {
Validate.require(mesh.countTriangles() > 0, "at least one triangle");

this.nativeMesh = new CompoundMesh(mesh, offset);
createShape();
}

/**
* Instantiate a shape based on the specified native mesh(es).
*
* @param submeshes the mesh(es) on which to base the shape (not null)
* @param submeshes the mesh(es) on which to base the shape (not null, must
* contain at least one triangle)
*/
public GImpactCollisionShape(IndexedMesh... submeshes) {
this.nativeMesh = new CompoundMesh();
for (IndexedMesh submesh : submeshes) {
nativeMesh.add(submesh);
}
Validate.require(
nativeMesh.countTriangles() > 0, "at least one triangle");

createShape();
}

/**
* Instantiate a shape based on the specified JME mesh(es).
*
* @param jmeMeshes the mesh(es) on which to base the shape (not null,
* unaffected)
* @param jmeMeshes the mesh(es) on which to base the shape (not null, must
* contain at least one triangle, unaffected)
*/
public GImpactCollisionShape(Mesh... jmeMeshes) {
this.nativeMesh = new CompoundMesh(jmeMeshes);
Validate.require(
nativeMesh.countTriangles() > 0, "at least one triangle");

createShape();
}
// *************************************************************************
Expand Down Expand Up @@ -303,6 +313,9 @@ public void write(JmeExporter exporter) throws IOException {
* Instantiate the configured {@code btGImpactMeshShape}.
*/
private void createShape() {
int numTriangles = nativeMesh.countTriangles();
assert numTriangles > 0 : numTriangles;

long meshId = nativeMesh.nativeId();
long shapeId = createShape(meshId);
setNativeId(shapeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,21 @@ public void testEmptyShape() {
shape = new CompoundCollisionShape();
rigidBody = new PhysicsRigidBody(shape, PhysicsBody.massForStatic);

shape = new GImpactCollisionShape(indexedMeshArray);
rigidBody = new PhysicsRigidBody(shape, PhysicsBody.massForStatic);
// Attempt to create empty shapes in various illegal ways.
try {
shape = new GImpactCollisionShape(indexedMeshArray);
Assert.fail("Expected an IllegalArgumentException");
} catch (IllegalArgumentException exception) {
// do nothing
}

shape = new GImpactCollisionShape(jmeMeshArray);
rigidBody = new PhysicsRigidBody(shape, PhysicsBody.massForStatic);
try {
shape = new GImpactCollisionShape(jmeMeshArray);
Assert.fail("Expected an IllegalArgumentException");
} catch (IllegalArgumentException exception) {
// do nothing
}

// Attempt to create empty shapes in various illegal ways.
try {
shape = new HeightfieldCollisionShape(floatArray);
Assert.fail("Expected an IllegalArgumentException");
Expand Down

0 comments on commit d59d424

Please sign in to comment.