Skip to content

Commit

Permalink
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 c3ee83c commit a0367e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,32 @@ public class GImpactCollisionShape extends CollisionShape {
/**
* 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();
}
// *************************************************************************
Expand Down Expand Up @@ -226,6 +233,9 @@ public void setScale(Vector3f scale) {
* 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
9 changes: 5 additions & 4 deletions src/test/java/TestLibbulletjme.java
Original file line number Diff line number Diff line change
Expand Up @@ -1191,14 +1191,15 @@ public void test012() {

CollisionShape box = new BoxCollisionShape(0.1f, 0.2f, 0.3f);
CollisionShape compound = new CompoundCollisionShape();
CollisionShape gimpact = new GImpactCollisionShape();

float[] hf = {0f, 0f, 0f, 0f};
CollisionShape heightfield = new HeightfieldCollisionShape(hf);

int[] indexArray = {0, 0, 0};
Vector3f[] positionArray = {new Vector3f(0f, 0f, 0f)};
IndexedMesh indexedMesh = new IndexedMesh(positionArray, indexArray);
CollisionShape gimpact = new GImpactCollisionShape(indexedMesh);

float[] hf = {0f, 0f, 0f, 0f};
CollisionShape heightfield = new HeightfieldCollisionShape(hf);

CollisionShape mesh = new MeshCollisionShape(true, indexedMesh);

Plane pl = new Plane(Vector3f.UNIT_Y, 0f);
Expand Down

0 comments on commit a0367e8

Please sign in to comment.