1
0
Fork 0
Browse Source

feat: add new function to compute face normals

Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
combat-scorched-earth-from-outer-space
André Jaenisch 11 months ago
parent
commit
b172a79b00
Signed by: ryuno-ki
GPG Key ID: 5A668E771F1ED854
  1. 16
      src/js/shape.js
  2. 37
      test/js/shape.test.js
  3. 6
      types/shape.d.ts

16
src/js/shape.js

@ -1,5 +1,5 @@
import { gravity } from './constants.js'
import { Vec2 } from './vector.js'
import { normalize, subtract, Vec2 } from './vector.js'
/** @typedef {import('./vector.js').Vector2D} Vector2D
@ -67,5 +67,19 @@ export function RigidShape ({
]
}
computeNormals(shape)
return shape
}
/**
* Compute face normals
*
* @param {Shape} shape
*/
export function computeNormals (shape) {
shape.N.forEach(function (normal, index) {
shape.N[index] = normalize(
subtract(shape.X[(index + 1) % 4], shape.X[(index + 2) % 4])
)
})
}

37
test/js/shape.test.js

@ -1,6 +1,6 @@
import { expect } from 'chai'
import { RigidShape } from '../../src/js/shape.js'
import { computeNormals, RigidShape } from '../../src/js/shape.js'
import { Vec2 } from '../../src/js/vector.js'
describe('RigidShape', function () {
@ -28,4 +28,39 @@ describe('RigidShape', function () {
// Assert
expect(shape).to.be.an('object')
})
describe('computeNormals', function () {
it('should compute the face normals of a shape', function () {
// Arrange
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
})
const originalFaces = shape.N.map(function (face) {
return {...face}
})
// Act
computeNormals(shape)
// Assert
shape.N.forEach(function (face, index) {
const originalFace = originalFaces[index]
expect(face).to.not.deep.equal(originalFace)
})
})
})
})

6
types/shape.d.ts vendored

@ -39,6 +39,12 @@ export function RigidShape({ center, mass, friction, restitution, bounds, width,
width: number;
height: number;
}): Shape;
/**
* Compute face normals
*
* @param {Shape} shape
*/
export function computeNormals(shape: Shape): void;
/**
* /**
*/

Loading…
Cancel
Save