UNPKG

separating-axis-test

Version:

test for the intersection of convex polytopes in 2d or 3d, computing the minimum translation vector

138 lines (134 loc) 3.33 kB
var sat3d = require('../3d.js') var test = require('tape') test('axis-aligned boxes 3d', function (t) { t.deepEqual(sat3d( [], { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [0,0,0],[0,1,0],[1,1,0],[1,0,0], [0,0,1],[0,1,1],[1,1,1],[1,0,1] ] }, { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [-0.5,0.4,0],[0.5,0.4,0],[0.5,-0.6,0],[-0.5,-0.6,0], [-0.5,0.4,1],[0.5,0.4,1],[0.5,-0.6,1],[-0.5,-0.6,1] ] } ), [0,0.4,0]) t.deepEqual(sat3d( [], { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [0,0,0],[0,1,0],[1,1,0],[1,0,0], [0,0,1],[0,1,1],[1,1,1],[1,0,1] ] }, { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [-0.6,0.5,0],[0.4,0.5,0],[0.4,-0.5,0],[-0.6,-0.5,0], [-0.6,0.5,1],[0.4,0.5,1],[0.4,-0.5,1],[-0.6,-0.5,1] ] } ), [0.4,0,0]) t.deepEqual(sat3d( [], { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [0,0,0],[0,1,0],[1,1,0],[1,0,0], [0,0,1],[0,1,1],[1,1,1],[1,0,1] ] }, { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [-0.4,1.5,0],[0.6,1.5,0],[0.6,0.5,0],[-0.4,0.5,0], [-0.4,1.5,1],[0.6,1.5,1],[0.6,0.5,1],[-0.4,0.5,1] ] } ), [0,-0.5,0]) t.deepEqual(sat3d( [], { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [0,0,0],[0,1,0],[1,1,0],[1,0,0], [0,0,1],[0,1,1],[1,1,1],[1,0,1] ] }, { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [-0.5,1.6,0],[0.5,1.6,0],[0.5,0.6,0],[-0.5,0.6,0], [-0.5,1.6,1],[0.5,1.6,1],[0.5,0.6,1],[-0.5,0.6,1] ] } ), [0,-0.4,0]) t.notOk(sat3d( [], { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [0,1,0],[1,1,0],[1,0,0],[0,0,0], [0,1,1],[1,1,1],[1,0,1],[0,0,1] ] }, { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [-0.5,-0.5,0],[0.5,-0.5,0],[0.5,-1.5,0],[-0.5,-1.5,0], [-0.5,-0.5,1],[0.5,-0.5,1],[0.5,-1.5,1],[-0.5,-1.5,1] ] } )) t.end() }) test('oriented boxes 3d', function (t) { var sq22 = Math.sqrt(2)/2 t.deepEqual(roundv3(1000,sat3d( [], { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [0,1,0],[1,1,0],[1,0,0],[0,0,0], [0,1,1],[1,1,1],[1,0,1],[0,0,1] ] }, { separatingAxes: [[sq22,sq22,0],[-sq22,sq22,0],[0,0,1]], positions: [ [-0.3,0.5,0],[+0.2,0.0,0],[-0.3,-0.5,0],[-0.8,0.0,0], [-0.3,0.5,1],[+0.2,0.0,1],[-0.3,-0.5,1],[-0.8,0.0,1] ] } )), [0.1,0.1,0]) t.notOk(sat3d( [], { separatingAxes: [[0,0,1],[0,1,0],[1,0,0]], positions: [ [0,1,0],[1,1,0],[1,0,0],[0,0,0], [0,1,1],[1,1,1],[1,0,1],[0,0,1] ] }, { separatingAxes: [[sq22,sq22,0],[-sq22,sq22,0],[0,0,1]], positions: [ [-0.3,0.25,0],[+0.2,-0.25,0],[-0.3,-0.75,0],[-0.8,-0.25,0], [-0.3,0.25,1],[+0.2,-0.25,1],[-0.3,-0.75,1],[-0.8,-0.25,1] ] } )) t.end() }) function roundv3 (n, v) { v[0] = Math.round(v[0]*n)/n v[1] = Math.round(v[1]*n)/n v[2] = Math.round(v[2]*n)/n return v }