Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map for shape overlaps #283 #285

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/objects/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ Body.prototype.addShape = function(shape, _offset, _orientation){

shape.body = this;

// If the body is already in the world, add the shape to the map now.
if(this.world){
this.world.idToShapeMap[shape.id] = shape;
}

return this;
};

Expand Down
15 changes: 15 additions & 0 deletions src/shapes/Heightfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,25 @@ Heightfield.prototype.getConvexTrianglePillar = function(xi, yi, getUpperTriangl
}

result = new ConvexPolyhedron();
// Add shape to shape map. This is a special case for
// heightfields. Normally this happens in
// addShape/addBody. world may be null for some tests.
var world = this.body && this.body.world
if(world){
world.idToShapeMap[result.id] = result;
}
// Assign body so the shape overlap events will have it.
result.body = this.body;
offsetResult = new Vec3();

this.pillarConvex = result;
this.pillarOffset = offsetResult;
} else {
// If the cache is not used, make the pillar map to our shape.
if(!result.body){
this.body.world.idToShapeMap[result.id] = this;
result.body = this.body;
}
}

var data = this.data;
Expand Down
39 changes: 28 additions & 11 deletions src/world/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,18 @@ function World(options){
body : null
};

/**
* @property idToBodyMap
* @type {Object} Map of ids to bodies for bodyOverlapKeeper.
*/
this.idToBodyMap = {};

/**
* @property idToShapeMap
* @type {Object} Map of ids to shapes for shapeOverlapKeeper.
*/
this.idToShapeMap = {};

this.broadphase.setWorld(this);
}
World.prototype = new EventTarget();
Expand Down Expand Up @@ -302,6 +312,18 @@ World.prototype.add = World.prototype.addBody = function(body){
this.collisionMatrix.setNumObjects(this.bodies.length);
this.addBodyEvent.body = body;
this.idToBodyMap[body.id] = body;
// Add all shapes to the shape map.
var shapes = body.shapes,
n = shapes.length;
for(var i=0;i!==n; i++){
var s = shapes[i];
this.idToShapeMap[s.id] = s;
// Special case for Box. Use the creating shape.
var c = s.convexPolyhedronRepresentation;
if (c) {
this.idToShapeMap[c.id] = s;
}
}
this.dispatchEvent(this.addBodyEvent);
};

Expand Down Expand Up @@ -433,6 +455,11 @@ World.prototype.remove = function(body){
this.collisionMatrix.setNumObjects(n);
this.removeBodyEvent.body = body;
delete this.idToBodyMap[body.id];
var shapes = this.shapes,
sn = shapes && shapes.length || 0;
for(i=0; i!==sn; i++){
delete this.idToShapeMap[shapes[i].id];
}
this.dispatchEvent(this.removeBodyEvent);
}
};
Expand All @@ -448,18 +475,8 @@ World.prototype.getBodyById = function(id){
return this.idToBodyMap[id];
};

// TODO Make a faster map
World.prototype.getShapeById = function(id){
var bodies = this.bodies;
for(var i=0, bl = bodies.length; i<bl; i++){
var shapes = bodies[i].shapes;
for (var j = 0, sl = shapes.length; j < sl; j++) {
var shape = shapes[j];
if(shape.id === id){
return shape;
}
}
}
return this.idToShapeMap[id];
};

/**
Expand Down