diff --git a/TODO.md b/TODO.md index 275e2b7..7f59858 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/packages/physics/abstract/body.js b/packages/physics/abstract/body.js index 9460b80..0c7df7d 100644 --- a/packages/physics/abstract/body.js +++ b/packages/physics/abstract/body.js @@ -12,6 +12,7 @@ export class AbstractBody extends decorate(class {}) { constructor(world, shape) { super(); + this.impulse = [0, 0]; this.shape = shape; this.world = world; } diff --git a/packages/physics/dummy/body.js b/packages/physics/dummy/body.js index 0dd8e30..f9e9060 100644 --- a/packages/physics/dummy/body.js +++ b/packages/physics/dummy/body.js @@ -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); } } diff --git a/packages/physics/dummy/world.js b/packages/physics/dummy/world.js index 9091d69..44e6a82 100644 --- a/packages/physics/dummy/world.js +++ b/packages/physics/dummy/world.js @@ -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); diff --git a/packages/physics/matter/body.js b/packages/physics/matter/body.js index 3186729..84d490c 100644 --- a/packages/physics/matter/body.js +++ b/packages/physics/matter/body.js @@ -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) { diff --git a/packages/physics/matter/world.js b/packages/physics/matter/world.js index a05cfea..3d6fdc5 100644 --- a/packages/physics/matter/world.js +++ b/packages/physics/matter/world.js @@ -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); }