/** * RigidBody Runtime * @param physics GameLib.D3.Graphics * @param apiRigidBody GameLib.D3.API.RigidBody * @constructor */ GameLib.D3.RigidBody = function ( physics, apiRigidBody ) { this.physics = physics; this.physics.isNotCannonThrow(); if (GameLib.Utils.UndefinedOrNull(apiRigidBody)) { apiRigidBody = {}; } if (apiRigidBody instanceof GameLib.D3.RigidBody) { return apiRigidBody; } GameLib.D3.API.RigidBody.call( this, apiRigidBody.id, apiRigidBody.name, apiRigidBody.mass, apiRigidBody.friction, apiRigidBody.position, apiRigidBody.quaternion, apiRigidBody.velocity, apiRigidBody.angularVelocity, apiRigidBody.linearDamping, apiRigidBody.angularDamping, apiRigidBody.allowSleep, apiRigidBody.sleepSpeedLimit, apiRigidBody.sleepTimeLimit, apiRigidBody.collisionFilterGroup, apiRigidBody.collisionFilterMask, apiRigidBody.fixedRotation, apiRigidBody.shapes, apiRigidBody.kinematic, apiRigidBody.parentEntity ); GameLib.Component.call( this, GameLib.Component.COMPONENT_RIGID_BODY, { 'shapes' : [GameLib.D3.Shape] } ); }; GameLib.D3.RigidBody.prototype = Object.create(GameLib.D3.API.RigidBody.prototype); GameLib.D3.RigidBody.prototype.constructor = GameLib.D3.RigidBody; /** * * @returns {*} */ GameLib.D3.RigidBody.prototype.createInstance = function() { //TODO: create instance var instance = new CANNON.Body( { mass : this.mass, friction : this.friction, position : new CANNON.Vec3( this.position.x, this.position.y, this.position.z ), quaternion : new CANNON.Quaternion( this.quaternion.x, this.quaternion.y, this.quaternion.z, this.quaternion.w ), velocity : new CANNON.Vec3( this.velocity.x, this.velocity.y, this.velocity.z ), angularVelocity : this.angularVelocity, linearDamping : this.linearDamping, angularDamping : this.angularDamping, allowSleep : this.allowSleep, sleepSpeedLimit : this.sleepSpeedLimit, sleepTimeLimit : this.sleepTimeLimit, collisionFilterGroup : this.collisionFilterGroup, collisionFilterMask : this.collisionFilterMask, fixedRotation : this.fixedRotation, kinematic : this.kinematic } ); this.shapes.map(function(shape){ if (shape.loaded) { instance.addShape(shape.instance) } else { console.warn('todo : listen for shape created and add here'); throw new Error('todo : listen for shape created and add here'); } }); return instance; }; /** * */ GameLib.D3.RigidBody.prototype.updateInstance = function() { this.instance.mass = this.mass; this.instance.friction = this.friction; this.instance.position.x = this.position.x; this.instance.position.y = this.position.y; this.instance.position.z = this.position.z; this.instance.quaternion.x = this.quaternion.x; this.instance.quaternion.y = this.quaternion.y; this.instance.quaternion.z = this.quaternion.z; this.instance.quaternion.w = this.quaternion.w; this.instance.velocity.x = this.velocity.x; this.instance.velocity.y = this.velocity.y; this.instance.velocity.z = this.velocity.z; this.instance.angularVelocity = this.angularVelocity; this.instance.linearDamping = this.linearDamping; this.instance.angularDamping = this.angularDamping; this.instance.allowSleep = this.allowSleep; this.instance.sleepSpeedLimit = this.sleepSpeedLimit; this.instance.sleepTimeLimit = this.sleepTimeLimit; this.instance.collisionFilterGroup = this.collisionFilterGroup; this.instance.collisionFilterMask = this.collisionFilterMask; this.instance.fixedRotation = this.fixedRotation; this.instance.kinematic = this.kinematic; }; /** * GameLib.D3.RigidBody to GameLib.D3.API.RigidBody * @returns {GameLib.D3.API.RigidBody} */ GameLib.D3.RigidBody.prototype.toApiObject = function() { var apiRigidBody = new GameLib.D3.API.RigidBody( this.id, this.name, this.mass, this.friction, this.position, this.quaternion, this.velocity, this.angularVelocity, this.linearDamping, this.angularDamping, this.allowSleep, this.sleepSpeedLimit, this.sleepTimeLimit, this.collisionFilterGroup, this.collisionFilterMask, this.fixedRotation, this.shapes.map(function(shape){return GameLib.Utils.IdOrNull(shape)}), this.kinematic, GameLib.Utils.IdOrNull(this.parentEntity) ); return apiRigidBody; }; /** * GameLib.D3.RigidBody from Object RigidBody * @param physics * @param objectComponent * @returns {GameLib.D3.RigidBody} * @constructor */ GameLib.D3.RigidBody.FromObject = function(physics, objectComponent) { var apiRigidBody = GameLib.D3.API.RigidBody.FromObject(objectComponent); return new GameLib.D3.RigidBody( physics, apiRigidBody ); };