2016-10-14 12:32:53 +02:00
|
|
|
/**
|
|
|
|
* Physics Shape Superset
|
2016-10-28 11:47:50 +02:00
|
|
|
* @param engine GameLib.D3.Engine
|
|
|
|
* @param shapeType
|
|
|
|
* @param scale GameLib.D3.Vector3
|
|
|
|
* @param vertices Number[]
|
|
|
|
* @param indices Number[]
|
|
|
|
* @param radius Number
|
|
|
|
* @param halfExtensions GameLib.D3.Vector3
|
|
|
|
* @param radiusTop Number
|
|
|
|
* @param radiusBottom Number
|
|
|
|
* @param height Number
|
|
|
|
* @param numSegments Number
|
|
|
|
* @param heightmap GameLib.D3.Heightmap
|
|
|
|
* @param elementSize
|
2016-10-14 12:32:53 +02:00
|
|
|
* @constructor
|
|
|
|
*/
|
2016-10-28 11:47:50 +02:00
|
|
|
GameLib.D3.Shape = function(
|
|
|
|
engine,
|
|
|
|
shapeType,
|
|
|
|
scale,
|
|
|
|
vertices,
|
|
|
|
indices,
|
|
|
|
radius,
|
|
|
|
halfExtensions,
|
|
|
|
radiusTop,
|
|
|
|
radiusBottom,
|
|
|
|
height,
|
|
|
|
numSegments,
|
|
|
|
heightmap
|
2016-10-14 12:32:53 +02:00
|
|
|
) {
|
2016-11-01 16:08:22 +01:00
|
|
|
|
2016-10-28 11:47:50 +02:00
|
|
|
this.engine = engine;
|
|
|
|
|
|
|
|
this.engine.isNotCannonThrow();
|
|
|
|
|
2016-10-14 12:32:53 +02:00
|
|
|
this.shapeType = shapeType;
|
2016-10-28 11:47:50 +02:00
|
|
|
|
|
|
|
if (typeof scale == 'undefined') {
|
|
|
|
scale = new GameLib.D3.Vector3(1, 1, 1)
|
|
|
|
}
|
|
|
|
this.scale = scale;
|
|
|
|
|
|
|
|
if (typeof vertices == 'undefined') {
|
|
|
|
vertices = [];
|
|
|
|
}
|
|
|
|
this.vertices = vertices;
|
|
|
|
|
|
|
|
if (typeof indices == 'undefined') {
|
|
|
|
indices = [];
|
|
|
|
}
|
|
|
|
this.indices = indices;
|
|
|
|
|
|
|
|
if (typeof radius == 'undefined') {
|
|
|
|
radius = 1;
|
|
|
|
}
|
|
|
|
this.radius = radius;
|
|
|
|
|
|
|
|
if (typeof halfExtensions == 'undefined') {
|
|
|
|
halfExtensions = new GameLib.D3.Vector3(1,1,1);
|
|
|
|
}
|
|
|
|
this.halfExtensions = halfExtensions;
|
|
|
|
|
|
|
|
if (typeof radiusTop == 'undefined') {
|
|
|
|
radiusTop = 1;
|
|
|
|
}
|
|
|
|
this.radiusTop = radiusTop;
|
|
|
|
|
|
|
|
if (typeof radiusBottom == 'undefined') {
|
|
|
|
radiusBottom = 1;
|
|
|
|
}
|
|
|
|
this.radiusBottom = radiusBottom;
|
|
|
|
|
|
|
|
if (typeof height == 'undefined') {
|
|
|
|
height = 1;
|
|
|
|
}
|
|
|
|
this.height = height;
|
|
|
|
|
|
|
|
if (typeof numSegments == 'undefined') {
|
|
|
|
numSegments = 1;
|
|
|
|
}
|
|
|
|
this.numSegments = numSegments;
|
|
|
|
|
|
|
|
if (typeof heightmap == 'undefined') {
|
|
|
|
heightmap = new GameLib.D3.Heightmap();
|
|
|
|
}
|
|
|
|
this.heightmap = heightmap;
|
|
|
|
|
2016-11-01 16:08:22 +01:00
|
|
|
this.instance = this.createInstance();
|
2016-10-28 11:47:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shape constants
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
GameLib.D3.Shape.SHAPE_TYPE_SPHERE = 1;
|
|
|
|
GameLib.D3.Shape.SHAPE_TYPE_BOX = 2;
|
|
|
|
GameLib.D3.Shape.SHAPE_TYPE_TRIMESH = 3;
|
|
|
|
GameLib.D3.Shape.SHAPE_TYPE_CYLINDER = 4;
|
|
|
|
GameLib.D3.Shape.SHAPE_TYPE_HEIGHT_MAP = 5;
|
|
|
|
GameLib.D3.Shape.SHAPE_TYPE_CONVEX_HULL = 6;
|
2016-11-10 11:09:55 +01:00
|
|
|
GameLib.D3.Shape.SHAPE_TYPE_PLANE = 7;
|
2016-10-28 11:47:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
GameLib.D3.Shape.prototype.createInstance = function() {
|
|
|
|
|
|
|
|
var instance = null;
|
|
|
|
|
|
|
|
if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_TRIMESH) {
|
|
|
|
instance = new this.engine.instance.Trimesh(
|
|
|
|
this.vertices,
|
|
|
|
this.indices
|
|
|
|
);
|
|
|
|
} else if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_SPHERE) {
|
|
|
|
instance = new this.engine.instance.Sphere(
|
|
|
|
this.radius
|
|
|
|
);
|
|
|
|
} else if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_BOX) {
|
|
|
|
instance = new this.engine.instance.Box(
|
|
|
|
new this.engine.instance.Vec3(
|
|
|
|
this.halfExtensions.x,
|
|
|
|
this.halfExtensions.y,
|
|
|
|
this.halfExtensions.z
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_CYLINDER) {
|
|
|
|
instance = new this.engine.instance.Cylinder(
|
|
|
|
this.radiusTop,
|
|
|
|
this.radiusBottom,
|
|
|
|
this.height,
|
|
|
|
this.numSegments
|
|
|
|
);
|
|
|
|
} else if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_HEIGHT_MAP) {
|
|
|
|
instance = new this.engine.instance.Heightfield(
|
|
|
|
this.heightmap.matrix,
|
|
|
|
{
|
|
|
|
elementSize: this.heightmap.elementSize
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_CONVEX_HULL) {
|
2016-11-08 11:13:55 +01:00
|
|
|
|
|
|
|
instance = new this.engine.instance.ConvexPolyhedron(
|
|
|
|
this.vertices, this.indices
|
|
|
|
);
|
|
|
|
|
2016-11-10 11:09:55 +01:00
|
|
|
} else if(this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_PLANE) {
|
|
|
|
instance = new this.engine.instance.Plane();
|
2016-10-28 11:47:50 +02:00
|
|
|
} else {
|
|
|
|
console.warn('Shape type not implemented: ' + this.shapeType);
|
|
|
|
throw new Error('Shape type not implemented: ' + this.shapeType);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.instance = instance;
|
|
|
|
|
|
|
|
return instance;
|
2016-10-14 12:32:53 +02:00
|
|
|
};
|
|
|
|
|
2016-10-28 11:47:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* update
|
|
|
|
*/
|
|
|
|
GameLib.D3.Shape.prototype.update = function(
|
|
|
|
engine
|
|
|
|
) {
|
|
|
|
engine.isNotCannonThrow();
|
|
|
|
|
|
|
|
if(this.shapeType === GameLib.D3.Shape.SHAPE_TYPE_TRIMESH) {
|
|
|
|
this.instance.setScale(
|
|
|
|
new engine.instance.Vec3(
|
|
|
|
this.scale.x,
|
|
|
|
this.scale.y,
|
|
|
|
this.scale.z
|
|
|
|
)
|
|
|
|
);
|
|
|
|
this.instance.updateAABB();
|
|
|
|
this.instance.updateNormals();
|
|
|
|
this.instance.updateEdges();
|
|
|
|
this.instance.updateBoundingSphereRadius();
|
|
|
|
this.instance.updateTree();
|
2016-10-14 12:32:53 +02:00
|
|
|
}
|
|
|
|
};
|