import {Composite, Bodies, Body as MatterBody} from 'matter-js'; import {Vector} from '@avocado/math'; import {ShapeList, PolygonShape, RectangleShape} from '@avocado/physics'; import {AbstractBody} from '../abstract/body'; import { Rectangle } from '../../math'; export class Body extends AbstractBody { constructor(shape) { super(shape); this.matterBody = this.constructor.bodyFromShape(shape); } applyForce(force) { MatterBody.applyForce( this.matterBody, this.matterBody.position, { x: force[0], y: force[1], } ); } applyImpulse(impulse, elapsed) { impulse = Vector.scale(impulse, elapsed); MatterBody.translate(this.matterBody, { x: impulse[0], y: impulse[1], }); } static bodyFromShape(shape) { if (shape instanceof RectangleShape) { return Bodies.rectangle( shape.x, shape.y, shape.width, shape.height, ); } else if (shape instanceof PolygonShape) { const vectors = []; for (const vertice of shape) { vectors.push({ x: vertice[0], y: vertice[1], }); } return Bodies.fromVertices( shape.x, shape.y, vectors, ); } } get position() { return [ this.matterBody.position.x, this.matterBody.position.y, ]; } set position(position) { MatterBody.setPosition(this.matterBody, { x: position[0], y: position[1], }); } }