schteppe / cannon.js

A lightweight 3D physics engine written in JavaScript.
http://schteppe.github.com/cannon.js
MIT License
4.67k stars 709 forks source link

TextGeometry in Cannon? #298

Open Tzikas opened 8 years ago

Tzikas commented 8 years ago

How would I import a string of text as new CANNON.Body({}) ?

Is that possible? Would I convert text to something like the bunny.js file seen here? http://schteppe.github.io/cannon.js/demos/bunny.html

Thanks.

schteppe commented 8 years ago

Hi, If you want to use the ConvexPolyhedron class, you need to do use something like HACD to decompose the mesh into convex pieces. You could also use the Trimesh shape directly but it has limited collisions... See the collision table.

You could also use simpler shapes to make up efficient approximate collision shapes for your geometry. Like this:

class-boxcollider-2

Tzikas commented 8 years ago

How do you use https://github.com/kmammou/v-hacd ? I can't find any clear instructions.

schteppe commented 8 years ago

I never actually used HACD myself, I downloaded the bunny pre-decomposed... Can't help you there.

Tzikas commented 8 years ago

I'm trying to have multiple letters crash into one another. Do you think I could wrap each 3D letter in a box shape and simulate it that way? It doesn't have to be perfect.

schteppe commented 8 years ago

Boxes should be fine I think! Depends on how you will use them.

An other idea: if you don't need bevels on your text mesh, you could probably use a 2d polygon decomposition library and then extrude the convex 2d polygons into 3d shapes. See https://github.com/schteppe/poly-decomp.js

Tzikas commented 8 years ago

I'll look into that, but to be honest I have no idea how to implement it. I'm creating 3D letter using THREE.TextGeometry and THREE.Mesh. How would I insert that obj into a Cannon box ?: var boxShape = new CANNON.Box(new CANNON.Vec3(1,1,1)); var b2 = new CANNON.Body({ mass: 5 }); b2.addShape(boxShape);

schteppe commented 8 years ago

I guess you could get the bounding box from the mesh (see http://stackoverflow.com/questions/15492857/any-way-to-get-a-bounding-box-from-a-three-js-object3d) and then use the size of that box for the Cannon box.

To sync positions and rotations, see https://github.com/schteppe/cannon.js/blob/master/examples/README.markdown#threejs (may be out of date).