Skip to content

Commit

Permalink
hull/hull contact, something is still wrong though
Browse files Browse the repository at this point in the history
  • Loading branch information
schteppe committed May 9, 2012
1 parent 6503441 commit 4b0bf9b
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 14 deletions.
51 changes: 47 additions & 4 deletions build/cannon.demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ CANNON.Demo = function(){
paused:false,
rendermode:0,
contacts:false, // Contact points
cm2contact:false // center of mass to contact points
cm2contact:false, // center of mass to contact points
normals:false // contact normals
};

this._phys_bodies = [];
Expand All @@ -38,6 +39,7 @@ CANNON.Demo = function(){
this.timestep = 1.0/60.0;
this.shadowsOn = true;
this._contactmeshes = [];
this._normallines = [];
this._contactlines = [];

this.three_contactpoint_geo = new THREE.SphereGeometry( 0.1, 6, 6);
Expand Down Expand Up @@ -160,6 +162,7 @@ CANNON.Demo.prototype.updateVisuals = function(){
var sphere_geometry = this.three_contactpoint_geo;
var numadded = 0;
var old_meshes = this._contactmeshes;
var old_normal_meshes = this._normalmeshes;
this._contactmeshes = [];
for(var ci in this._world.contacts){
var c = this._world.contacts[ci];
Expand Down Expand Up @@ -257,6 +260,47 @@ CANNON.Demo.prototype.updateVisuals = function(){
this._scene.remove(this._contactlines[i]);

}

// Normal lines
if(this.settings.normals){
var old_lines = this._normallines;
this._normallines = [];
for(var ci in this._world.contacts){
var c = this._world.contacts[ci];
var bi=c.bi, bj=c.bj, mesh;
var i=bi.id, j=bj.id;
var line, geometry;
if(old_lines.length){
// Get mesh from prev timestep
line = old_lines.pop();
geometry = line.geometry;
geometry.vertices.pop();
geometry.vertices.pop();
} else {
// Create new mesh
geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(0,0,0));
geometry.vertices.push(new THREE.Vector3(1,1,1));
line = new THREE.Line( geometry, new THREE.LineBasicMaterial({color:0x00ff00}));
this._scene.add(line);
}
this._normallines.push(line);
var n = c.ni;
var b = bi;
line.scale.set(n.x,n.y,n.z);
b.position.copy(line.position);
c.ri.vadd(line.position,line.position);
this._scene.add(line);
}

// Remove overflowing
while(old_lines.length)
this._scene.remove(old_lines.pop());
} else if(this._normallines.length){
// Remove all contact lines
for(var i=0; i<this._normallines.length; i++)
this._scene.remove(this._normallines[i]);
}
};

/**
Expand Down Expand Up @@ -453,9 +497,8 @@ CANNON.Demo.prototype.start = function(){
rf.add(that.settings,'contacts').onChange(function(contacts){
// Do nothing... contacts are dynamically added/removed for each frame
});
rf.add(that.settings,'cm2contact').onChange(function(cm2contact){

});
rf.add(that.settings,'cm2contact').onChange(function(cm2contact){});
rf.add(that.settings,'normals').onChange(function(normals){});

// World folder
var wf = that._gui.addFolder('World');
Expand Down
29 changes: 27 additions & 2 deletions build/cannon.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ CANNON.NaiveBroadphase.prototype.collisionPairs = function(world){
continue;
}

// --- Box / sphere / compound collision ---
// --- Box / sphere / compound / hull collision ---
if((ti & BOX_SPHERE_COMPOUND_CONVEX) && (tj & BOX_SPHERE_COMPOUND_CONVEX)){

// Rel. position
Expand Down Expand Up @@ -3532,7 +3532,32 @@ CANNON.ContactGenerator = function(){
}

} else if(si.type==CANNON.Shape.types.CONVEXHULL){


if(sj.type==CANNON.Shape.types.CONVEXHULL){ // hull-hull
var sepAxis = new CANNON.Vec3();
if(si.findSeparatingAxis(sj,xi,qi,xj,qj,sepAxis)){
//console.log(sepAxis.toString());
var res = [];
si.clipAgainstHull(xi,qi,sj,xj,qj,sepAxis,-100,100,res);
for(var j=0; j<res.length; j++){
var r = makeResult(bi,bj);
sepAxis.negate(r.ni);
var q = new CANNON.Vec3();
res[j].normal.negate(q);
q.mult(res[j].depth,q);
r.ri.set(res[j].point.x + q.x*0.5,
res[j].point.y + q.y*0.5,
res[j].point.z + q.z*0.5);
r.rj.set(res[j].point.x - q.x*0.5,
res[j].point.y - q.y*0.5,
res[j].point.z - q.z*0.5);
// Contact points are in world coordinates. Transform back to relative
r.rj.vsub(xj,r.rj);
r.ri.vsub(xi,r.ri);
result.push(r);
}
}
}
}

// Swap back if we swapped bodies in the beginning
Expand Down
2 changes: 1 addition & 1 deletion build/cannon.min.js

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions demos/convexhull.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,49 @@
*/
var demo = new CANNON.Demo();

demo.addScene(function(app){
var world = setupWorld(app);

// ConvexHull box shape
var hullShape = new CANNON.ConvexHull();
// At the moment one must provide vertices, faces and normals..
var size = 2;
hullShape.addPoints([new CANNON.Vec3(-size,-size,-size),
new CANNON.Vec3( size,-size,-size),
new CANNON.Vec3( size, size,-size),
new CANNON.Vec3(-size, size,-size),
new CANNON.Vec3(-size,-size, size),
new CANNON.Vec3( size,-size, size),
new CANNON.Vec3( size, size, size),
new CANNON.Vec3(-size, size, size)],

// At the moment the convex hull can't resolve normals and faces by itself, so we need to help it. This should be changed in the future
[
[0,1,2,3], // -z
[4,5,6,7], // +z
[0,1,4,5], // -y
[2,3,6,7], // +y
[0,3,4,7], // -x
[1,2,5,6], // +x
],

[new CANNON.Vec3( 0, 0,-1),
new CANNON.Vec3( 0, 0, 1),
new CANNON.Vec3( 0,-1, 0),
new CANNON.Vec3( 0, 1, 0),
new CANNON.Vec3(-1, 0, 0),
new CANNON.Vec3( 1, 0, 0)]);
var mass = 10;
var boxbody1 = new CANNON.RigidBody(mass,hullShape);
var boxbody2 = new CANNON.RigidBody(mass,hullShape);
boxbody1.position.set(0,0,size+1);
boxbody2.position.set(1.5,0,4*size+1);
world.add(boxbody1);
world.add(boxbody2);
app.addVisual(boxbody1);
app.addVisual(boxbody2);
});

demo.addScene(function(app){
var world = setupWorld(app);

Expand Down
2 changes: 1 addition & 1 deletion libs/Three.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/collision/NaiveBroadphase.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ CANNON.NaiveBroadphase.prototype.collisionPairs = function(world){
continue;
}

// --- Box / sphere / compound collision ---
// --- Box / sphere / compound / hull collision ---
if((ti & BOX_SPHERE_COMPOUND_CONVEX) && (tj & BOX_SPHERE_COMPOUND_CONVEX)){

// Rel. position
Expand Down
51 changes: 47 additions & 4 deletions src/demo/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ CANNON.Demo = function(){
paused:false,
rendermode:0,
contacts:false, // Contact points
cm2contact:false // center of mass to contact points
cm2contact:false, // center of mass to contact points
normals:false // contact normals
};

this._phys_bodies = [];
Expand All @@ -38,6 +39,7 @@ CANNON.Demo = function(){
this.timestep = 1.0/60.0;
this.shadowsOn = true;
this._contactmeshes = [];
this._normallines = [];
this._contactlines = [];

this.three_contactpoint_geo = new THREE.SphereGeometry( 0.1, 6, 6);
Expand Down Expand Up @@ -160,6 +162,7 @@ CANNON.Demo.prototype.updateVisuals = function(){
var sphere_geometry = this.three_contactpoint_geo;
var numadded = 0;
var old_meshes = this._contactmeshes;
var old_normal_meshes = this._normalmeshes;
this._contactmeshes = [];
for(var ci in this._world.contacts){
var c = this._world.contacts[ci];
Expand Down Expand Up @@ -257,6 +260,47 @@ CANNON.Demo.prototype.updateVisuals = function(){
this._scene.remove(this._contactlines[i]);

}

// Normal lines
if(this.settings.normals){
var old_lines = this._normallines;
this._normallines = [];
for(var ci in this._world.contacts){
var c = this._world.contacts[ci];
var bi=c.bi, bj=c.bj, mesh;
var i=bi.id, j=bj.id;
var line, geometry;
if(old_lines.length){
// Get mesh from prev timestep
line = old_lines.pop();
geometry = line.geometry;
geometry.vertices.pop();
geometry.vertices.pop();
} else {
// Create new mesh
geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(0,0,0));
geometry.vertices.push(new THREE.Vector3(1,1,1));
line = new THREE.Line( geometry, new THREE.LineBasicMaterial({color:0x00ff00}));
this._scene.add(line);
}
this._normallines.push(line);
var n = c.ni;
var b = bi;
line.scale.set(n.x,n.y,n.z);
b.position.copy(line.position);
c.ri.vadd(line.position,line.position);
this._scene.add(line);
}

// Remove overflowing
while(old_lines.length)
this._scene.remove(old_lines.pop());
} else if(this._normallines.length){
// Remove all contact lines
for(var i=0; i<this._normallines.length; i++)
this._scene.remove(this._normallines[i]);
}
};

/**
Expand Down Expand Up @@ -453,9 +497,8 @@ CANNON.Demo.prototype.start = function(){
rf.add(that.settings,'contacts').onChange(function(contacts){
// Do nothing... contacts are dynamically added/removed for each frame
});
rf.add(that.settings,'cm2contact').onChange(function(cm2contact){

});
rf.add(that.settings,'cm2contact').onChange(function(cm2contact){});
rf.add(that.settings,'normals').onChange(function(normals){});

// World folder
var wf = that._gui.addFolder('World');
Expand Down
27 changes: 26 additions & 1 deletion src/world/ContactGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,32 @@ CANNON.ContactGenerator = function(){
}

} else if(si.type==CANNON.Shape.types.CONVEXHULL){


if(sj.type==CANNON.Shape.types.CONVEXHULL){ // hull-hull
var sepAxis = new CANNON.Vec3();
if(si.findSeparatingAxis(sj,xi,qi,xj,qj,sepAxis)){
//console.log(sepAxis.toString());
var res = [];
si.clipAgainstHull(xi,qi,sj,xj,qj,sepAxis,-100,100,res);
for(var j=0; j<res.length; j++){
var r = makeResult(bi,bj);
sepAxis.negate(r.ni);
var q = new CANNON.Vec3();
res[j].normal.negate(q);
q.mult(res[j].depth,q);
r.ri.set(res[j].point.x + q.x*0.5,
res[j].point.y + q.y*0.5,
res[j].point.z + q.z*0.5);
r.rj.set(res[j].point.x - q.x*0.5,
res[j].point.y - q.y*0.5,
res[j].point.z - q.z*0.5);
// Contact points are in world coordinates. Transform back to relative
r.rj.vsub(xj,r.rj);
r.ri.vsub(xi,r.ri);
result.push(r);
}
}
}
}

// Swap back if we swapped bodies in the beginning
Expand Down

0 comments on commit 4b0bf9b

Please sign in to comment.