Skip to content

Commit

Permalink
Tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Sep 30, 2016
1 parent 97b005d commit 82dcdb6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
3 changes: 1 addition & 2 deletions sandbox/web/src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ player.on('update', function () {
}
player.vel.x = groundSpeed;
}
if (game.input.keyboard.wasPressed(ex.Input.Keys.Up)) {
if (game.input.keyboard.isHeld(ex.Input.Keys.Up)) {
if (!inAir) {
player.vel.y = -jumpSpeed;
inAir = true;
Expand Down Expand Up @@ -300,7 +300,6 @@ player.on('postupdate', function (data) {
data.target.acc.y = 800; // * data.delta/1000;
}
else {
data.target.acc.y = 0;
}
// Reset values because we don't know until we check the next update
// inAir = true;
Expand Down
4 changes: 2 additions & 2 deletions sandbox/web/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ player.on('update', () => {
// TODO: When platform is moving in same direction, add its dx
}

if (game.input.keyboard.wasPressed(ex.Input.Keys.Up)) {
if (game.input.keyboard.isHeld(ex.Input.Keys.Up)) {
if (!inAir) {
player.vel.y = -jumpSpeed;
inAir = true;
Expand Down Expand Up @@ -353,7 +353,7 @@ player.on('postupdate', (data?: ex.PostUpdateEvent) => {
if (!isColliding) {
data.target.acc.y = 800;// * data.delta/1000;
} else {
data.target.acc.y = 0;
//data.target.acc.y = 0;
}

// Reset values because we don't know until we check the next update
Expand Down
22 changes: 12 additions & 10 deletions src/engine/Collision/CollisionContact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,27 @@ module ex {
} else if (bodyA.vel.x >= 0 && bodyB.vel.x >= 0) {
velX = Math.max(bodyA.vel.x, bodyB.vel.x);
velX = bodyA.vel.x + bodyA.vel.x;
}else {
} else if (bodyB.collisionType === ex.CollisionType.Fixed) {
// bodies are traveling in opposite directions
velX = 0;
velX = bodyB.vel.x;
}
bodyA.vel.x = velX;
}


if (this.mtv.y !== 0) {
var velY = 0;
// both bodies are traveling in the same direction (negative or positive)
if (bodyA.vel.y <= 0 && bodyB.vel.y <= 0) {
velY = Math.max(bodyA.vel.y, bodyB.vel.y);
} else if (bodyA.vel.y >= 0 && bodyB.vel.y >= 0) {
velY = Math.min(bodyA.vel.y, bodyB.vel.y);
} else {
if (bodyB.collisionType === ex.CollisionType.Fixed) {
// bodies are traveling in opposite directions
velY = 0;
}
velY = bodyB.vel.y;
} else {
// both bodies are traveling in the same direction (negative or positive)
if (bodyA.vel.y <= 0 && bodyB.vel.y <= 0) {
velY = Math.min(bodyA.vel.y, bodyB.vel.y);
} else if (bodyA.vel.y >= 0 && bodyB.vel.y >= 0) {
velY = Math.max(bodyA.vel.y, bodyB.vel.y);
}
}

bodyA.vel.y = velY;
}
Expand Down
22 changes: 10 additions & 12 deletions src/engine/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,31 +255,29 @@ module ex {
this.tileMaps[i].update(engine, delta);
}

for (i = 0, len = this.children.length; i < len; i++) {
//this.children[i].integrate(delta);
}

// Cycle through actors updating actors
for (i = 0, len = this.children.length; i < len; i++) {
this.children[i].update(engine, delta);
this.children[i].update(engine, delta);
}

// Run the broadphase
if (this._broadphase) {
this._broadphase.update(this.children, delta);
}
this._broadphase.update(this.children, delta);

var iter: number = 20; //Physics.collisionPasses;
// Run the narrowphase
var iter: number = Physics.collisionPasses;
var collisionDelta = delta / iter;

while (iter > 0) {
// Run collision resolution strategy
if (this._broadphase && Physics.enabled) {
this._broadphase.detect(this.children, collisionDelta);
for (i = 0, len = this.children.length; i < len; i++) {
this.children[i].integrate(collisionDelta * .01);
this.children[i].integrate(collisionDelta * .001);
}
}
iter--;
}


}

// Remove actors from scene graph after being killed
var actorIndex: number;
Expand Down
11 changes: 5 additions & 6 deletions src/spec/ActorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ describe('A game actor', () => {
var active = new ex.Actor(0, -50, 100, 100);
active.collisionType = ex.CollisionType.Active;
active.vel.y = -100;
//active.acc.y = 1000;
ex.Physics.acc.setTo(0, 0);

var fixed = new ex.Actor(-100, 50, 1000, 100);
fixed.collisionType = ex.CollisionType.Fixed;
Expand All @@ -556,16 +556,15 @@ describe('A game actor', () => {
expect(fixed.pos.x).toBe(-100);
expect(fixed.pos.y).toBe(50);

var iterations = 50;
var iterations = 1;
// update many times for safety
for (var i = 0; i < iterations; i++) {
scene.update(engine, 100);
scene.update(engine, 1000);
}

var seconds = 100 / 1000;

expect(ex.Physics.acc.y).toBe(0);
expect(active.pos.x).toBeCloseTo(0, .0001);
expect(active.pos.y).toBeCloseTo(-100 * seconds * iterations, .0001);
expect(active.pos.y).toBeCloseTo(-100 * iterations + (-50) /* original y is -50 */, .0001);

expect(fixed.pos.x).toBe(-100);
expect(fixed.pos.y).toBe(50);
Expand Down

0 comments on commit 82dcdb6

Please sign in to comment.