r3-legacy/src/game-lib-d3-shape-convex-hu...

240 lines
6.1 KiB
JavaScript
Raw Normal View History

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();
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-06-24 02:42:28 +02:00
var instance = new CANNON.ConvexPolyhedron(
2017-09-02 12:55:57 +02:00
this.vertices.map(function(vertex){
return vertex.position.instance;
2017-06-24 02:42:28 +02:00
}),
2017-09-02 12:55:57 +02:00
this.faces.map(function(face){
faceNormals.push(face.normal.instance);
return [face.v0index, face.v1index, face.v2index]
})
2017-06-24 02:42:28 +02:00
);
2017-09-02 12:55:57 +02:00
instance.faceNormals = faceNormals;
2017-06-24 02:42:28 +02:00
return instance;
};
/**
* 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();
};
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) {
return GameLib.API.Vector3.FromObject(axis);
2017-09-02 12:55:57 +02:00
}
);
var uniqueEdges = objectShape.uniqueEdges.map(
function(edge) {
return GameLib.API.Vector3.FromObject(edge);
2017-09-02 12:55:57 +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,
inheritableProperties.vertices,
inheritableProperties.faces,
inheritableProperties.uniqueAxes,
inheritableProperties.uniqueEdges
2017-09-02 12:55:57 +02:00
);
2017-06-24 02:42:28 +02:00
};