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