Created
March 28, 2012 22:23
-
-
Save jicksta/2231068 to your computer and use it in GitHub Desktop.
Experimenting with Collada support for Physijs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Physijs.MeshMixin, used to convert a THREE.Mesh into a Physijs.Mesh. Direct use of | |
// this mixin is not necessary if you're instantiating Physijs.Meshes directly. | |
// Returns the given THREE.Mesh with Physijs mesh behavior and metadata mixed in. | |
Physijs.MeshMixin = function(mesh, mass, params) { | |
var index; | |
params = params || {}; | |
Eventable.call(this); | |
if (!mesh.geometry.boundingBox) { | |
mesh.geometry.computeBoundingBox(); | |
} | |
mesh._physijs = { | |
type:null, | |
id:getObjectId(), | |
mass:mass || 0, | |
touches:[], | |
linearVelocity:new THREE.Vector3, | |
angularVelocity:new THREE.Vector3 | |
}; | |
for (index in params) { | |
if (!params.hasOwnProperty(index)) continue; | |
this._physijs[index] = params[index]; | |
} | |
return mesh; | |
}; | |
// Physijs.Mesh | |
Physijs.Mesh = function(geometry, material, mass, params) { | |
if (!geometry) return; | |
THREE.Mesh.call(this, geometry, material); | |
Physijs.MeshMixin(this, mass, params); | |
}; | |
Physijs.Scene.prototype.addFromCollada = function(object) { | |
// Not sure if it's correct to be using THREE.Scene.prototype.add instead of THREE.Mesh.prototype.add here. | |
THREE.Scene.prototype.add.call(this, object); | |
var material = new THREE.MeshBasicMaterial({color: 0xff0000}); | |
THREE.SceneUtils.traverseHierarchy(object, function(mesh) { | |
Physijs.MeshMixin(mesh, material) | |
object.__dirtyPosition = true; | |
object.__dirtyRotation = true; | |
this._objects[object._physijs.id] = object; | |
object.world = this; | |
this.execute( 'addObject', object._physijs ); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment