diff --git a/physijs_worker.js b/physijs_worker.js index 649b44d..1613954 100644 --- a/physijs_worker.js +++ b/physijs_worker.js @@ -1,7 +1,7 @@ 'use strict'; -var +var transferableMessage = self.webkitPostMessage || self.postMessage, - + // enum MESSAGE_TYPES = { WORLDREPORT: 0, @@ -9,12 +9,12 @@ var VEHICLEREPORT: 2, CONSTRAINTREPORT: 3 }, - + // temp variables _object, _vector, _transform, - + // functions public_functions = {}, getShapeFromCache, @@ -24,7 +24,7 @@ var reportVehicles, reportCollisions, reportConstraints, - + // world variables fixedTimeStep, // used when calling stepSimulation rateLimit, // sets whether or not to sync the simulation rate with fixedTimeStep @@ -51,16 +51,16 @@ var // up. All are cleaned up when they're corresponding body is destroyed. // Unfortunately, it's very difficult to get at these objects from the // body, so we have to track them ourselves. - _motion_states = {}, + _motion_states = {}, // Don't need to worry about it for cached shapes. _noncached_shapes = {}, // A body with a compound shape always has a regular shape as well, so we // have track them separately. - _compound_shapes = {}, - + _compound_shapes = {}, + // object reporting REPORT_CHUNKSIZE, // report array is increased in increments of this chunk size - + WORLDREPORT_ITEMSIZE = 14, // how many float values each reported item needs worldreport, @@ -91,7 +91,7 @@ setShapeCache = function ( cache_key, shape ) { createShape = function( description ) { var cache_key, shape; - + _transform.setIdentity(); switch ( description.type ) { case 'plane': @@ -104,7 +104,7 @@ createShape = function( description ) { setShapeCache( cache_key, shape ); } break; - + case 'box': cache_key = 'box_' + description.width + '_' + description.height + '_' + description.depth; if ( ( shape = getShapeFromCache( cache_key ) ) === null ) { @@ -115,7 +115,7 @@ createShape = function( description ) { setShapeCache( cache_key, shape ); } break; - + case 'sphere': cache_key = 'sphere_' + description.radius; if ( ( shape = getShapeFromCache( cache_key ) ) === null ) { @@ -123,7 +123,7 @@ createShape = function( description ) { setShapeCache( cache_key, shape ); } break; - + case 'cylinder': cache_key = 'cylinder_' + description.width + '_' + description.height + '_' + description.depth; if ( ( shape = getShapeFromCache( cache_key ) ) === null ) { @@ -134,7 +134,7 @@ createShape = function( description ) { setShapeCache( cache_key, shape ); } break; - + case 'capsule': cache_key = 'capsule_' + description.radius + '_' + description.height; if ( ( shape = getShapeFromCache( cache_key ) ) === null ) { @@ -143,7 +143,7 @@ createShape = function( description ) { setShapeCache( cache_key, shape ); } break; - + case 'cone': cache_key = 'cone_' + description.radius + '_' + description.height; if ( ( shape = getShapeFromCache( cache_key ) ) === null ) { @@ -151,14 +151,14 @@ createShape = function( description ) { setShapeCache( cache_key, shape ); } break; - + case 'concave': var i, triangle, triangle_mesh = new Ammo.btTriangleMesh; if (!description.triangles.length) return false for ( i = 0; i < description.triangles.length; i++ ) { triangle = description.triangles[i]; - + _vec3_1.setX(triangle[0].x); _vec3_1.setY(triangle[0].y); _vec3_1.setZ(triangle[0].z); @@ -170,7 +170,7 @@ createShape = function( description ) { _vec3_3.setX(triangle[2].x); _vec3_3.setY(triangle[2].y); _vec3_3.setZ(triangle[2].z); - + triangle_mesh.addTriangle( _vec3_1, _vec3_2, @@ -186,18 +186,18 @@ createShape = function( description ) { ); _noncached_shapes[description.id] = shape; break; - + case 'convex': var i, point, shape = new Ammo.btConvexHullShape; for ( i = 0; i < description.points.length; i++ ) { point = description.points[i]; - + _vec3_1.setX(point.x); _vec3_1.setY(point.y); _vec3_1.setZ(point.z); shape.addPoint(_vec3_1); - + } _noncached_shapes[description.id] = shape; break; @@ -225,29 +225,29 @@ createShape = function( description ) { _vec3_1.setX(description.xsize/(description.xpts - 1)); _vec3_1.setY(description.ysize/(description.ypts - 1)); _vec3_1.setZ(1); - + shape.setLocalScaling(_vec3_1); _noncached_shapes[description.id] = shape; break; - + default: // Not recognized return; break; } - + return shape; }; public_functions.init = function( params ) { importScripts( params.ammo ); - + _transform = new Ammo.btTransform; _vec3_1 = new Ammo.btVector3(0,0,0); _vec3_2 = new Ammo.btVector3(0,0,0); _vec3_3 = new Ammo.btVector3(0,0,0); _quat = new Ammo.btQuaternion(0,0,0,0); - + REPORT_CHUNKSIZE = params.reportsize || 50; if ( SUPPORT_TRANSFERABLE ) { // Transferable messages are supported, take advantage of them with TypedArrays @@ -266,39 +266,39 @@ public_functions.init = function( params ) { collisionreport[0] = MESSAGE_TYPES.COLLISIONREPORT; vehiclereport[0] = MESSAGE_TYPES.VEHICLEREPORT; constraintreport[0] = MESSAGE_TYPES.CONSTRAINTREPORT; - + var collisionConfiguration = new Ammo.btDefaultCollisionConfiguration, dispatcher = new Ammo.btCollisionDispatcher( collisionConfiguration ), solver = new Ammo.btSequentialImpulseConstraintSolver, broadphase; - + if ( !params.broadphase ) params.broadphase = { type: 'dynamic' }; switch ( params.broadphase.type ) { case 'sweepprune': - + _vec3_1.setX(params.broadphase.aabbmin.x); _vec3_1.setY(params.broadphase.aabbmin.y); _vec3_1.setZ(params.broadphase.aabbmin.z); - + _vec3_2.setX(params.broadphase.aabbmax.x); _vec3_2.setY(params.broadphase.aabbmax.y); _vec3_2.setZ(params.broadphase.aabbmax.z); - + broadphase = new Ammo.btAxisSweep3( _vec3_1, _vec3_2 ); - + break; - + case 'dynamic': default: broadphase = new Ammo.btDbvtBroadphase; break; } - + world = new Ammo.btDiscreteDynamicsWorld( dispatcher, broadphase, solver, collisionConfiguration ); - + fixedTimeStep = params.fixedTimeStep; rateLimit = params.rateLimit; @@ -325,7 +325,7 @@ public_functions.setGravity = function( description ) { }; public_functions.addObject = function( description ) { - + var i, localInertia, shape, motionState, rbInfo, body; @@ -335,29 +335,29 @@ if (!shape) return if ( description.children ) { var compound_shape = new Ammo.btCompoundShape, _child; compound_shape.addChildShape( _transform, shape ); - + for ( i = 0; i < description.children.length; i++ ) { _child = description.children[i]; - + var trans = new Ammo.btTransform; trans.setIdentity(); - + _vec3_1.setX(_child.position_offset.x); _vec3_1.setY(_child.position_offset.y); _vec3_1.setZ(_child.position_offset.z); - trans.setOrigin(_vec3_1); - + trans.setOrigin(_vec3_1); + _quat.setX(_child.rotation.x); _quat.setY(_child.rotation.y); _quat.setZ(_child.rotation.z); _quat.setW(_child.rotation.w); - trans.setRotation(_quat); - + trans.setRotation(_quat); + shape = createShape( description.children[i] ); compound_shape.addChildShape( trans, shape ); Ammo.destroy(trans); } - + shape = compound_shape; _compound_shapes[ description.id ] = shape; } @@ -365,45 +365,45 @@ if ( description.children ) { _vec3_1.setY(0); _vec3_1.setZ(0); shape.calculateLocalInertia( description.mass, _vec3_1 ); - + _transform.setIdentity(); - + _vec3_2.setX(description.position.x); _vec3_2.setY(description.position.y); _vec3_2.setZ(description.position.z); _transform.setOrigin(_vec3_2); - + _quat.setX(description.rotation.x); _quat.setY(description.rotation.y); _quat.setZ(description.rotation.z); _quat.setW(description.rotation.w); _transform.setRotation(_quat); - + motionState = new Ammo.btDefaultMotionState( _transform ); // #TODO: btDefaultMotionState supports center of mass offset as second argument - implement rbInfo = new Ammo.btRigidBodyConstructionInfo( description.mass, motionState, shape, _vec3_1 ); - + if ( description.materialId !== undefined ) { rbInfo.set_m_friction( _materials[ description.materialId ].friction ); rbInfo.set_m_restitution( _materials[ description.materialId ].restitution ); } - + body = new Ammo.btRigidBody( rbInfo ); Ammo.destroy(rbInfo); - + if ( typeof description.collision_flags !== 'undefined' ) { body.setCollisionFlags( description.collision_flags ); } - + world.addRigidBody( body ); - + body.id = description.id; _objects[ body.id ] = body; _motion_states[ body.id ] = motionState; - + var ptr = body.a != undefined ? body.a : body.ptr; _objects_ammo[ptr] = body.id; _num_objects++; - + transferableMessage({ cmd: 'objectReady', params: body.id }); }; @@ -441,19 +441,19 @@ public_functions.addWheel = function( description ) { tuning.set_m_maxSuspensionTravelCm( description.tuning.max_suspension_travel ); tuning.set_m_maxSuspensionForce( description.tuning.max_suspension_force ); } - + _vec3_1.setX(description.connection_point.x); _vec3_1.setY(description.connection_point.y); _vec3_1.setZ(description.connection_point.z); - + _vec3_2.setX(description.wheel_direction.x); _vec3_2.setY(description.wheel_direction.y); _vec3_2.setZ(description.wheel_direction.z); - + _vec3_3.setX(description.wheel_axle.x); _vec3_3.setY(description.wheel_axle.y); _vec3_3.setZ(description.wheel_axle.z); - + _vehicles[description.id].addWheel( _vec3_1, _vec3_2, @@ -509,14 +509,14 @@ public_functions.removeObject = function( details ) { public_functions.updateTransform = function( details ) { _object = _objects[details.id]; _object.getMotionState().getWorldTransform( _transform ); - + if ( details.pos ) { _vec3_1.setX(details.pos.x); _vec3_1.setY(details.pos.y); _vec3_1.setZ(details.pos.z); _transform.setOrigin(_vec3_1); } - + if ( details.quat ) { _quat.setX(details.quat.x); _quat.setY(details.quat.y); @@ -524,7 +524,7 @@ public_functions.updateTransform = function( details ) { _quat.setW(details.quat.w); _transform.setRotation(_quat); } - + _object.setWorldTransform( _transform ); _object.activate(); }; @@ -532,25 +532,25 @@ public_functions.updateTransform = function( details ) { public_functions.updateMass = function( details ) { // #TODO: changing a static object into dynamic is buggy _object = _objects[details.id]; - + // Per http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?p=&f=9&t=3663#p13816 world.removeRigidBody( _object ); - + _vec3_1.setX(0); _vec3_1.setY(0); _vec3_1.setZ(0); - + _object.setMassProps( details.mass, _vec3_1 ); world.addRigidBody( _object ); _object.activate(); }; public_functions.applyCentralImpulse = function ( details ) { - + _vec3_1.setX(details.x); _vec3_1.setY(details.y); _vec3_1.setZ(details.z); - + _objects[details.id].applyCentralImpulse(_vec3_1); _objects[details.id].activate(); }; @@ -560,7 +560,7 @@ public_functions.applyImpulse = function ( details ) { _vec3_1.setX(details.impulse_x); _vec3_1.setY(details.impulse_y); _vec3_1.setZ(details.impulse_z); - + _vec3_2.setX(details.x); _vec3_2.setY(details.y); _vec3_2.setZ(details.z); @@ -573,29 +573,29 @@ public_functions.applyImpulse = function ( details ) { }; public_functions.applyCentralForce = function ( details ) { - + _vec3_1.setX(details.x); _vec3_1.setY(details.y); _vec3_1.setZ(details.z); - + _objects[details.id].applyCentralForce(_vec3_1); _objects[details.id].activate(); }; public_functions.applyForce = function ( details ) { - + _vec3_1.setX(details.impulse_x); _vec3_1.setY(details.impulse_y); _vec3_1.setZ(details.impulse_z); - + _vec3_2.setX(details.x); _vec3_2.setY(details.y); _vec3_2.setZ(details.z); - + _objects[details.id].applyForce( _vec3_1, _vec3_2 - ); + ); _objects[details.id].activate(); }; @@ -608,7 +608,7 @@ public_functions.setAngularVelocity = function ( details ) { _vec3_1.setX(details.x); _vec3_1.setY(details.y); _vec3_1.setZ(details.z); - + _objects[details.id].setAngularVelocity( _vec3_1 ); @@ -620,7 +620,7 @@ public_functions.setLinearVelocity = function ( details ) { _vec3_1.setX(details.x); _vec3_1.setY(details.y); _vec3_1.setZ(details.z); - + _objects[details.id].setLinearVelocity( _vec3_1 ); @@ -632,7 +632,7 @@ public_functions.setAngularFactor = function ( details ) { _vec3_1.setX(details.x); _vec3_1.setY(details.y); _vec3_1.setZ(details.z); - + _objects[details.id].setAngularFactor( _vec3_1 ); @@ -643,7 +643,7 @@ public_functions.setLinearFactor = function ( details ) { _vec3_1.setX(details.x); _vec3_1.setY(details.y); _vec3_1.setZ(details.z); - + _objects[details.id].setLinearFactor( _vec3_1 ); @@ -665,28 +665,28 @@ public_functions.addConstraint = function ( details ) { var constraint; switch ( details.type ) { - + case 'point': if ( details.objectb === undefined ) { - + _vec3_1.setX(details.positiona.x); _vec3_1.setY(details.positiona.y); _vec3_1.setZ(details.positiona.z); - + constraint = new Ammo.btPoint2PointConstraint( _objects[ details.objecta ], _vec3_1 ); } else { - + _vec3_1.setX(details.positiona.x); _vec3_1.setY(details.positiona.y); _vec3_1.setZ(details.positiona.z); - + _vec3_2.setX(details.positionb.x); _vec3_2.setY(details.positionb.y); _vec3_2.setZ(details.positionb.z); - + constraint = new Ammo.btPoint2PointConstraint( _objects[ details.objecta ], _objects[ details.objectb ], @@ -695,29 +695,29 @@ public_functions.addConstraint = function ( details ) { ); } break; - + case 'hinge': if ( details.objectb === undefined ) { - + _vec3_1.setX(details.positiona.x); _vec3_1.setY(details.positiona.y); _vec3_1.setZ(details.positiona.z); - + _vec3_2.setX(details.axis.x); _vec3_2.setY(details.axis.y); _vec3_2.setZ(details.axis.z); - + constraint = new Ammo.btHingeConstraint( _objects[ details.objecta ], _vec3_1, _vec3_2 ); } else { - + _vec3_1.setX(details.positiona.x); _vec3_1.setY(details.positiona.y); _vec3_1.setZ(details.positiona.z); - + _vec3_2.setX(details.positionb.x); _vec3_2.setY(details.positionb.y); _vec3_2.setZ(details.positionb.z); @@ -725,7 +725,7 @@ public_functions.addConstraint = function ( details ) { _vec3_3.setX(details.axis.x); _vec3_3.setY(details.axis.y); _vec3_3.setZ(details.axis.z); - + constraint = new Ammo.btHingeConstraint( _objects[ details.objecta ], _objects[ details.objectb ], @@ -736,22 +736,22 @@ public_functions.addConstraint = function ( details ) { ); } break; - + case 'slider': var transforma, transformb, rotation; - + transforma = new Ammo.btTransform(); - + _vec3_1.setX(details.positiona.x); _vec3_1.setY(details.positiona.y); _vec3_1.setZ(details.positiona.z); - + transforma.setOrigin(_vec3_1); - + var rotation = transforma.getRotation(); rotation.setEuler( details.axis.x, details.axis.y, details.axis.z ); transforma.setRotation( rotation ); - + if ( details.objectb ) { transformb = new Ammo.btTransform(); @@ -760,11 +760,11 @@ public_functions.addConstraint = function ( details ) { _vec3_2.setZ(details.positionb.z); transformb.setOrigin(_vec3_2); - + rotation = transformb.getRotation(); rotation.setEuler( details.axis.x, details.axis.y, details.axis.z ); transformb.setRotation( rotation ); - + constraint = new Ammo.btSliderConstraint( _objects[ details.objecta ], _objects[ details.objectb ], @@ -779,85 +779,85 @@ public_functions.addConstraint = function ( details ) { true ); } - + Ammo.destroy(transforma); if (transformb != undefined) { - Ammo.destroy(transformb); + Ammo.destroy(transformb); } break; - + case 'conetwist': var transforma, transformb; - + transforma = new Ammo.btTransform(); transforma.setIdentity(); - + transformb = new Ammo.btTransform(); transformb.setIdentity(); - + _vec3_1.setX(details.positiona.x); _vec3_1.setY(details.positiona.y); _vec3_1.setZ(details.positiona.z); - + _vec3_2.setX(details.positionb.x); _vec3_2.setY(details.positionb.y); _vec3_2.setZ(details.positionb.z); - + transforma.setOrigin(_vec3_1); transformb.setOrigin(_vec3_2); - + var rotation = transforma.getRotation(); rotation.setEulerZYX( -details.axisa.z, -details.axisa.y, -details.axisa.x ); transforma.setRotation( rotation ); - + rotation = transformb.getRotation(); rotation.setEulerZYX( -details.axisb.z, -details.axisb.y, -details.axisb.x ); transformb.setRotation( rotation ); - + constraint = new Ammo.btConeTwistConstraint( _objects[ details.objecta ], _objects[ details.objectb ], transforma, transformb ); - + constraint.setLimit( Math.PI, 0, Math.PI ); - + Ammo.destroy(transforma); - Ammo.destroy(transformb); - + Ammo.destroy(transformb); + break; - + case 'dof': var transforma, transformb, rotation; - + transforma = new Ammo.btTransform(); transforma.setIdentity(); - + _vec3_1.setX(details.positiona.x); _vec3_1.setY(details.positiona.y); _vec3_1.setZ(details.positiona.z); - + transforma.setOrigin(_vec3_1 ); - + rotation = transforma.getRotation(); rotation.setEulerZYX( -details.axisa.z, -details.axisa.y, -details.axisa.x ); transforma.setRotation( rotation ); - + if ( details.objectb ) { transformb = new Ammo.btTransform(); transformb.setIdentity(); - + _vec3_2.setX(details.positionb.x); _vec3_2.setY(details.positionb.y); _vec3_2.setZ(details.positionb.z); - + transformb.setOrigin(_vec3_2); - + rotation = transformb.getRotation(); rotation.setEulerZYX( -details.axisb.z, -details.axisb.y, -details.axisb.x ); transformb.setRotation( rotation ); - + constraint = new Ammo.btGeneric6DofConstraint( _objects[ details.objecta ], _objects[ details.objectb ], @@ -872,15 +872,15 @@ public_functions.addConstraint = function ( details ) { } Ammo.destroy(transforma); if (transformb != undefined) { - Ammo.destroy(transformb); + Ammo.destroy(transformb); } break; - + default: return; - + }; - + world.addConstraint( constraint ); constraint.enableFeedback(); @@ -914,7 +914,7 @@ public_functions.constraint_setBreakingImpulseThreshold = function( details ) { public_functions.simulate = function simulate( params ) { if ( world ) { params = params || {}; - + if ( !params.timeStep ) { if ( last_simulation_time ) { params.timeStep = 0; @@ -934,12 +934,12 @@ public_functions.simulate = function simulate( params ) { last_simulation_duration = Date.now(); world.stepSimulation( params.timeStep, params.maxSubSteps, fixedTimeStep ); - + reportVehicles(); reportCollisions(); reportConstraints(); reportWorld(); - + last_simulation_duration = ( Date.now() - last_simulation_duration ) / 1000; last_simulation_time = Date.now(); } @@ -969,7 +969,7 @@ public_functions.slider_setLimits = function( params ) { var constraint = _constraints[ params.constraint ]; constraint.setLowerLinLimit( params.lin_lower || 0 ); constraint.setUpperLinLimit( params.lin_upper || 0 ); - + constraint.setLowerAngLimit( params.ang_lower || 0 ); constraint.setUpperAngLimit( params.ang_upper || 0 ); }; @@ -984,7 +984,7 @@ public_functions.slider_enableLinearMotor = function( params ) { constraint.setMaxLinMotorForce( params.acceleration ); constraint.setPoweredLinMotor( true ); constraint.getRigidBodyA().activate(); - if ( constraint.getRigidBodyB ) { + if ( constraint.getRigidBodyB() ) { constraint.getRigidBodyB().activate(); } }; @@ -1031,14 +1031,14 @@ public_functions.conetwist_setMaxMotorImpulse = function( params ) { }; public_functions.conetwist_setMotorTarget = function( params ) { var constraint = _constraints[ params.constraint ]; - + _quat.setX(params.x); _quat.setY(params.y); _quat.setZ(params.z); _quat.setW(params.w); - + constraint.setMotorTarget(_quat); - + constraint.getRigidBodyA().activate(); constraint.getRigidBodyB().activate(); }; @@ -1051,13 +1051,13 @@ public_functions.conetwist_disableMotor = function( params ) { public_functions.dof_setLinearLowerLimit = function( params ) { var constraint = _constraints[ params.constraint ]; - + _vec3_1.setX(params.x); _vec3_1.setY(params.y); _vec3_1.setZ(params.z); - + constraint.setLinearLowerLimit(_vec3_1); - + constraint.getRigidBodyA().activate(); if ( constraint.getRigidBodyB() ) { constraint.getRigidBodyB().activate(); @@ -1065,11 +1065,11 @@ public_functions.dof_setLinearLowerLimit = function( params ) { }; public_functions.dof_setLinearUpperLimit = function( params ) { var constraint = _constraints[ params.constraint ]; - + _vec3_1.setX(params.x); _vec3_1.setY(params.y); _vec3_1.setZ(params.z); - + constraint.setLinearUpperLimit(_vec3_1); constraint.getRigidBodyA().activate(); @@ -1079,27 +1079,27 @@ public_functions.dof_setLinearUpperLimit = function( params ) { }; public_functions.dof_setAngularLowerLimit = function( params ) { var constraint = _constraints[ params.constraint ]; - + _vec3_1.setX(params.x); _vec3_1.setY(params.y); _vec3_1.setZ(params.z); - + constraint.setAngularLowerLimit(_vec3_1); - + constraint.getRigidBodyA().activate(); - if ( constraint.getRigidBodyB() ) { + if ( constraint.getRigidBodyB() ) { constraint.getRigidBodyB().activate(); } }; public_functions.dof_setAngularUpperLimit = function( params ) { var constraint = _constraints[ params.constraint ]; - + _vec3_1.setX(params.x); _vec3_1.setY(params.y); _vec3_1.setZ(params.z); - + constraint.setAngularUpperLimit(_vec3_1); - + constraint.getRigidBodyA().activate(); if ( constraint.getRigidBodyB() ) { constraint.getRigidBodyB().activate(); @@ -1107,10 +1107,10 @@ public_functions.dof_setAngularUpperLimit = function( params ) { }; public_functions.dof_enableAngularMotor = function( params ) { var constraint = _constraints[ params.constraint ]; - + var motor = constraint.getRotationalLimitMotor( params.which ); motor.set_m_enableMotor( true ); - + constraint.getRigidBodyA().activate(); if ( constraint.getRigidBodyB() ) { constraint.getRigidBodyB().activate(); @@ -1118,14 +1118,14 @@ public_functions.dof_enableAngularMotor = function( params ) { }; public_functions.dof_configureAngularMotor = function( params ) { var constraint = _constraints[ params.constraint ]; - + var motor = constraint.getRotationalLimitMotor( params.which ); - + motor.set_m_loLimit( params.low_angle ); motor.set_m_hiLimit( params.high_angle ); motor.set_m_targetVelocity( params.velocity ); motor.set_m_maxMotorForce( params.max_force ); - + constraint.getRigidBodyA().activate(); if ( constraint.getRigidBodyB() ) { constraint.getRigidBodyB().activate(); @@ -1133,10 +1133,10 @@ public_functions.dof_configureAngularMotor = function( params ) { }; public_functions.dof_disableAngularMotor = function( params ) { var constraint = _constraints[ params.constraint ]; - + var motor = constraint.getRotationalLimitMotor( params.which ); motor.set_m_enableMotor( false ); - + constraint.getRigidBodyA().activate(); if ( constraint.getRigidBodyB() ) { constraint.getRigidBodyB().activate(); @@ -1145,10 +1145,10 @@ public_functions.dof_disableAngularMotor = function( params ) { reportWorld = function() { var index, object, - transform, origin, rotation, + transform, origin, rotation, offset = 0, i = 0; - + if ( SUPPORT_TRANSFERABLE ) { if ( worldreport.length < 2 + _num_objects * WORLDREPORT_ITEMSIZE ) { worldreport = new Float32Array( @@ -1158,55 +1158,55 @@ reportWorld = function() { worldreport[0] = MESSAGE_TYPES.WORLDREPORT; } } - + worldreport[1] = _num_objects; // record how many objects we're reporting on //for ( i = 0; i < worldreport[1]; i++ ) { for ( index in _objects ) { if ( _objects.hasOwnProperty( index ) ) { object = _objects[index]; - + // #TODO: we can't use center of mass transform when center of mass can change, // but getMotionState().getWorldTransform() screws up on objects that have been moved //object.getMotionState().getWorldTransform( transform ); - transform = object.getCenterOfMassTransform(); - - origin = transform.getOrigin(); + transform = object.getCenterOfMassTransform(); + + origin = transform.getOrigin(); rotation = transform.getRotation(); - + // add values to report offset = 2 + (i++) * WORLDREPORT_ITEMSIZE; - + worldreport[ offset ] = object.id; - + worldreport[ offset + 1 ] = origin.x(); worldreport[ offset + 2 ] = origin.y(); worldreport[ offset + 3 ] = origin.z(); - + worldreport[ offset + 4 ] = rotation.x(); worldreport[ offset + 5 ] = rotation.y(); worldreport[ offset + 6 ] = rotation.z(); worldreport[ offset + 7 ] = rotation.w(); - + _vector = object.getLinearVelocity(); worldreport[ offset + 8 ] = _vector.x(); worldreport[ offset + 9 ] = _vector.y(); worldreport[ offset + 10 ] = _vector.z(); - + _vector = object.getAngularVelocity(); worldreport[ offset + 11 ] = _vector.x(); worldreport[ offset + 12 ] = _vector.y(); worldreport[ offset + 13 ] = _vector.z(); } } - - + + if ( SUPPORT_TRANSFERABLE ) { transferableMessage( worldreport.buffer, [worldreport.buffer] ); } else { transferableMessage( worldreport ); } - + }; reportCollisions = function() { @@ -1215,7 +1215,7 @@ reportCollisions = function() { num = dp.getNumManifolds(), manifold, num_contacts, j, pt, _collided = false; - + if ( SUPPORT_TRANSFERABLE ) { if ( collisionreport.length < 2 + num * COLLISIONREPORT_ITEMSIZE ) { collisionreport = new Float32Array( @@ -1225,17 +1225,17 @@ reportCollisions = function() { collisionreport[0] = MESSAGE_TYPES.COLLISIONREPORT; } } - + collisionreport[1] = 0; // how many collisions we're reporting on - + for ( i = 0; i < num; i++ ) { manifold = dp.getManifoldByIndexInternal( i ); - + num_contacts = manifold.getNumContacts(); if ( num_contacts === 0 ) { continue; } - + for ( j = 0; j < num_contacts; j++ ) { pt = manifold.getContactPoint( j ); //if ( pt.getDistance() < 0 ) { @@ -1249,13 +1249,13 @@ reportCollisions = function() { collisionreport[ offset + 4 ] = _vector.z(); break; //} - - transferableMessage( _objects_ammo ); - - } + + transferableMessage( _objects_ammo ); + + } } - - + + if ( SUPPORT_TRANSFERABLE ) { transferableMessage( collisionreport.buffer, [collisionreport.buffer] ); } else { @@ -1265,7 +1265,7 @@ reportCollisions = function() { reportVehicles = function() { var index, vehicle, - transform, origin, rotation, + transform, origin, rotation, offset = 0, i = 0, j = 0; @@ -1288,10 +1288,10 @@ reportVehicles = function() { //vehicle.updateWheelTransform( j, true ); //transform = vehicle.getWheelTransformWS( j ); - transform = vehicle.getWheelInfo( j ).get_m_worldTransform(); + transform = vehicle.getWheelInfo( j ).get_m_worldTransform(); - origin = transform.getOrigin(); - rotation = transform.getRotation(); + origin = transform.getOrigin(); + rotation = transform.getRotation(); // add values to report offset = 1 + (i++) * VEHICLEREPORT_ITEMSIZE; @@ -1312,7 +1312,7 @@ reportVehicles = function() { } } - + if ( j !== 0 ) { if ( SUPPORT_TRANSFERABLE ) { transferableMessage( vehiclereport.buffer, [vehiclereport.buffer] ); @@ -1325,7 +1325,7 @@ reportVehicles = function() { reportConstraints = function() { var index, constraint, offset_body, - transform, origin, + transform, origin, offset = 0, i = 0; @@ -1343,7 +1343,7 @@ reportConstraints = function() { if ( _constraints.hasOwnProperty( index ) ) { constraint = _constraints[index]; offset_body = constraint.getRigidBodyA(); - transform = constraint.getFrameOffsetA(); + transform = constraint.getFrameOffsetA(); origin = transform.getOrigin(); // add values to report @@ -1358,7 +1358,7 @@ reportConstraints = function() { } } - + if ( i !== 0 ) { if ( SUPPORT_TRANSFERABLE ) { transferableMessage( constraintreport.buffer, [constraintreport.buffer] ); @@ -1366,14 +1366,14 @@ reportConstraints = function() { transferableMessage( constraintreport ); } } - + }; self.onmessage = function( event ) { - + if ( event.data instanceof Float32Array ) { // transferable object - + switch ( event.data[0] ) { case MESSAGE_TYPES.WORLDREPORT: worldreport = new Float32Array( event.data ); @@ -1391,13 +1391,13 @@ self.onmessage = function( event ) { constraintreport = new Float32Array( event.data ); break; } - + return; } - + if ( event.data.cmd && public_functions[event.data.cmd] ) { //if ( event.data.params.id !== undefined && _objects[event.data.params.id] === undefined && event.data.cmd !== 'addObject' && event.data.cmd !== 'registerMaterial' ) return; public_functions[event.data.cmd]( event.data.params ); } - + };