Skip to content

Commit

Permalink
implementing getAngularVelocity and getLinearVelocity, closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
chandlerprall committed Mar 21, 2012
1 parent 52da407 commit f5d7a8f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
14 changes: 13 additions & 1 deletion examples/body.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
scene = new Physijs.Scene;
scene.setGravity({ x: 0, y: -10, z: 0 });

scene.addEventListener('update', function( details ) {
var vector,
info = '';

vector = box.getLinearVelocity();
info += 'Linear Velocity: ' + Math.round(vector.x * 10) / 10 + ',' + Math.round(vector.y * 10) / 10 + ',' + Math.round(vector.z * 10) / 10;

vector = box.getAngularVelocity();
info += '<br />Angular Velocity: ' + Math.round(vector.x * 10) / 10 + ',' + Math.round(vector.y * 10) / 10 + ',' + Math.round(vector.z * 10) / 10;
document.getElementById( 'info' ).innerHTML = info;
});

camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
Expand Down Expand Up @@ -156,7 +168,7 @@

<body>
<p>
Interacting with a rigid body - move your cursor near the box to push it.
Interacting with a rigid body - move your cursor near the box to push it.<br /><span id="info"></span>
</p>
<div id="viewport"></div>
</body>
Expand Down
20 changes: 19 additions & 1 deletion physi.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,19 @@ var Physijs = (function() {
obj = event.data.params.objects[obj_id];
sceneObj = self._objects[obj_id];

// Update position & rotation
sceneObj.position.set( obj.pos_x, obj.pos_y, obj.pos_z );
if ( sceneObj.useQuaternion) {
sceneObj.quaternion.set( obj.quat_x, obj.quat_y, obj.quat_z, obj.quat_w );
} else {
sceneObj.rotation = getEulerXYZFromQuaternion( obj.quat_x, obj.quat_y, obj.quat_z, obj.quat_w );
};

// Record velocities
sceneObj._physijs.linearVelocity.set( obj.linear_x, obj.linear_y, obj.linear_z );
sceneObj._physijs.angularVelocity.set( obj.angular_x, obj.angular_y, obj.angular_z );

// Collisions
collided_with.length = 0;

if ( obj.collisions.length > 0 ) {
Expand Down Expand Up @@ -334,7 +340,9 @@ var Physijs = (function() {
type: null,
id: getObjectId(),
mass: mass || 0,
touches: []
touches: [],
linearVelocity: new THREE.Vector3,
angularVelocity: new THREE.Vector3
};

for ( index in params ) {
Expand Down Expand Up @@ -371,13 +379,23 @@ var Physijs = (function() {
}
};

// Physijs.Mesh.getAngularVelocity
Physijs.Mesh.prototype.getAngularVelocity = function () {
return this._physijs.angularVelocity;
};

// Physijs.Mesh.setAngularVelocity
Physijs.Mesh.prototype.setAngularVelocity = function ( velocity ) {
if ( this.world ) {
this.world.execute( 'setAngularVelocity', { id: this._physijs.id, x: velocity.x, y: velocity.y, z: velocity.z } );
}
};

// Physijs.Mesh.getLinearVelocity
Physijs.Mesh.prototype.getLinearVelocity = function () {
return this._physijs.linearVelocity;
};

// Physijs.Mesh.setLinearVelocity
Physijs.Mesh.prototype.setLinearVelocity = function ( velocity ) {
if ( this.world ) {
Expand Down
12 changes: 12 additions & 0 deletions physijs_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var // temp variables
_object,
_vector1,
_vector2,
_transform,

// functions
Expand Down Expand Up @@ -245,6 +247,8 @@ reportWorld = function() {

origin = transform.getOrigin();
rotation = transform.getRotation();
_vector1 = object.getLinearVelocity();
_vector2 = object.getAngularVelocity();

report[object.id] = {
pos_x: origin.x(),
Expand All @@ -256,6 +260,14 @@ reportWorld = function() {
quat_z: rotation.z(),
quat_w: rotation.w(),

linear_x: _vector1.x(),
linear_y: _vector1.y(),
linear_z: _vector1.z(),

angular_x: _vector2.x(),
angular_y: _vector2.y(),
angular_z: _vector2.z(),

collisions: []
};
}
Expand Down

0 comments on commit f5d7a8f

Please sign in to comment.