1
0
Fork 0
Browse Source

feat: add shape module

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

71
src/js/shape.js

@ -0,0 +1,71 @@
import { gravity } from './constants.js'
import { Vec2 } from './vector.js'
/** @typedef {import('./vector.js').Vector2D} Vector2D
/**
* @typedef {object} Shape
* @property {Vector2D} C
* @property {number} F
* @property {number} R
* @property {number} M
* @property {Vector2D} V
* @property {Vector2D} A
* @property {number} G
* @property {number} v
* @property {number} a
* @property {number} B
* @property {number} W
* @property {number} H
* @property {Array<*>} N
* @property {Array<Vector2D>} X
*/
/**
* Defines a rigid shape.
*
* @param {object} config
* @param {Vector2D} config.center
* @param {number} config.mass
* @param {number} config.friction
* @param {number} config.restitution
* @param {number} config.bounds
* @param {number} config.width
* @param {number} config.height
* @returns {Shape}
*/
export function RigidShape ({
center,
mass,
friction,
restitution, // bouncing
bounds, // radius
width,
height
}) {
const immobile = 0
const shape = {
C: center,
F: friction,
R: restitution,
M: mass ? 1 / mass : immobile,
V: Vec2(0, 0), // velocity, i.e. speed
A: mass ? gravity : Vec2(0, 0), // acceleration
G: 0, // angle
v: 0, // angle velocity
a: 0, // angle acceleration
B: bounds,
W: width,
H: height,
N: [], // face normals array
X: [
Vec2(center.x - width / 2, center.y - height / 2), // top-left
Vec2(center.x + width / 2, center.y - height / 2), // top-right
Vec2(center.x + width / 2, center.y + height / 2), // bottom-right
Vec2(center.x - width / 2, center.y + height / 2) // bottom-left
]
}
return shape
}

31
test/js/shape.test.js

@ -0,0 +1,31 @@
import { expect } from 'chai'
import { RigidShape } from '../../src/js/shape.js'
import { Vec2 } from '../../src/js/vector.js'
describe('RigidShape', function () {
it('should create a rigid 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
// Act
const shape = RigidShape({
center,
mass,
friction,
restitution,
bounds,
width,
height
})
// Assert
expect(shape).to.be.an('object')
})
})

61
types/shape.d.ts vendored

@ -0,0 +1,61 @@
/** @typedef {import('./vector.js').Vector2D} Vector2D
/**
* @typedef {object} Shape
* @property {Vector2D} C
* @property {number} F
* @property {number} R
* @property {number} M
* @property {Vector2D} V
* @property {Vector2D} A
* @property {number} G
* @property {number} v
* @property {number} a
* @property {number} B
* @property {number} W
* @property {number} H
* @property {Array<*>} N
* @property {Array<Vector2D>} X
*/
/**
* Defines a rigid shape.
*
* @param {object} config
* @param {Vector2D} config.center
* @param {number} config.mass
* @param {number} config.friction
* @param {number} config.restitution
* @param {number} config.bounds
* @param {number} config.width
* @param {number} config.height
* @returns {Shape}
*/
export function RigidShape({ center, mass, friction, restitution, bounds, width, height }: {
center: Vector2D;
mass: number;
friction: number;
restitution: number;
bounds: number;
width: number;
height: number;
}): Shape;
/**
* /**
*/
export type Vector2D = import('./vector.js').Vector2D;
export type Shape = {
C: Vector2D;
F: number;
R: number;
M: number;
V: Vector2D;
A: Vector2D;
G: number;
v: number;
a: number;
B: number;
W: number;
H: number;
N: Array<any>;
X: Array<Vector2D>;
};
Loading…
Cancel
Save