From 065d1711dea175948dbbb57fd0769d9d11333d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Jaenisch?= Date: Fri, 20 Aug 2021 18:40:54 +0200 Subject: [PATCH] feat: move rotation into dedicated file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: André Jaenisch --- src/js/rotation.js | 31 +++++++++++++++++++++++++++ test/js/rotation.test.js | 45 ++++++++++++++++++++++++++++++++++++++++ types/rotation.d.ts | 10 +++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/js/rotation.js create mode 100644 test/js/rotation.test.js create mode 100644 types/rotation.d.ts diff --git a/src/js/rotation.js b/src/js/rotation.js new file mode 100644 index 0000000..6c473b5 --- /dev/null +++ b/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) + }) +} diff --git a/test/js/rotation.test.js b/test/js/rotation.test.js new file mode 100644 index 0000000..85ee363 --- /dev/null +++ b/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 +} diff --git a/types/rotation.d.ts b/types/rotation.d.ts new file mode 100644 index 0000000..e2522ce --- /dev/null +++ b/types/rotation.d.ts @@ -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;