/** * 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 faces * @param uniqueAxes * @param uniqueEdges * @param vertices * @constructor */ GameLib.D3.Shape.ConvexHull = function ( physics, apiShape, vertices, faces, uniqueAxes, uniqueEdges ) { this.physics = physics; this.physics.isNotCannonThrow(); if (GameLib.Utils.UndefinedOrNull(apiShape)) { apiShape = {}; } if (apiShape instanceof GameLib.D3.Shape.ConvexHull) { return apiShape; } if (GameLib.Utils.UndefinedOrNull(vertices)) { vertices = []; } this.vertices = vertices; if (GameLib.Utils.UndefinedOrNull(faces)) { faces = []; } this.faces = faces; if (GameLib.Utils.UndefinedOrNull(uniqueAxes)) { uniqueAxes = []; } this.uniqueAxes = uniqueAxes; if (GameLib.Utils.UndefinedOrNull(uniqueEdges)) { uniqueEdges = []; } this.uniqueEdges = uniqueEdges; this.vertices = this.vertices.map(function(vertex){ if (vertex instanceof GameLib.D3.API.Vertex){ return new GameLib.D3.Vertex( this.physics, vertex ) } return vertex; }.bind(this)); this.faces = this.faces.map(function(face){ if (face instanceof GameLib.D3.API.Face){ return new GameLib.D3.Face( this.physics, face ) } return face; }.bind(this)); this.uniqueAxes = this.uniqueAxes.map(function(axis){ if (axis instanceof GameLib.API.Vector3) { return new GameLib.Vector3( this.physics, axis, this ) } return axis; }.bind(this)); this.uniqueEdges = this.uniqueEdges.map(function(edge){ if (edge instanceof GameLib.API.Vector3) { return new GameLib.Vector3( this.physics, edge, this ) } return edge; }.bind(this)); GameLib.D3.Shape.call( this, this.physics, apiShape ); }; GameLib.D3.Shape.ConvexHull.prototype = Object.create(GameLib.D3.Shape.prototype); GameLib.D3.Shape.ConvexHull.prototype.constructor = GameLib.D3.Shape.ConvexHull; /** * Create instance * @returns {GameLib.D3.Shape.ConvexHull} */ GameLib.D3.Shape.ConvexHull.prototype.createInstance = function() { var faceNormals = []; var instance = new CANNON.ConvexPolyhedron( this.vertices.map(function(vertex){ return vertex.position.instance; }), this.faces.map(function(face){ faceNormals.push(face.normal.instance); return [face.v0index, face.v1index, face.v2index] }) ); instance.faceNormals = faceNormals; return instance; }; /** * Update instance */ GameLib.D3.Shape.ConvexHull.prototype.updateInstance = function() { console.log('todo: update convex hull instance'); // this.instance.vertices = this.vertices; // this.instance.indices = this.indices; // this.instance.updateAABB(); // this.instance.updateBoundingSphereRadius(); // this.instance.updateEdges(); // this.instance.updateNormals(); // this.instance.updateTree(); }; GameLib.D3.Shape.ConvexHull.prototype.loadFromInstance = function() { console.log('todo: eventually load the faces and vertices from the instance faces and vertices and normals'); console.log('todo: this way we can nicely visualize them with our gamelib classes :)'); }; GameLib.D3.Shape.ConvexHull.prototype.toApiObject = function() { var apiShape = GameLib.D3.Shape.prototype.toApiObject.call(this); apiShape.vertices = this.vertices.map( function(vertex) { if (vertex instanceof GameLib.D3.Vertex) { return vertex.toApiObject(); } return vertex; } ); apiShape.faces = this.faces.map( function(face) { if (face instanceof GameLib.D3.Face){ return face.toApiObject(); } return face; } ); apiShape.uniqueAxes = this.uniqueAxes.map( function(axis){ if (axis instanceof GameLib.Vector3) { return axis.toApiObject(); } return axis; } ); apiShape.uniqueEdges = this.uniqueEdges.map( function(edge) { if (edge instanceof GameLib.Vector3) { return edge.toApiObject(); } return edge; } ); return apiShape; }; GameLib.D3.Shape.ConvexHull.prototype.setFromMesh = function() { console.log('todo: set convex hull from mesh'); this.updateInstance(); }; GameLib.D3.Shape.ConvexHull.InheritableProperties = function(physics, objectShape) { var vertices = objectShape.vertices.map( function(objectVertex) { return GameLib.D3.Vertex.FromObject(physics, objectVertex); } ); var faces = objectShape.faces.map( function(objectFace) { return GameLib.D3.Face.FromObject(physics, objectFace); } ); var uniqueAxes = objectShape.uniqueAxes.map( function(axis) { return GameLib.API.Vector3.FromObject(axis); } ); var uniqueEdges = objectShape.uniqueEdges.map( function(edge) { return GameLib.API.Vector3.FromObject(edge); } ); return { vertices : vertices, faces : faces, uniqueAxes : uniqueAxes, uniqueEdges : uniqueEdges }; }; GameLib.D3.Shape.ConvexHull.FromObject = function(physics, objectShape) { var apiShape = GameLib.D3.API.Shape.FromObject(objectShape); var inheritableProperties = GameLib.D3.Shape.ConvexHull.InheritableProperties(physics, objectShape); return new GameLib.D3.Shape.ConvexHull.call( this, physics, apiShape, inheritableProperties.vertices, inheritableProperties.faces, inheritableProperties.uniqueAxes, inheritableProperties.uniqueEdges ); };