Skip to content

Instantly share code, notes, and snippets.

@a-type
Created September 21, 2020 19:08
Show Gist options
  • Save a-type/b65058da523b6759c8e769a2f05bf464 to your computer and use it in GitHub Desktop.
Save a-type/b65058da523b6759c8e769a2f05bf464 to your computer and use it in GitHub Desktop.
ThreeJS mesh geometry collider in cannon.js
// supposing "geometry" is a ThreeJS BufferGeometry - if it's a geometry, just skip this step
const geo = new Geometry().fromBufferGeometry(geometry);
const vertices = [];
const indices = [];
// "scale" is a uniform scale you may or may not have applied to your mesh as a whole.
// just set it to 1 if you don't scale the mesh.
for (const vert of geo.vertices) {
vertices.push(vert.x * scale);
vertices.push(vert.y * scale);
vertices.push(vert.z * scale);
}
for (const face of geo.faces) {
indices.push(face.a);
indices.push(face.b);
indices.push(face.c);
}
const collider = new Trimesh(vertices, indices);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment