avocado-old/packages/physics/matter/body.js
2020-04-19 21:02:11 -05:00

179 lines
4.9 KiB
JavaScript

import {Composite, Bodies, Body as MatterBody, Bounds, Vertices} from 'matter-js';
import {Vector} from '@avocado/math';
import {
ShapeList,
CircleShape,
PolygonShape,
RectangleShape,
} from '@avocado/physics';
import {AbstractBody} from '../abstract/body';
// Trick matter into not needing decomp for our convex polygons.
const g = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : undefined;
if (g) {
g.decomp = require('poly-decomp');
}
// Translate "real" coordinates to physics coordinates.
const SCALE = 1;
export class Body extends AbstractBody {
static collisionCategory(group) {
if (!('filterCategoryBit' in this)) {
this.filterCategoryBit = 0;
}
if (!('filterCategories' in this)) {
this.filterCategories = {};
}
if (!this.filterCategories[group]) {
this.filterCategories[group] = 1 << this.filterCategoryBit;
this.filterCategoryBit += 1;
}
return this.filterCategories[group];
}
static lookupBody(matterBody) {
if (this.bodies) {
return this.bodies.get(matterBody);
}
}
static associateBody(matterBody, avocadoBody) {
if (!this.bodies) {
this.bodies = new WeakMap();
}
this.bodies.set(matterBody, avocadoBody);
}
constructor(world, shape) {
super(world, shape);
const [body, position, origin] = this.constructor.bodyFromShape(shape);
// Set body first, then origin, then position. Order important.
this.matterBody = body;
this.origin = origin;
this.position = Vector.scale(position, SCALE);
this.constructor.associateBody(this.matterBody, this);
}
get aabb() {
const bounds = this.matterBody.bounds;
return [
bounds.min.x,
bounds.min.y,
bounds.max.x - bounds.min.x,
bounds.max.y - bounds.min.y,
].map((p) => p * SCALE);
}
applyForce(force) {
force = Vector.scale(force, 1 / SCALE);
const [x, y] = force;
MatterBody.applyForce(this.matterBody, this.matterBody.position, {x, y});
}
applyImpulse(impulse) {
this.impulse = Vector.add(this.impulse, Vector.scale(impulse, 1 / SCALE));
}
static bodyFromShape(shape) {
const shapePosition = Vector.scale(shape.position, 1 / SCALE);
if (shape instanceof ShapeList) {
const children = [];
for (const child of shape) {
const [body, position, origin] = this.bodyFromShape(child);
const [x, y] = Vector.add(position, origin);
MatterBody.setPosition(body, {x, y});
children.push(body);
}
let body = MatterBody.create({parts: children});
// Convex hull?
if (true) {
const {vertices} = body;
body = Bodies.fromVertices(0, 0, vertices);
MatterBody.setPosition(body, Vertices.centre(vertices));
}
const {x, y} = body.position;
return [body, shapePosition, [x, y]];
}
else if (shape instanceof RectangleShape) {
const [width, height] = Vector.scale(shape.size, 1 / SCALE);
const body = Bodies.rectangle(0, 0, width, height);
return [body, shapePosition, [0, 0]];
}
else if (shape instanceof CircleShape) {
const body = Bodies.circle(0, 0, shape.radius / SCALE);
return [body, shapePosition, [0, 0]];
}
else if (shape instanceof PolygonShape) {
const vectors = [];
for (let vertice of shape) {
const[x, y] = Vector.scale(vertice, 1 / SCALE)
vectors.push({x, y});
}
const {x, y} = Vertices.centre(vectors);
const body = Bodies.fromVertices(0, 0, vectors);
return [body, shapePosition, [x, y]];
}
}
get position() {
const {x, y} = this.matterBody.position;
return Vector.sub(Vector.scale([x, y], SCALE), this.origin);
}
set position(position) {
position = Vector.scale(position, 1 / SCALE);
position = Vector.add(position, this.origin);
if (Vector.equalsClose(this.position, position)) {
return;
}
const [x, y] = position;
MatterBody.setPosition(this.matterBody, {x, y});
}
setCollision(category, mask, group = 0) {
MatterBody.set(this.matterBody, 'collisionFilter', {
category,
group,
mask,
});
}
setCollisionForEntity(entity) {
if (entity.is('collider')) {
const ctor = this.constructor;
const category = ctor.collisionCategory(entity.collisionGroup);
let mask = 0;
for (const group of entity.collidesWithGroups) {
mask = mask | ctor.collisionCategory(group);
}
this.setCollision(category, mask);
if (entity.isSensor) {
MatterBody.set(this.matterBody, 'isSensor', true);
}
}
else {
this.setCollision(0, 0, -1);
}
}
get static() {
return this.matterBody.isStatic;
}
set static(isStatic) {
MatterBody.setStatic(this.matterBody, isStatic);
}
get vertices() {
const vertices = [];
for (const {x, y} of this.matterBody.vertices) {
vertices.push(Vector.scale([x, y], SCALE));
}
return vertices;
}
}