Skip to content

Commit

Permalink
Merge pull request #41 from playsign/rigidbox
Browse files Browse the repository at this point in the history
Add hook to handle EC_RigidBody boxes using Physijs
  • Loading branch information
erno committed Mar 27, 2014
2 parents d35cd0b + 500dd29 commit 8f932af
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Application.prototype = {
this.dataConnection.scene.componentRemoved.add(this.viewer.onComponentRemoved.bind(this.viewer));
this.dataConnection.scene.threeScene = this.viewer.scene;


this.dataConnection.scene.componentAdded.add(
this.onRigidBodyPossiblyAdded.bind(this));

// an alternative to hooking per component attributeChanged signals,
// would simplify business registering/unregistering handlers in
// component lifetime mgmt:
Expand Down Expand Up @@ -193,3 +197,33 @@ function check() {
}
}

Application.prototype.onRigidBodyPossiblyAdded = function(entity, component) {
if (! (component instanceof EC_RigidBody))
return;


var onRigidBodyAttributeChanged = function(changedAttr, changeType) {
//console.log("onRigidBodyAddedOrChanged due to attributeChanged ->", changedAttr.ref);
if (component.shapeType !== 0) {
console.log("unhandled shape type " + component.shapeType);
return;
}
var physiCube = component.physiCube;
if (!physiCube) {
component.physiCube = physiCube = new THREE.Mesh(
new THREE.CubeGeometry(1, 1, 1),
this.viewer.wireframeMaterial);
physiCube.mass = 0;
var threeGroup = this.viewer.o3dByEntityId[entity.id];
threeGroup.add(physiCube);
}
console.log("ok, have cube");
var boxSize = component.size;
if (!(boxSize.x && boxSize.y && boxSize.z))
console.log("RigidBody of entitity " + entity.id + ": one or more dimensions are zero");
physiCube.scale.set(boxSize.x, boxSize.y, boxSize.z);
console.log("box updated");
}.bind(this);
onRigidBodyAttributeChanged();
component.attributeChanged.add(onRigidBodyAttributeChanged);
};

0 comments on commit 8f932af

Please sign in to comment.