add physics on blender model, three.js Physijs

Question!

How can i add physics on a blender model ? I tried ConvexMesh, ConcaveMesh but no luck.

var mesh = {... blender exporter ...}

var loader = new THREE.JSONLoader();
var mesh_obj = loader.parse(mesh,'./');
var mesh_materials = mesh_obj.materials;
var mesh_geometry = mesh_obj.geometry;

var _materials=[];
for ( var i = 0, i<mesh_materials.length;i ++ ) {
 var materialv = Physijs.createMaterial(mesh_materials[i],0.8,0.1);
 _materials.push(materialv)
}

mesh = new Physijs.ConcaveMesh(mesh_geometry, new THREE.MeshFaceMaterial(_materials),0 );
scene.add(mesh)

screenshoot



Answers
Maybe an old question but I had the same issue and opted for wrapping up the model in a BoxMesh "container" and adding the model as a child of the container. Below I have an example with a track model I used. Then set the child's position so that it is inside the Mesh. For more complex models I'm guessing an approach would be to encapsulate the individual geometries with other Physijs shapes.

var box_container = new Physijs.BoxMesh(
    new THREE.CubeGeometry( 1, 0.7, 1.5 ),
    new THREE.MeshBasicMaterial({ transparent: true, opacity: 0.0 })
    // Uncomment the next line to see the wireframe of the container shape
    // new THREE.MeshBasicMaterial({ wireframe: true, opacity: 0.5 })
);
// Assuming your model has already been imported
var track = global_models.track.clone();

track.position.set(0,0,0);
box_container.add(track);

You can see an example of my code using the track model I described with the wireframe around to see the container.

Track model with a BoxMesh wrapping for physics



Check this:

  • You should wrap new THREE.MeshFaceMaterial() in Physijs.CreateMaterial, not conversely.
  • You should set Gravity. (I can't see the full code so just check it in your source)
  • Check if you don't load a BufferGeometry
  • Check if you use a Physijs.Scene as a scene object.

To prevent such problems try WhitestormJS engine it is built using three.js and has built-in physics.



This video can help you solving your question :)
By: admin