/** * System takes care of updating all the entities (based on their component data) * @param apiSystem R3.API.System * @constructor */ R3.System.Physics = function( apiSystem ) { R3.System.call( this, apiSystem ); this.worlds = []; // this.rigidBodies = []; // this.wheels = []; // this.vehicles = []; // this.worldSubscription = null; // this.rigidBodySubscription = null; this.beforeRenderSubscription = null; this.afterRenderSubscription = null; }; R3.System.Physics.prototype = Object.create(R3.System.prototype); R3.System.Physics.prototype.constructor = R3.System.Physics; R3.System.Physics.prototype.start = function() { R3.System.prototype.start.call(this); this.worlds = R3.EntityManager.Instance.queryComponents(R3.Component.PHYSICS_WORLD); // this.rigidBodies = R3.EntityManager.Instance.queryComponents(R3.Component.RIGID_BODY); // this.wheels = R3.EntityManager.Instance.queryComponents(R3.Component.RAYCAST_WHEEL); // this.vehicles = R3.EntityManager.Instance.queryComponents(R3.Component.RAYCAST_VEHICLE); // this.worlds.map( // function(world) { // world.instance.addEventListener( // 'postStep', // function() { // // this.vehicles.map( // function(vehicle) { // vehicle.instance.wheelInfos.map( // function(wheelInfo, index) { // vehicle.instance.updateWheelTransform(index); // var t = wheelInfo.worldTransform; // // vehicle.wheels[index].instance.position.copy(t.position); // // vehicle.wheels[index].instance.quaternion.copy(t.quaternion); // // // vehicle.raycastWheels[index].parentMesh.localPosition.x = t.position.x; // // vehicle.raycastWheels[index].parentMesh.localPosition.y = t.position.y; // // vehicle.raycastWheels[index].parentMesh.localPosition.z = t.position.z; // // // vehicle.raycastWheels[index].parentMesh.updateInstance(); // } // ); // } // ); // // // }.bind(this) // ) // }.bind(this) // ); this.beforeRenderSubscription = this.subscribe( R3.Event.BEFORE_RENDER, this.beforeRender ); }; /** * Update script */ R3.System.Physics.prototype.beforeRender = function(data) { this.worlds.map( function(world) { if (world.instance) { world.instance.step(data.delta); world.rigidBodies.map( function(rigidBody){ rigidBody.position.x = rigidBody.instance.position.x; rigidBody.position.y = rigidBody.instance.position.y; rigidBody.position.z = rigidBody.instance.position.z; rigidBody.quaternion.x = rigidBody.instance.quaternion.x; rigidBody.quaternion.y = rigidBody.instance.quaternion.y; rigidBody.quaternion.z = rigidBody.instance.quaternion.z; rigidBody.quaternion.w = rigidBody.instance.quaternion.w; rigidBody.parentMesh.position.x = rigidBody.instance.position.x; rigidBody.parentMesh.position.y = rigidBody.instance.position.y; rigidBody.parentMesh.position.z = rigidBody.instance.position.z; rigidBody.parentMesh.quaternion.x = rigidBody.instance.quaternion.x; rigidBody.parentMesh.quaternion.y = rigidBody.instance.quaternion.y; rigidBody.parentMesh.quaternion.z = rigidBody.instance.quaternion.z; rigidBody.parentMesh.quaternion.w = rigidBody.instance.quaternion.w; rigidBody.instance.getVelocityAtWorldPoint(new CANNON.Vec3(0,0,0), rigidBody.velocity.instance); rigidBody.velocity.x = rigidBody.velocity.instance.x; rigidBody.velocity.y = rigidBody.velocity.instance.y; rigidBody.velocity.z = rigidBody.velocity.instance.z; rigidBody.parentMesh.updateRotationFromAxisAngle = false; rigidBody.parentMesh.updateInstance(); rigidBody.parentMesh.updateRotationFromAxisAngle = true; } ) } }.bind(this) ); }; R3.System.Physics.prototype.stop = function() { R3.System.prototype.stop.call(this); this.worlds = []; this.rigidBodies = []; this.wheels = []; this.vehicles = []; if (this.beforeRenderSubscription) { this.beforeRenderSubscription.remove(); } if (this.afterRenderSubscription) { this.afterRenderSubscription.remove(); } };