fix: physics tick drag
This commit is contained in:
parent
f0bc544aed
commit
23919573d6
2
TODO.md
2
TODO.md
|
@ -36,7 +36,7 @@
|
||||||
- ✔ Remove position pack check from trait and add to Vector
|
- ✔ Remove position pack check from trait and add to Vector
|
||||||
- ❌ PIXI particles
|
- ❌ PIXI particles
|
||||||
- ❌ Pointer events polyfill: https://github.com/jquery/PEP
|
- ❌ 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
|
- ❌ Socket WebWorker can't connect in Firefox
|
||||||
- ✔ PIXI update to v5
|
- ✔ PIXI update to v5
|
||||||
- ❌ chunked tile communication
|
- ❌ chunked tile communication
|
||||||
|
|
|
@ -12,6 +12,7 @@ export class AbstractBody extends decorate(class {}) {
|
||||||
|
|
||||||
constructor(world, shape) {
|
constructor(world, shape) {
|
||||||
super();
|
super();
|
||||||
|
this.impulse = [0, 0];
|
||||||
this.shape = shape;
|
this.shape = shape;
|
||||||
this.world = world;
|
this.world = world;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,6 @@ export class Body extends decorate(AbstractBody) {
|
||||||
constructor(world, shape) {
|
constructor(world, shape) {
|
||||||
super(world, shape);
|
super(world, shape);
|
||||||
this.force = [0, 0];
|
this.force = [0, 0];
|
||||||
this.impulse = [0, 0];
|
|
||||||
this.contacts = I.Set();
|
this.contacts = I.Set();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,10 +30,7 @@ export class Body extends decorate(AbstractBody) {
|
||||||
}
|
}
|
||||||
|
|
||||||
applyImpulse(vector) {
|
applyImpulse(vector) {
|
||||||
this.impulse = Vector.add(
|
this.impulse = Vector.add(this.impulse, vector);
|
||||||
this.impulse,
|
|
||||||
Vector.scale(vector, this.world.stepTime)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +65,7 @@ export class World extends AbstractWorld {
|
||||||
for (const entity of this.entities.values()) {
|
for (const entity of this.entities.values()) {
|
||||||
const body = entity.body;
|
const body = entity.body;
|
||||||
const translation = Vector.add(
|
const translation = Vector.add(
|
||||||
body.impulse,
|
Vector.mul(body.impulse, elapsed),
|
||||||
body.force,
|
body.force,
|
||||||
);
|
);
|
||||||
body.position = Vector.add(body.position, translation);
|
body.position = Vector.add(body.position, translation);
|
||||||
|
@ -144,7 +144,7 @@ export class World extends AbstractWorld {
|
||||||
const contacts = body.contacts;
|
const contacts = body.contacts;
|
||||||
if (contacts.size > 0) {
|
if (contacts.size > 0) {
|
||||||
const translation = Vector.add(
|
const translation = Vector.add(
|
||||||
body.impulse,
|
Vector.mul(body.impulse, elapsed),
|
||||||
body.force,
|
body.force,
|
||||||
);
|
);
|
||||||
body.position = Vector.sub(body.position, translation);
|
body.position = Vector.sub(body.position, translation);
|
||||||
|
|
|
@ -74,10 +74,7 @@ export class Body extends AbstractBody {
|
||||||
}
|
}
|
||||||
|
|
||||||
applyImpulse(impulse) {
|
applyImpulse(impulse) {
|
||||||
impulse = Vector.scale(impulse, 1 / SCALE);
|
this.impulse = Vector.add(this.impulse, Vector.scale(impulse, 1 / SCALE));
|
||||||
impulse = Vector.scale(impulse, this.world.stepTime);
|
|
||||||
const [x, y] = impulse;
|
|
||||||
MatterBody.translate(this.matterBody, {x, y});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bodyFromShape(shape) {
|
static bodyFromShape(shape) {
|
||||||
|
|
|
@ -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 {Body} from './body';
|
||||||
import {AbstractWorld} from '../abstract/world';
|
import {AbstractWorld} from '../abstract/world';
|
||||||
|
@ -62,10 +64,22 @@ export class World extends AbstractWorld {
|
||||||
const stepTime = this.stepTime;
|
const stepTime = this.stepTime;
|
||||||
const stepTimeInMs = stepTime * 1000;
|
const stepTimeInMs = stepTime * 1000;
|
||||||
while (elapsed >= stepTime) {
|
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);
|
Engine.update(this.engine, stepTimeInMs);
|
||||||
elapsed -= stepTime;
|
elapsed -= stepTime;
|
||||||
}
|
}
|
||||||
this.elapsedRemainder = elapsed;
|
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.
|
// Propagate.
|
||||||
super.tick(elapsed);
|
super.tick(elapsed);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user