/** * Shape Superset - The apiShape properties get moved into the Shape object itself, and then the instance is created * @param physics * @param apiShape GameLib.D3.API.Shape * @param halfExtents * @param parentMesh * @constructor */ GameLib.D3.Shape.Box = function ( physics, apiShape, halfExtents, parentMesh ) { this.physics = physics; this.physics.isNotCannonThrow(); if (GameLib.Utils.UndefinedOrNull(halfExtents)) { halfExtents = new GameLib.Vector3( physics, new GameLib.API.Vector3( 1,1,1 ) ); } else if (halfExtents instanceof GameLib.API.Vector3) { halfExtents = new GameLib.Vector3( this.physics, halfExtents, this ) } this.halfExtents = halfExtents; if (GameLib.Utils.UndefinedOrNull(parentMesh)) { parentMesh = null; } this.parentMesh = parentMesh; GameLib.D3.Shape.call( this, this.physics, apiShape ); }; GameLib.D3.Shape.Box.prototype = Object.create(GameLib.D3.Shape.prototype); GameLib.D3.Shape.Box.prototype.constructor = GameLib.D3.Shape.Box; /** * * @returns {GameLib.D3.Shape.Box|*|SEA3D.Box} */ GameLib.D3.Shape.Box.prototype.createInstance = function() { var instance = new CANNON.Box( this.halfExtents.instance ); return instance; }; GameLib.D3.Shape.Box.prototype.updateInstance = function() { this.instance.halfExtents.x = this.halfExtents.x; this.instance.halfExtents.y = this.halfExtents.y; this.instance.halfExtents.z = this.halfExtents.z; this.instance.updateBoundingSphereRadius(); this.instance.updateConvexPolyhedronRepresentation(); }; GameLib.D3.Shape.prototype.visualize = function() { }; GameLib.D3.Shape.Box.prototype.toApiObject = function() { var apiShape = GameLib.D3.Shape.prototype.toApiObject.call(this); apiShape.halfExtents = this.halfExtents.toApiObject(); apiShape.parentMesh = GameLib.Utils.IdOrNull(this.parentMesh); return apiShape; }; GameLib.D3.Shape.Box.prototype.setFromMesh = function() { if (this.parentMesh === null) { console.log('select a mesh first'); return; } var box = this.parentMesh.getBoundingBox(); this.halfExtents.x = box.x / 2; this.halfExtents.y = box.y / 2; this.halfExtents.z = box.z / 2; this.halfExtents.updateInstance(); }; GameLib.D3.Shape.Box.FromObject = function(physics, objectShape) { var apiShape = GameLib.D3.API.Shape.FromObject(objectShape); apiShape.halfExtents = GameLib.API.Vector3.FromObject(objectShape.halfExtents); apiShape.parentMesh = objectShape.parentMesh; return new GameLib.D3.Shape.Box( physics, apiShape, apiShape.halfExtents, apiShape.parentMesh ); };