Skip to content

Commit

Permalink
added optional per-shape collision filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
schteppe committed Apr 28, 2016
1 parent d68c3a4 commit c7d1603
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/shapes/Box.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/shapes/ConvexPolyhedron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/shapes/Cylinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ function Cylinder( radiusTop, radiusBottom, height , numSegments ) {
}
faces.push(temp);

this.type = Shape.types.CONVEXPOLYHEDRON;
ConvexPolyhedron.call( this, verts, faces, axes );
}

Expand Down
5 changes: 3 additions & 2 deletions src/shapes/Heightfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ... }
Expand Down
6 changes: 3 additions & 3 deletions src/shapes/Particle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/shapes/Plane.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 20 additions & 5 deletions src/shapes/Shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/shapes/Sphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
5 changes: 3 additions & 2 deletions src/shapes/Trimesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/world/Narrowphase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit c7d1603

Please sign in to comment.