4

I need to know how can I generate a complex shape to integrate in my Three.js scene, with the benefit of Cannon.js physics.

I started from this example: http://schteppe.github.io/cannon.js/demos/bunny.html

Looking at the code, I noticed that the bunny is described with its faces and vertices in arrays. Seems pretty powerful but how can I do if I want to generate these arrays dynamically ?

For example, using only Three.js, it's possible to export a Blender model on json format and to load it inside the scene programmatically. I need to do the same thing for Cannon.js, but it doesn't seem to work the same way as Three.js with Blender models.

Thanks in advance

WestLangley
  • 102,557
  • 10
  • 276
  • 276
Rotan
  • 577
  • 6
  • 16

2 Answers2

7

The bunny is made out of convex shapes that have been precomputed using a Convex Decomposition software called HACD. Convex decomposition is when you take a concave mesh and turn it into convex subshapes.

One solution is to do the same thing yourself: use a tool to make convex subshapes from a more complicated mesh. An alternative solution is to use simple built-in primitives such as spheres and boxes to build your physics shape. A shape that consist of several subshapes is called a compound shape (see the Cannon.js compound demo). Many game developers use this approach because it's a simpler workflow and provides better physics performance.

Compound collider in Unity

(Picture from Unity manual)

schteppe
  • 1,984
  • 13
  • 15
7

For the future readers, there is an example to convert a custom mesh to cannon body.

I hope you find this answer useful.