diff --git a/src/shapes/Box.js b/src/shapes/Box.js index 2313067ea..d1305cd95 100644 --- a/src/shapes/Box.js +++ b/src/shapes/Box.js @@ -13,9 +13,9 @@ var ConvexPolyhedron = require('./ConvexPolyhedron'); * @extends Shape */ function Box(halfExtents){ - Shape.call(this); - - this.type = Shape.types.BOX; + Shape.call(this, { + type: Shape.types.BOX + }); /** * @property halfExtents diff --git a/src/shapes/ConvexPolyhedron.js b/src/shapes/ConvexPolyhedron.js index 3980a5a61..841727350 100644 --- a/src/shapes/ConvexPolyhedron.js +++ b/src/shapes/ConvexPolyhedron.js @@ -25,9 +25,9 @@ var Transform = require('../math/Transform'); * @todo Automatically merge coplanar polygons in constructor. */ function ConvexPolyhedron(points, faces, uniqueAxes) { - var that = this; - Shape.call(this); - this.type = Shape.types.CONVEXPOLYHEDRON; + Shape.call(this, { + type: Shape.types.CONVEXPOLYHEDRON + }); /** * Array of Vec3 diff --git a/src/shapes/Cylinder.js b/src/shapes/Cylinder.js index e283fa971..39698be07 100644 --- a/src/shapes/Cylinder.js +++ b/src/shapes/Cylinder.js @@ -73,7 +73,6 @@ function Cylinder( radiusTop, radiusBottom, height , numSegments ) { } faces.push(temp); - this.type = Shape.types.CONVEXPOLYHEDRON; ConvexPolyhedron.call( this, verts, faces, axes ); } diff --git a/src/shapes/Heightfield.js b/src/shapes/Heightfield.js index 4605ccd24..94d6d0263 100644 --- a/src/shapes/Heightfield.js +++ b/src/shapes/Heightfield.js @@ -75,12 +75,13 @@ function Heightfield(data, options){ this.cacheEnabled = true; - Shape.call(this); + Shape.call(this, { + type: Shape.types.HEIGHTFIELD + }); this.pillarConvex = new ConvexPolyhedron(); this.pillarOffset = new Vec3(); - this.type = Shape.types.HEIGHTFIELD; this.updateBoundingSphereRadius(); // "i_j_isUpper" => { convex: ..., offset: ... } diff --git a/src/shapes/Particle.js b/src/shapes/Particle.js index 6e2ca50d6..5f38ef5b1 100644 --- a/src/shapes/Particle.js +++ b/src/shapes/Particle.js @@ -11,9 +11,9 @@ var Vec3 = require('../math/Vec3'); * @extends Shape */ function Particle(){ - Shape.call(this); - - this.type = Shape.types.PARTICLE; + Shape.call(this, { + type: Shape.types.PARTICLE + }); } Particle.prototype = new Shape(); Particle.prototype.constructor = Particle; diff --git a/src/shapes/Plane.js b/src/shapes/Plane.js index f2c538229..df046aa83 100644 --- a/src/shapes/Plane.js +++ b/src/shapes/Plane.js @@ -11,8 +11,9 @@ var Vec3 = require('../math/Vec3'); * @author schteppe */ function Plane(){ - Shape.call(this); - this.type = Shape.types.PLANE; + Shape.call(this, { + type: Shape.types.PLANE + }); // World oriented normal this.worldNormal = new Vec3(); diff --git a/src/shapes/Shape.js b/src/shapes/Shape.js index 09f7e6cc4..b64df4b68 100644 --- a/src/shapes/Shape.js +++ b/src/shapes/Shape.js @@ -9,10 +9,15 @@ var Material = require('../material/Material'); * Base class for shapes * @class Shape * @constructor + * @param {object} [options] + * @param {number} [options.collisionFilterGroup=1] + * @param {number} [options.collisionFilterMask=-1] + * @param {number} [options.collisionResponse=true] + * @param {number} [options.material=null] * @author schteppe - * @todo Should have a mechanism for caching bounding sphere radius instead of calculating it each time */ -function Shape(){ +function Shape(options){ + options = options || {}; /** * Identifyer of the Shape. @@ -26,7 +31,7 @@ function Shape(){ * @type {Number} * @see Shape.types */ - this.type = 0; + this.type = options.type || 0; /** * The local bounding sphere radius of this shape. @@ -38,12 +43,22 @@ function Shape(){ * Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled. * @property {boolean} collisionResponse */ - this.collisionResponse = true; + this.collisionResponse = options.collisionResponse ? options.collisionResponse : true; + + /** + * @property {Number} collisionFilterGroup + */ + this.collisionFilterGroup = options.collisionFilterGroup !== undefined ? options.collisionFilterGroup : 1; + + /** + * @property {Number} collisionFilterMask + */ + this.collisionFilterMask = options.collisionFilterMask !== undefined ? options.collisionFilterMask : -1; /** * @property {Material} material */ - this.material = null; + this.material = options.material ? options.material : null; /** * @property {Body} body diff --git a/src/shapes/Sphere.js b/src/shapes/Sphere.js index e411c7dbd..68658ea98 100644 --- a/src/shapes/Sphere.js +++ b/src/shapes/Sphere.js @@ -12,13 +12,14 @@ var Vec3 = require('../math/Vec3'); * @author schteppe / http://github.com/schteppe */ function Sphere(radius){ - Shape.call(this); + Shape.call(this, { + type: Shape.types.SPHERE + }); /** * @property {Number} radius */ - this.radius = radius!==undefined ? Number(radius) : 1.0; - this.type = Shape.types.SPHERE; + this.radius = radius !== undefined ? radius : 1.0; if(this.radius < 0){ throw new Error('The sphere radius cannot be negative.'); diff --git a/src/shapes/Trimesh.js b/src/shapes/Trimesh.js index a7b036cef..99e521a1b 100644 --- a/src/shapes/Trimesh.js +++ b/src/shapes/Trimesh.js @@ -26,8 +26,9 @@ var Octree = require('../utils/Octree'); * var trimeshShape = new Trimesh(vertices, indices); */ function Trimesh(vertices, indices) { - Shape.call(this); - this.type = Shape.types.TRIMESH; + Shape.call(this, { + type: Shape.types.TRIMESH + }); /** * @property vertices diff --git a/src/world/Narrowphase.js b/src/world/Narrowphase.js index cec997ce1..1d4bb462a 100644 --- a/src/world/Narrowphase.js +++ b/src/world/Narrowphase.js @@ -260,6 +260,10 @@ Narrowphase.prototype.getContacts = function(p1, p2, world, result, oldcontacts, xj.vadd(bj.position, xj); var sj = bj.shapes[j]; + if(!((si.collisionFilterMask & sj.collisionFilterGroup) && (sj.collisionFilterMask & si.collisionFilterGroup))){ + continue; + } + if(xi.distanceTo(xj) > si.boundingSphereRadius + sj.boundingSphereRadius){ continue; }