Skip to content

Commit

Permalink
#6 OBB vs OBB collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaspietravallo committed Jul 21, 2022
1 parent cbb1dd7 commit 799c9f7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
4 changes: 2 additions & 2 deletions coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{"total": {"lines":{"total":1056,"covered":974,"skipped":0,"pct":92.23},"statements":{"total":1144,"covered":1055,"skipped":0,"pct":92.22},"functions":{"total":244,"covered":228,"skipped":0,"pct":93.44},"branches":{"total":629,"covered":519,"skipped":0,"pct":82.51},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"}}
,"/Users/tomaspietravallo/Documents/GitHub/sparkar-volts/volts.ts": {"lines":{"total":1056,"covered":974,"skipped":0,"pct":92.23},"functions":{"total":244,"covered":228,"skipped":0,"pct":93.44},"statements":{"total":1144,"covered":1055,"skipped":0,"pct":92.22},"branches":{"total":629,"covered":519,"skipped":0,"pct":82.51}}
{"total": {"lines":{"total":1070,"covered":988,"skipped":0,"pct":92.33},"statements":{"total":1160,"covered":1071,"skipped":0,"pct":92.32},"functions":{"total":245,"covered":229,"skipped":0,"pct":93.46},"branches":{"total":633,"covered":525,"skipped":0,"pct":82.93},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"}}
,"/Users/tomaspietravallo/Documents/GitHub/sparkar-volts/volts.ts": {"lines":{"total":1070,"covered":988,"skipped":0,"pct":92.33},"functions":{"total":245,"covered":229,"skipped":0,"pct":93.46},"statements":{"total":1160,"covered":1071,"skipped":0,"pct":92.32},"branches":{"total":633,"covered":525,"skipped":0,"pct":82.93}}
}
32 changes: 31 additions & 1 deletion tests/colliders.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Matrix, OBB, Vector } from "../volts";
import { Matrix, OBB, Quaternion, Vector } from "../volts";

describe('OBB - Oriented Bounding Box', () => {
test('constructor', () => {
Expand All @@ -22,4 +22,34 @@ describe('OBB - Oriented Bounding Box', () => {
expect(interval.max).toBeCloseTo(+1);
expect(interval.min).toBeCloseTo(-1);
})
test('againstOBB', () => {
const OBB1 = new OBB({ position: new Vector(0,0,0), size: new Vector(0.5) });
const OBB2 = new OBB({ position: new Vector(0,0,1), size: new Vector(0.5) });
// These two are barely touching
expect(OBB1.againstOBB(OBB2)).toEqual(true);
expect(OBB2.againstOBB(OBB1)).toEqual(true);

const OBB3 = new OBB({ position: new Vector(0,0,0), size: new Vector(0.499) });
const OBB4 = new OBB({ position: new Vector(0,0,1), size: new Vector(0.5) });
// These two not touching
expect(OBB3.againstOBB(OBB4)).toEqual(false);
expect(OBB4.againstOBB(OBB3)).toEqual(false);

const OBB5 = new OBB({ position: new Vector(0,0,0), size: new Vector(0.499) });
const OBB6 = new OBB({ position: new Vector(0,0,1), size: new Vector(0.5), orientation: Quaternion.fromEuler(0.3,0.3,0).toMatrix() });
// Because of the rotation these should be touching
expect(OBB5.againstOBB(OBB6)).toEqual(true);
expect(OBB6.againstOBB(OBB5)).toEqual(true);

// Test 10 Z axis rotations, all should have no effect
for (let index = 0; index < 10; index++) {
const orientation = Quaternion.fromEuler(0,0, (Math.PI * 2) * (index / 10) ).toMatrix();

const OBB1 = new OBB({ position: new Vector(0,0,0), size: new Vector(0.499) });
const OBB2 = new OBB({ position: new Vector(0,0,1), size: new Vector(0.5), orientation });
// Because of the rotation these should be touching
expect(OBB1.againstOBB(OBB2)).toEqual(false);
expect(OBB2.againstOBB(OBB1)).toEqual(false);
}
})
})
33 changes: 33 additions & 0 deletions volts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,39 @@ export class OBB {

return result;
}

/**
* @todo **Optimize**
*/
againstOBB(other: OBB) {
const to = this.orientation.values.flat();
const oo = other.orientation.values.flat();

const testAxis = [
new Vector(to[0], to[1], to[2]),
new Vector(to[3], to[4], to[5]),
new Vector(to[6], to[7], to[8]),
new Vector(oo[0], oo[1], oo[2]),
new Vector(oo[3], oo[4], oo[5]),
new Vector(oo[6], oo[7], oo[8])
];

for (let i = 0; i < 3; i++) { // Fill out rest of axis
testAxis[6 + i * 3 + 0] = testAxis[i].cross(testAxis[0]);
testAxis[6 + i * 3 + 1] = testAxis[i].cross(testAxis[1]);
testAxis[6 + i * 3 + 2] = testAxis[i].cross(testAxis[2]);
}

for (let i = 0; i < 15; i++) {
const a = this.getInterval(testAxis[i]);
const b = other.getInterval(testAxis[i]);
const overlaps = ((b.min <= a.max) && (a.min <= b.max));
if (!overlaps) {
return false;
}
}
return true
}
}
//#endregion

Expand Down

0 comments on commit 799c9f7

Please sign in to comment.