cylinder shape

beta.r3js.org
-=yb4f310 2017-09-02 12:55:57 +02:00
parent 7055891a84
commit 1c81b517b8
7 changed files with 327 additions and 105 deletions

View File

@ -139,7 +139,7 @@ GameLib.Component.COMPONENT_SHAPE_BOX = 0x29;
GameLib.Component.COMPONENT_SHAPE_SPHERE = 0x2a;
GameLib.Component.COMPONENT_SHAPE_TRI_MESH = 0x2b;
GameLib.Component.COMPONENT_SHAPE_CONVEX_HULL = 0x2c;
GameLib.Component.COMPONENT_SHAPE_CYLINDER = 0x2d;
GameLib.Component.COMPONENT_SHAPE_CONVEX_HULL_CYLINDER = 0x2d;
GameLib.Component.COMPONENT_SHAPE_HEIGHT_MAP = 0x2e;
GameLib.Component.COMPONENT_SHAPE_PLANE = 0x2f;
GameLib.Component.COMPONENT_CONTROLS = 0x30;
@ -202,7 +202,7 @@ GameLib.Component.GetComponentName = function(number) {
case 0x2a : return 'GameLib.D3.Shape.Sphere';
case 0x2b : return 'GameLib.D3.Shape.TriMesh';
case 0x2c : return 'GameLib.D3.Shape.ConvexHull';
case 0x2d : return 'GameLib.D3.Shape.Cylinder';
case 0x2d : return 'GameLib.D3.Shape.ConvexHull.Cylinder';
case 0x2e : return 'GameLib.D3.Shape.HeightMap';
case 0x2f : return 'GameLib.D3.Shape.Plane';
case 0x30 : return 'GameLib.D3.Controls';

View File

@ -2,12 +2,20 @@
* Raw Shape API object - should always correspond with the Shape Schema
* @param id
* @param name
* @param boundingSphereRadius
* @param collisionResponse
* @param frictionMaterial
* @param parentMesh
* @param parentEntity
* @constructor
*/
GameLib.D3.API.Shape = function(
id,
name,
boundingSphereRadius,
collisionResponse,
frictionMaterial,
parentMesh,
parentEntity
) {
@ -21,6 +29,26 @@ GameLib.D3.API.Shape = function(
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(boundingSphereRadius)) {
boundingSphereRadius = 0;
}
this.boundingSphereRadius = boundingSphereRadius;
if (GameLib.Utils.UndefinedOrNull(collisionResponse)) {
collisionResponse = true;
}
this.collisionResponse = collisionResponse;
if (GameLib.Utils.UndefinedOrNull(frictionMaterial)) {
frictionMaterial = null;
}
this.frictionMaterial = frictionMaterial;
if (GameLib.Utils.UndefinedOrNull(parentMesh)) {
parentMesh = null;
}
this.parentMesh = parentMesh;
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
}
@ -39,6 +67,10 @@ GameLib.D3.API.Shape.FromObject = function(objectShape) {
return new GameLib.D3.API.Shape(
objectShape.id,
objectShape.name,
objectShape.boundingSphereRadius,
objectShape.collisionResponse,
objectShape.frictionMaterial,
objectShape.parentMesh,
objectShape.parentEntity
);
};

View File

@ -23,16 +23,21 @@ GameLib.D3.Shape = function (
this,
apiShape.id,
apiShape.name,
apiShape.boundingSphereRadius,
apiShape.collisionResponse,
apiShape.frictionMaterial,
apiShape.parentMesh,
apiShape.parentEntity
);
var componentType = GameLib.Component.COMPONENT_SHAPE;
var linkedObjects = {};
var linkedObjects = {
parentMesh : GameLib.D3.Mesh
};
if (this instanceof GameLib.D3.Shape.Box) {
componentType = GameLib.Component.COMPONENT_SHAPE_BOX;
linkedObjects.parentMesh = GameLib.D3.Mesh;
}
if (this instanceof GameLib.D3.Shape.Sphere) {
@ -52,7 +57,7 @@ GameLib.D3.Shape = function (
}
if (this instanceof GameLib.D3.Shape.Cylinder) {
componentType = GameLib.Component.COMPONENT_SHAPE_CYLINDER;
componentType = GameLib.Component.COMPONENT_SHAPE_CONVEX_HULL_CYLINDER;
}
if (this instanceof GameLib.D3.Shape.ConvexHull) {
@ -96,6 +101,10 @@ GameLib.D3.Shape.prototype.toApiObject = function() {
var apiShape = new GameLib.D3.API.Shape(
this.id,
this.name,
this.boundingSphereRadius,
this.collisionResponse,
GameLib.Utils.IdOrNull(this.frictionMaterial),
GameLib.Utils.IdOrNull(this.parentMesh),
GameLib.Utils.IdOrNull(this.parentEntity)
);

View File

@ -3,14 +3,12 @@
* @param physics
* @param apiShape GameLib.D3.API.Shape
* @param halfExtents
* @param parentMesh
* @constructor
*/
GameLib.D3.Shape.Box = function (
physics,
apiShape,
halfExtents,
parentMesh
halfExtents
) {
this.physics = physics;
this.physics.isNotCannonThrow();
@ -31,11 +29,6 @@ GameLib.D3.Shape.Box = function (
}
this.halfExtents = halfExtents;
if (GameLib.Utils.UndefinedOrNull(parentMesh)) {
parentMesh = null;
}
this.parentMesh = parentMesh;
GameLib.D3.Shape.call(
this,
this.physics,
@ -67,16 +60,11 @@ GameLib.D3.Shape.Box.prototype.updateInstance = function() {
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;
};
@ -102,13 +90,11 @@ 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
apiShape.halfExtents
);
};

View File

@ -0,0 +1,114 @@
/**
* 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 radiusTop
* @param radiusBottom
* @param height
* @param numSegments
* @constructor
*/
GameLib.D3.Shape.ConvexHull.Cylinder = function (
physics,
apiShape,
radiusTop,
radiusBottom,
height,
numSegments
) {
this.physics = physics;
this.physics.isNotCannonThrow();
if (GameLib.Utils.UndefinedOrNull(radiusTop)) {
radiusTop = 1;
}
this.radiusTop = radiusTop;
if (GameLib.Utils.UndefinedOrNull(radiusBottom)) {
radiusBottom = 1;
}
this.radiusBottom = radiusBottom;
if (GameLib.Utils.UndefinedOrNull(height)) {
height = 1;
}
this.height = height;
if (GameLib.Utils.UndefinedOrNull(numSegments)) {
numSegments = 1;
}
this.numSegments = numSegments;
GameLib.D3.Shape.ConvexHull.call(
this,
this.physics,
apiShape
);
};
GameLib.D3.Shape.ConvexHull.Cylinder.prototype = Object.create(GameLib.D3.Shape.ConvexHull.prototype);
GameLib.D3.Shape.ConvexHull.Cylinder.prototype.constructor = GameLib.D3.Shape.ConvexHull.Cylinder;
/**
*
* @returns {GameLib.D3.Shape.Cylinder|*|SEA3D.Cylinder}
*/
GameLib.D3.Shape.ConvexHull.Cylinder.prototype.createInstance = function() {
var instance = new CANNON.Cylinder(
this.radiusTop,
this.radiusBottom,
this.height,
this.numSegments
);
return instance;
};
GameLib.D3.Shape.ConvexHull.Cylinder.prototype.updateInstance = function() {
console.log('todo : update cylinder instance');
// this.instance.radius = this.radius;
// this.instance.updateAABB();
// this.instance.updateBoundingCylinderRadius();
// this.instance.updateEdges();
// this.instance.updateNormals();
// this.instance.updateTree();
};
GameLib.D3.Shape.ConvexHull.Cylinder.prototype.setFromMesh = function() {
console.log('todo: set from mesh');
};
GameLib.D3.Shape.ConvexHull.Cylinder.prototype.toApiObject = function() {
var apiShape = GameLib.D3.Shape.ConvexHull.prototype.toApiObject.call(this);
apiShape.radiusTop = this.radiusTop;
apiShape.radiusBottom = this.radiusBottom;
apiShape.height = this.height;
apiShape.numSegments = this.numSegments;
return apiShape;
};
GameLib.D3.Shape.ConvexHull.Cylinder.FromObject = function(physics, objectShape) {
var apiShape = GameLib.D3.Shape.ConvexHull.FromObject(objectShape);
var radiusTop = objectShape.radiusTop;
var radiusBottom = objectShape.radiusBottom;
var height = objectShape.height;
var numSegments = objectShape.numSegments;
return new GameLib.D3.Shape.ConvexHull.Cylinder(
physics,
apiShape,
radiusTop,
radiusBottom,
height,
numSegments
);
};

View File

@ -2,29 +2,85 @@
* 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 points
* @param faces
* @param uniqueAxes
* @param uniqueEdges
* @param vertices
* @constructor
*/
GameLib.D3.Shape.ConvexHull = function (
physics,
apiShape,
points,
faces
vertices,
faces,
uniqueAxes,
uniqueEdges
) {
this.physics = physics;
this.physics.isNotCannonThrow();
if (GameLib.Utils.UndefinedOrNull(points)) {
points = [];
if (GameLib.Utils.UndefinedOrNull(vertices)) {
vertices = [];
}
this.points = points;
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,
@ -41,13 +97,20 @@ GameLib.D3.Shape.ConvexHull.prototype.constructor = GameLib.D3.Shape.ConvexHull;
*/
GameLib.D3.Shape.ConvexHull.prototype.createInstance = function() {
var faceNormals = [];
var instance = new CANNON.ConvexPolyhedron(
this.points.map(function(point){
return point.instance;
this.vertices.map(function(vertex){
return vertex.position.instance;
}),
this.faces
this.faces.map(function(face){
faceNormals.push(face.normal.instance);
return [face.v0index, face.v1index, face.v2index]
})
);
instance.faceNormals = faceNormals;
return instance;
};
@ -55,6 +118,7 @@ GameLib.D3.Shape.ConvexHull.prototype.createInstance = function() {
* 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();
@ -62,4 +126,96 @@ GameLib.D3.Shape.ConvexHull.prototype.updateInstance = function() {
// 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.FromObject = function(physics, objectShape) {
var apiShape = GameLib.D3.API.Shape.FromObject(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.Vector3.FromObject(axis);
}
);
var uniqueEdges = objectShape.uniqueEdges.map(
function(edge) {
return GameLib.Vector3.FromObject(edge);
}
);
return new GameLib.D3.Shape.ConvexHull(
physics,
apiShape,
vertices,
faces,
uniqueAxes,
uniqueEdges
);
};

View File

@ -1,75 +0,0 @@
/**
* 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 radiusTop
* @param radiusBottom
* @param height
* @param numSegments
* @constructor
*/
GameLib.D3.Shape.Cylinder = function (
physics,
apiShape,
radiusTop,
radiusBottom,
height,
numSegments
) {
this.physics = physics;
this.physics.isNotCannonThrow();
if (GameLib.Utils.UndefinedOrNull(radiusTop)) {
radiusTop = 1;
}
this.radiusTop = radiusTop;
if (GameLib.Utils.UndefinedOrNull(radiusBottom)) {
radiusBottom = 1;
}
this.radiusBottom = radiusBottom;
if (GameLib.Utils.UndefinedOrNull(height)) {
height = 1;
}
this.height = height;
if (GameLib.Utils.UndefinedOrNull(numSegments)) {
numSegments = 1;
}
this.numSegments = numSegments;
GameLib.D3.Shape.call(
this,
this.physics,
apiShape
);
};
GameLib.D3.Shape.Cylinder.prototype = Object.create(GameLib.D3.Shape.prototype);
GameLib.D3.Shape.Cylinder.prototype.constructor = GameLib.D3.Shape.Cylinder;
/**
*
* @returns {GameLib.D3.Shape.Cylinder|*|SEA3D.Cylinder}
*/
GameLib.D3.Shape.Cylinder.prototype.createInstance = function() {
var instance = new CANNON.Cylinder(
this.radiusTop,
this.radiusBottom,
this.height,
this.numSegments
);
return instance;
};
GameLib.D3.Shape.Cylinder.prototype.updateInstance = function() {
// this.instance.radius = this.radius;
// this.instance.updateAABB();
// this.instance.updateBoundingCylinderRadius();
// this.instance.updateEdges();
// this.instance.updateNormals();
// this.instance.updateTree();
};