Browse Source
Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>combat-scorched-earth-from-outer-space
3 changed files with 86 additions and 0 deletions
@ -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) |
||||
}) |
||||
} |
@ -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 |
||||
} |
@ -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…
Reference in new issue