2017-06-24 02:42:28 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2017-09-02 12:55:57 +02:00
|
|
|
* @param uniqueAxes
|
|
|
|
* @param uniqueEdges
|
|
|
|
* @param vertices
|
2017-06-24 02:42:28 +02:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
GameLib.D3.Shape.ConvexHull = function (
|
|
|
|
physics,
|
|
|
|
apiShape,
|
2017-09-02 12:55:57 +02:00
|
|
|
vertices,
|
|
|
|
faces,
|
|
|
|
uniqueAxes,
|
|
|
|
uniqueEdges
|
2017-06-24 02:42:28 +02:00
|
|
|
) {
|
|
|
|
this.physics = physics;
|
|
|
|
this.physics.isNotCannonThrow();
|
|
|
|
|
2017-09-03 15:49:27 +02:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiShape)) {
|
|
|
|
apiShape = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiShape instanceof GameLib.D3.Shape.ConvexHull) {
|
|
|
|
return apiShape;
|
|
|
|
}
|
|
|
|
|
2017-09-02 12:55:57 +02:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(vertices)) {
|
|
|
|
vertices = [];
|
2017-06-24 02:42:28 +02:00
|
|
|
}
|
2017-09-02 12:55:57 +02:00
|
|
|
this.vertices = vertices;
|
2017-06-24 02:42:28 +02:00
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(faces)) {
|
|
|
|
faces = [];
|
|
|
|
}
|
|
|
|
this.faces = faces;
|
|
|
|
|
2017-09-02 12:55:57 +02:00
|
|
|
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));
|
|
|
|
|
2017-06-24 02:42:28 +02:00
|
|
|
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() {
|
|
|
|
|
2017-09-02 12:55:57 +02:00
|
|
|
var faceNormals = [];
|
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance = new CANNON.ConvexPolyhedron(
|
|
|
|
this.vertices.map(
|
|
|
|
function(vertex) {
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(vertex)) {
|
|
|
|
throw new Error('no vertex');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(vertex.position)) {
|
|
|
|
throw new Error('no vertex position');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(vertex.position.instance)) {
|
|
|
|
throw new Error('no vertex position instance');
|
|
|
|
}
|
|
|
|
|
|
|
|
return vertex.position.instance;
|
|
|
|
}
|
|
|
|
),
|
|
|
|
this.faces.map(
|
|
|
|
function(face) {
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(face)) {
|
|
|
|
throw new Error('no face');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(face.normal)) {
|
|
|
|
throw new Error('no face normal');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(face.normal.instance)) {
|
|
|
|
throw new Error('no face normal instance');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(face.v0index)) {
|
|
|
|
throw new Error('no face v0index');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(face.v1index)) {
|
|
|
|
throw new Error('no face v1index');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(face.v2index)) {
|
|
|
|
throw new Error('no face v2index');
|
|
|
|
}
|
|
|
|
|
|
|
|
faceNormals.push(face.normal.instance);
|
|
|
|
|
|
|
|
return [face.v0index, face.v1index, face.v2index];
|
|
|
|
}
|
|
|
|
)
|
2017-06-24 02:42:28 +02:00
|
|
|
);
|
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
this.instance.faceNormals = faceNormals;
|
2017-09-02 12:55:57 +02:00
|
|
|
|
2017-10-23 14:52:35 +02:00
|
|
|
GameLib.D3.Shape.prototype.createInstance.call(this);
|
2017-06-24 02:42:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update instance
|
|
|
|
*/
|
|
|
|
GameLib.D3.Shape.ConvexHull.prototype.updateInstance = function() {
|
2017-09-02 12:55:57 +02:00
|
|
|
console.log('todo: update convex hull instance');
|
2017-06-24 02:42:28 +02:00
|
|
|
// 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();
|
2017-09-02 12:55:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2017-09-03 15:49:27 +02:00
|
|
|
GameLib.D3.Shape.ConvexHull.InheritableProperties = function(physics, objectShape) {
|
2017-09-02 12:55:57 +02:00
|
|
|
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) {
|
2017-09-03 15:49:27 +02:00
|
|
|
return GameLib.API.Vector3.FromObject(axis);
|
2017-09-02 12:55:57 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
var uniqueEdges = objectShape.uniqueEdges.map(
|
|
|
|
function(edge) {
|
2017-09-03 15:49:27 +02:00
|
|
|
return GameLib.API.Vector3.FromObject(edge);
|
2017-09-02 12:55:57 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-09-03 15:49:27 +02:00
|
|
|
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,
|
2017-09-02 12:55:57 +02:00
|
|
|
physics,
|
|
|
|
apiShape,
|
2017-09-03 15:49:27 +02:00
|
|
|
inheritableProperties.vertices,
|
|
|
|
inheritableProperties.faces,
|
|
|
|
inheritableProperties.uniqueAxes,
|
|
|
|
inheritableProperties.uniqueEdges
|
2017-09-02 12:55:57 +02:00
|
|
|
);
|
2017-06-24 02:42:28 +02:00
|
|
|
};
|