Skip to content

Commit

Permalink
Fix for false positive on sphere/heightfield bail schteppe#265
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Yau committed May 28, 2021
1 parent c5eb05b commit 4e16d1b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/world/Narrowphase.js
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ Narrowphase.prototype.sphereHeightfield = function (
iMaxY = Math.ceil((localSpherePos.y + radius) / w) + 1;

// Bail out if we are out of the terrain
if(iMaxX < 0 || iMaxY < 0 || iMinX > data.length || iMaxY > data[0].length){
if(iMaxX < 0 || iMaxY < 0 || iMinX > data.length || iMinY > data[0].length) {
return;
}

Expand Down
68 changes: 46 additions & 22 deletions test/Narrowphase.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var Vec3 = require("../src/math/Vec3");
var Quaternion = require("../src/math/Quaternion");
var Box = require('../src/shapes/Box');
var Heightfield = require('../src/shapes/heightfield');
var Heightfield = require('../src/shapes/Heightfield');
var Narrowphase = require('../src/world/Narrowphase');
var Sphere = require('../src/shapes/Sphere');
var Body = require('../src/objects/Body');
Expand Down Expand Up @@ -39,31 +39,55 @@ module.exports = {
test.done();
},

sphereHeightfield : function(test){
var world = new World();
var cg = new Narrowphase(world);
var result = [];
var hfShape = createHeightfield();
var sphereShape = new Sphere(0.1);
cg.currentContactMaterial = new ContactMaterial();
cg.result = result;
cg.sphereHeightfield(
sphereShape,
hfShape,
new Vec3(0.25, 0.25, 0.05), // hit the first triangle in the field
new Vec3(0, 0, 0),
new Quaternion(),
new Quaternion(),
new Body(1, sphereShape),
new Body(1, hfShape)
);
sphereHeightfield : {
setUp : function (cb) {
var world = new World();
this.cg = new Narrowphase(world);
this.cg.currentContactMaterial = new ContactMaterial();
cb();
},

test.equal(result.length, 1);
test1 : function(test){
var hfShape = createHeightfield();
var sphereShape = new Sphere(0.1);
var cg = this.cg;
cg.sphereHeightfield(
sphereShape,
hfShape,
new Vec3(0.25, 0.25, 0.05), // hit the first triangle in the field
new Vec3(0, 0, 0),
new Quaternion(),
new Quaternion(),
new Body(1, sphereShape),
new Body(1, hfShape)
);

test.done();
},
test.equal(cg.result.length, 1);

test.done();
},

// Make sure the early iMinY bail works.
test2 : function(test){
var hfShape = createHeightfield();
var sphereShape = new Sphere(0.1);
var cg = this.cg;
cg.sphereHeightfield(
sphereShape,
hfShape,
new Vec3(18.75, 18.95, 0.05), // hit the last triangle in the field
new Vec3(0, 0, 0),
new Quaternion(),
new Quaternion(),
new Body(1, sphereShape),
new Body(1, hfShape)
);

test.equal(cg.result.length, 1);

test.done();
},
},
};

function createHeightfield(){
Expand Down

0 comments on commit 4e16d1b

Please sign in to comment.