flow: scaling, list/parts, elegance
This commit is contained in:
parent
1f6551ae8c
commit
e61ed5636d
|
@ -1,4 +1,4 @@
|
|||
import {Composite, Bodies, Body as MatterBody, Vertices} from 'matter-js';
|
||||
import {Composite, Bodies, Body as MatterBody, Bounds, Vertices} from 'matter-js';
|
||||
|
||||
import {Vector} from '@avocado/math';
|
||||
import {
|
||||
|
@ -10,6 +10,14 @@ import {
|
|||
|
||||
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');
|
||||
}
|
||||
|
||||
const SCALE = 1;
|
||||
|
||||
export class Body extends AbstractBody {
|
||||
|
||||
static collisionCategory(group) {
|
||||
|
@ -41,7 +49,11 @@ export class Body extends AbstractBody {
|
|||
|
||||
constructor(shape) {
|
||||
super(shape);
|
||||
[this.origin, this.matterBody] = this.constructor.bodyFromShape(shape);
|
||||
[
|
||||
this.matterBody,
|
||||
this.origin,
|
||||
this.position,
|
||||
] = this.constructor.bodyFromShape(shape);
|
||||
this.constructor.associateBody(this.matterBody, this);
|
||||
}
|
||||
|
||||
|
@ -52,84 +64,70 @@ export class Body extends AbstractBody {
|
|||
bounds.min.y,
|
||||
bounds.max.x - bounds.min.x,
|
||||
bounds.max.y - bounds.min.y,
|
||||
];
|
||||
].map((p) => p * SCALE);
|
||||
}
|
||||
|
||||
applyForce(force) {
|
||||
MatterBody.applyForce(
|
||||
this.matterBody,
|
||||
this.matterBody.position,
|
||||
{
|
||||
x: force[0],
|
||||
y: force[1],
|
||||
}
|
||||
);
|
||||
force = Vector.scale(force, 1 / SCALE);
|
||||
const [x, y] = force;
|
||||
MatterBody.applyForce(this.matterBody, this.matterBody.position, {x, y});
|
||||
}
|
||||
|
||||
applyImpulse(impulse, elapsed) {
|
||||
impulse = Vector.scale(impulse, 1 / SCALE);
|
||||
impulse = Vector.scale(impulse, elapsed);
|
||||
MatterBody.translate(this.matterBody, {
|
||||
x: impulse[0],
|
||||
y: impulse[1],
|
||||
});
|
||||
const [x, y] = impulse;
|
||||
MatterBody.translate(this.matterBody, {x, y});
|
||||
}
|
||||
|
||||
static bodyFromShape(shape) {
|
||||
if (shape instanceof RectangleShape) {
|
||||
const body = Bodies.rectangle(
|
||||
0,
|
||||
0,
|
||||
shape.width,
|
||||
shape.height,
|
||||
);
|
||||
return [shape.position, body];
|
||||
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(Vector.scale(position, 1 / SCALE), origin);
|
||||
MatterBody.setPosition(body, {x, y});
|
||||
children.push(body);
|
||||
}
|
||||
const body = MatterBody.create({parts: children});
|
||||
const {x, y} = body.position;
|
||||
return [body, shapePosition, Vector.scale([x, y], SCALE)];
|
||||
}
|
||||
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,
|
||||
);
|
||||
return [shape.position, body];
|
||||
const body = Bodies.circle(0, 0, shape.radius / SCALE);
|
||||
return [body, shapePosition, [0, 0]];
|
||||
}
|
||||
else if (shape instanceof PolygonShape) {
|
||||
const vectors = [];
|
||||
for (const vertice of shape) {
|
||||
vectors.push({
|
||||
x: vertice[0],
|
||||
y: vertice[1],
|
||||
});
|
||||
for (let vertice of shape) {
|
||||
const[x, y] = Vector.scale(vertice, 1 / SCALE)
|
||||
vectors.push({x, y});
|
||||
}
|
||||
const centre = Vertices.centre(vectors);
|
||||
const body = Bodies.fromVertices(
|
||||
0,
|
||||
0,
|
||||
vectors,
|
||||
);
|
||||
const origin = Vector.add(
|
||||
shape.position,
|
||||
Vector.scale([centre.x, centre.y], 1),
|
||||
);
|
||||
return [origin, body];
|
||||
const {x, y} = Vertices.centre(vectors);
|
||||
const body = Bodies.fromVertices(0, 0, vectors);
|
||||
return [body, shapePosition, [x, y]];
|
||||
}
|
||||
}
|
||||
|
||||
get position() {
|
||||
return [
|
||||
this.matterBody.position.x,
|
||||
this.matterBody.position.y,
|
||||
];
|
||||
const {x, y} = this.matterBody.position;
|
||||
return Vector.scale(Vector.sub([x, y], this.origin), SCALE);
|
||||
}
|
||||
|
||||
set position(position) {
|
||||
position = Vector.scale(position, 1 / SCALE);
|
||||
position = Vector.add(position, this.origin);
|
||||
if (Vector.equalsClose(this.position, position)) {
|
||||
return;
|
||||
}
|
||||
MatterBody.setPosition(this.matterBody, {
|
||||
x: position[0],
|
||||
y: position[1],
|
||||
});
|
||||
const [x, y] = position;
|
||||
MatterBody.setPosition(this.matterBody, {x, y});
|
||||
}
|
||||
|
||||
setCollision(category, mask, group = 0) {
|
||||
|
@ -166,7 +164,7 @@ export class Body extends AbstractBody {
|
|||
get vertices() {
|
||||
const vertices = [];
|
||||
for (const {x, y} of this.matterBody.vertices) {
|
||||
vertices.push([x, y]);
|
||||
vertices.push(Vector.scale([x, y], SCALE));
|
||||
}
|
||||
return vertices;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user