fix: physics tick drag

This commit is contained in:
cha0s 2020-04-19 21:02:11 -05:00
parent f0bc544aed
commit 23919573d6
6 changed files with 21 additions and 13 deletions

View File

@ -36,7 +36,7 @@
- ✔ Remove position pack check from trait and add to Vector
- ❌ PIXI particles
- ❌ Pointer events polyfill: https://github.com/jquery/PEP
- Fix physics drag during latency
- Fix physics drag during latency
- ❌ Socket WebWorker can't connect in Firefox
- ✔ PIXI update to v5
- ❌ chunked tile communication

View File

@ -12,6 +12,7 @@ export class AbstractBody extends decorate(class {}) {
constructor(world, shape) {
super();
this.impulse = [0, 0];
this.shape = shape;
this.world = world;
}

View File

@ -18,7 +18,6 @@ export class Body extends decorate(AbstractBody) {
constructor(world, shape) {
super(world, shape);
this.force = [0, 0];
this.impulse = [0, 0];
this.contacts = I.Set();
}
@ -31,10 +30,7 @@ export class Body extends decorate(AbstractBody) {
}
applyImpulse(vector) {
this.impulse = Vector.add(
this.impulse,
Vector.scale(vector, this.world.stepTime)
);
this.impulse = Vector.add(this.impulse, vector);
}
}

View File

@ -65,7 +65,7 @@ export class World extends AbstractWorld {
for (const entity of this.entities.values()) {
const body = entity.body;
const translation = Vector.add(
body.impulse,
Vector.mul(body.impulse, elapsed),
body.force,
);
body.position = Vector.add(body.position, translation);
@ -144,7 +144,7 @@ export class World extends AbstractWorld {
const contacts = body.contacts;
if (contacts.size > 0) {
const translation = Vector.add(
body.impulse,
Vector.mul(body.impulse, elapsed),
body.force,
);
body.position = Vector.sub(body.position, translation);

View File

@ -74,10 +74,7 @@ export class Body extends AbstractBody {
}
applyImpulse(impulse) {
impulse = Vector.scale(impulse, 1 / SCALE);
impulse = Vector.scale(impulse, this.world.stepTime);
const [x, y] = impulse;
MatterBody.translate(this.matterBody, {x, y});
this.impulse = Vector.add(this.impulse, Vector.scale(impulse, 1 / SCALE));
}
static bodyFromShape(shape) {

View File

@ -1,4 +1,6 @@
import {Engine, Events, World as MatterWorld} from 'matter-js';
import {Body as MatterBody, Engine, Events, World as MatterWorld} from 'matter-js';
import {Vector} from '@avocado/math';
import {Body} from './body';
import {AbstractWorld} from '../abstract/world';
@ -62,10 +64,22 @@ export class World extends AbstractWorld {
const stepTime = this.stepTime;
const stepTimeInMs = stepTime * 1000;
while (elapsed >= stepTime) {
// Apply impulses.
let it = this.entities.entries();
for (let value = it.next(); value.done !== true; value = it.next()) {
const body = value.value[0];
const [x, y] = Vector.scale(body.impulse, stepTime);
MatterBody.translate(body.matterBody, {x, y});
}
Engine.update(this.engine, stepTimeInMs);
elapsed -= stepTime;
}
this.elapsedRemainder = elapsed;
// Reset impulses.
const it = this.entities.entries();
for (let value = it.next(); value.done !== true; value = it.next()) {
value.value[0].impulse = [0, 0];
}
// Propagate.
super.tick(elapsed);
}