1
0
Fork 0
Browse Source

feat: add boundary test

Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
combat-scorched-earth-from-outer-space
André Jaenisch 10 months ago
parent
commit
5f88768fd0
Signed by: ryuno-ki
GPG Key ID: 5A668E771F1ED854
  1. 14
      src/js/collisions.js
  2. 43
      test/js/collisions.test.js
  3. 10
      types/collisions.d.ts

14
src/js/collisions.js

@ -0,0 +1,14 @@
import { length, subtract } from '../../src/js/vector.js'
/** @typedef {import('./shape').Shape} Shape */
/**
* Test two shapes on intersecting boundary circles (since rotation).
*
* @param {Shape} shape1
* @param {Shape} shape2
* @returns {boolean}
*/
export function testBoundaries (shape1, shape2) {
return length(subtract(shape2.C, shape1.C)) <= shape2.B + shape1.B
}

43
test/js/collisions.test.js

@ -0,0 +1,43 @@
import { expect } from 'chai'
import { testBoundaries } from '../../src/js/collisions.js'
import { RigidShape } from '../../src/js/shape.js'
import { Vec2 } from '../../src/js/vector.js'
describe('Collisions', function () {
describe('testBoundaries', function () {
it('should test the boundaries of two shapes for intersection', function () {
// Arrange
const shape1 = makeShape()
const shape2 = makeShape()
// Act
const haveIntersectingBoundaries = testBoundaries(shape1, shape2)
// Assert
expect(haveIntersectingBoundaries).to.equal(true)
})
})
})
function makeShape () {
const center = Vec2(500, 200)
const friction = 20
const restitution = 0
const mass = 400
const bounds = 1
const width = 5
const height = 5
const shape = RigidShape({
center,
mass,
friction,
restitution,
bounds,
width,
height
})
return shape
}

10
types/collisions.d.ts vendored

@ -0,0 +1,10 @@
/** @typedef {import('./shape').Shape} Shape */
/**
* Test two shapes on intersecting boundary circles (since rotation).
*
* @param {Shape} shape1
* @param {Shape} shape2
* @returns {boolean}
*/
export function testBoundaries(shape1: Shape, shape2: Shape): boolean;
export type Shape = import('./shape').Shape;
Loading…
Cancel
Save