1
0
Fork 0
Browse Source

feat: move rotation into dedicated file

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

31
src/js/rotation.js

@ -0,0 +1,31 @@
import { FPS } from './constants.js'
import { computeNormals } from './shape.js'
import { rotate } from './vector.js'
/** @typedef {import('./shape').Shape} Shape */
/** @typedef {import('./vector').Vector2D} Vector2D */
/**
* Update the rotation of the shape.
*
* @param {Shape} shape
*/
export function updateRotation (shape) {
shape.v = shape.v + shape.a * 1 / FPS
rotateShape(shape, shape.v * 1 / FPS)
}
/**
* Rotate a shape around its centre.
*
* @param {Shape} shape
* @param {number} angle
*/
function rotateShape (shape, angle) {
shape.G = shape.G + angle
shape.X.forEach(function (vertex, index) {
shape.X[index] = rotate(vertex, shape.C, angle)
computeNormals(shape)
})
}

45
test/js/rotation.test.js

@ -0,0 +1,45 @@
import { expect } from 'chai'
import { updateRotation } from '../../src/js/rotation.js'
import { RigidShape } from '../../src/js/shape.js'
import { Vec2 } from '../../src/js/vector.js'
describe('Rotation', function () {
it('should update the rotation', function () {
// Arrange
const shape = makeShape()
const g = shape.G // angle
const v = shape.v // angle velocity
const x = [...shape.X]
// Act
updateRotation(shape)
// Assert
expect(shape.G).to.equal(g)
expect(shape.v).to.equal(v)
shape.X.forEach(function (vertex, i) { expect(vertex).not.to.equal(x[i]) })
})
})
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/rotation.d.ts vendored

@ -0,0 +1,10 @@
/** @typedef {import('./shape').Shape} Shape */
/** @typedef {import('./vector').Vector2D} Vector2D */
/**
* Update the rotation of the shape.
*
* @param {Shape} shape
*/
export function updateRotation(shape: Shape): void;
export type Shape = import('./shape').Shape;
export type Vector2D = import('./vector').Vector2D;
Loading…
Cancel
Save