fix: physics apply stepTime as impulse

This commit is contained in:
cha0s 2019-05-04 13:31:16 -05:00
parent dc3eeb4e7d
commit c148bc2456
8 changed files with 16 additions and 14 deletions

View File

@ -62,7 +62,7 @@ class MobileBase extends Trait {
return;
}
if (this.entity.is('physical')) {
this.entity.applyImpulse(this.requestedMovement, elapsed);
this.entity.applyImpulse(this.requestedMovement);
}
else {
const requestedMovement = Vector.scale(

View File

@ -10,9 +10,10 @@ const decorate = compose(
export class AbstractBody extends decorate(class {}) {
constructor(shape) {
constructor(world, shape) {
super();
this.shape = shape;
this.world = world;
}
}

View File

@ -34,6 +34,7 @@ export class AbstractWorld extends decorate(class {}) {
this.entitiesList.splice(index, 1);
}
}
body.world = null;
}
tick(elapsed) {

View File

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

View File

@ -37,7 +37,7 @@ export class World extends AbstractWorld {
}
createBody(shape) {
return new Body(shape);
return new Body(this, shape);
}
removeBody(body) {

View File

@ -47,8 +47,8 @@ export class Body extends AbstractBody {
this.bodies.set(matterBody, avocadoBody);
}
constructor(shape) {
super(shape);
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;
@ -73,9 +73,9 @@ export class Body extends AbstractBody {
MatterBody.applyForce(this.matterBody, this.matterBody.position, {x, y});
}
applyImpulse(impulse, elapsed) {
applyImpulse(impulse) {
impulse = Vector.scale(impulse, 1 / SCALE);
impulse = Vector.scale(impulse, elapsed);
impulse = Vector.scale(impulse, this.world.stepTime);
const [x, y] = impulse;
MatterBody.translate(this.matterBody, {x, y});
}

View File

@ -27,7 +27,7 @@ export class World extends AbstractWorld {
}
createBody(shape) {
return new Body(shape);
return new Body(this, shape);
}
handleCollisions(eventName) {

View File

@ -115,9 +115,9 @@ export class Physical extends decorate(Trait) {
}
},
applyImpulse: (impulse, elapsed) => {
applyImpulse: (impulse) => {
if (this._world && this._body) {
this._body.applyImpulse(impulse, elapsed);
this._body.applyImpulse(impulse);
}
},