chandlerprall / Physijs

Physics plugin for Three.js
MIT License
2.78k stars 456 forks source link

Loading object model and applying physics to it. #128

Open TheMaverickProgrammer opened 11 years ago

TheMaverickProgrammer commented 11 years ago

I'm very new to THREEjs and Physijs but so far it's been a blast to set everything up appropriately. However, I am struggling to apply physics to a custom model.

I'm using the ObjMTLLoader here: https://dl.dropboxusercontent.com/u/1333628/Code/obj_mtl_loader.js

But changes I made from THREE.Mesh to Physi.ConvexMesh are not working. My models suspend in mid-air. I read an issue where the programmer used an invisible box mesh over a graphical object to simulated physics and I'll go down this route if I absolutely have to but I would rather use the mesh given to my by the model.

https://dl.dropboxusercontent.com/u/1333628/Code/screenshot.png

TheMaverickProgrammer commented 11 years ago

I ended up creating an invisible mesh and the simulation runs now. However, I would still like to learn how to use the system to apply proper physics on a model based on shape. Any future advice would be welcome.

chandlerprall commented 11 years ago

At the moment it is difficult to create a Physijs mesh with the obj loader, as the loader returns the mesh and not an object / material set. You could ignore the returned mesh and create a new Physijs one from its geometry and material(s).

TheMaverickProgrammer commented 11 years ago

That was what I really wanted to do, but how would I go about doing that?

chandlerprall commented 11 years ago

You should be able to reuse the mesh's geometry and material properties when creating the Physijs mesh.

pknowles commented 10 years ago

Everyone here's talking about creating an invisible mesh. Ideally I wouldn't have a mesh for graphics to begin with but maybe this is unavoidable. One way that works is to set physMaterial.visible = false, where physMaterial is from physMaterial = Physijs.createMaterial(...). Actually I believe visible is inherited from the threejs material. Is this the best way?

On a related note, parenting is a nice way to keep your graphics mesh attached to your physics mesh...

var threeGeom = ...
var threeMaterial = ...
var threeObject = new THREE.Mesh(threeGeom, threeMaterial);

var physGeom = new THREE.CylinderGeometry(0.5, 0.5, 2.0);
var physMaterial = new Physijs.createMaterial(new THREE.MeshBasicMaterial({}), friction, restitution);
physMaterial.visible = false;
var physObject = new Physijs.CapsuleMesh(physGeom, physMaterial, mass);

//parents the complex graphics model to the simple collision geometry
physObject.add(threeObject);

//apply any offset transform between the graphics model and physics object
threeObject.position.y = -1.0;

//add the phys/three hierarchy to the scene
scene.add(physObject);