5

I am creating a THREE.Mesh object using a THREE.JSONLoader object like so:

// Create castle.
loader.load('/Meshes/CastleTower.js', function(geometry, materials) {
    var tmp_material = new THREE.MeshLambertMaterial();
    THREE.ColorUtils.adjustHSV(tmp_material.color, 0, 0, 0.9);

    var castle = new THREE.Mesh(geometry, tmp_material);
    castle.scale.set(0.2, 0.2, 0.2);
    castle.rotation.setX(-Math.PI/2);
    scene.add(castle);
});

Is it possible to create a CANNON.RigidBody from the THREE.Mesh (var castle) or THREE.Geometry (var geometry) object? Another way you could read this is: How do you make any custom THREE.Mesh "solid"?

Update

I used Blender, created a new castle from boxes, and exported it to the Three.js format. If you set the mass to 0 of a CANNON.Body, it remains static. This worked out perfectly...

rgajrawala
  • 2,148
  • 1
  • 22
  • 35

2 Answers2

2

I had a similar issue and created the necessary "points" and "faces" (as described in Cannon docs) from the THREE.Geometry (called geometry here) with these two functions:

cannonPoints = geometry.vertices.map(function(v) {
    return new CANNON.Vec3( v.x, v.y, v.z )
})

cannonFaces = geometry.faces.map(function(f) {
    return [f.a, f.b, f.c]
})
Tyler Wolf
  • 106
  • 4
1

Well it depends on how exact the physical representatin of your model should be. I'm not very familiar with cannon.js, but here are some options I know:

  • use "computeBoundingBox" and on your tower and create a cannon.js box with those bounds
  • use "computeBoundingSphere" in a similar way
  • use physics for a concave (i.e. arbitrary) mesh. This is the most performance consuming way. Cannon.js has an example here: http://schteppe.github.io/cannon.js/demos/bunny.html

A non cannon.js related approach would be to e.g. use Recast. Recast would load your .obj file for you and create a navigation mesh for you according to your settings. Then you could walk around there (absolutely great if you have a RTS birdview like game, or bots running around). A recast javascript port can be found here: https://github.com/vincent/recast.js

Hope this helps!

Doidel
  • 1,743
  • 1
  • 14
  • 22
  • `computeBoundingBox` and `computerBoundingSphere` do not work in my case. I want the players to be able to walk in the castle, jump down stairs, etc... as if it's real-life. Using `CANNON.ConvexPolyhedron`s to create a `CANNON.Compound` could work, but I would have to change the pattern of [my object file](http://invisiball.herokuapp.com/Meshes/CastleTower/CastleTower.js) to match that of the [example's](http://schteppe.github.io/cannon.js/demos/bunny.js). I'm looking into how to do that right now. – rgajrawala Jul 09 '14 at 18:53
  • 1
    I also mentioned Recast in the answer now, for the sake of completeness/documentation. Might be a good choice for some, and maybe even for you – Doidel Jul 10 '14 at 06:47
  • 1
    https://github.com/schteppe/cannon.js/issues/144 I happened to find this at cannon.js... he answered all your questions, what do you want more? He says a) there is no support for arbitrary meshes in cannon.js and b) you might want to reconstruct your castle from smaller physics pieces (like in the image he showed). There *are* ways to create physics for arbitrary meshes automatically, but it's hugely complicated to code on your own. As mentioned, Recast does something in that direction. Apart from that I really recommend to rebuild your castle with primitives. – Doidel Jul 11 '14 at 08:05
  • Yup, I'm going to reconstruct my castle using smaller pieces and put them together into a `THREE.Compound`. Thank you for showing me the bunny example, I completely forgot about it. Unfortunately there isn't a lot of documentation on `CANNON.Compound`, but I'll play around with it and maybe even create a library to break custom meshes down into primitive shapes and then use `CANNON.Compound`s to rebuild it. Will update this question when something breakthrough happens. :) – rgajrawala Jul 11 '14 at 17:55
  • Just an update: I used Blender and created a new castle from boxes. If you set the mass to 0 of a `CANNON.Body`, it remains static. [This](https://github.com/Invisiball/Invisiball/blob/master/Assets/Js/Game.js#L407) worked out perfectly... – rgajrawala Oct 30 '14 at 05:10