Skip to content

Commit

Permalink
improved gravity behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
master-lincoln committed Feb 16, 2014
1 parent a4911e9 commit 1199f4b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/bird.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,25 @@ define([], function() {
function tick(xMove, delta) {
position.x += xMove;
if (!touchingGround) {
// calculate position and velocity using the 'velocity verlet' method
var timeStep = 1/ delta;
position.y += alive ? (timeStep * (velocity + timeStep * GRAVITY/2)) : 0;
// keep him below top
position.y = position.y < -boundingBox.w/2 ? -boundingBox.w/2 : position.y;
velocity += timeStep * GRAVITY;
fallDown(delta);
}
}

function fallDown(delta) {
// calculate position/velocity using the 'velocity verlet' integration
// http://en.wikipedia.org/wiki/Verlet_integration#Velocity_Verlet
var timeStep = delta/256;
position.y += timeStep * (velocity + timeStep * GRAVITY/2);
velocity += timeStep * GRAVITY;

// keep him below top
if ( position.y < -boundingBox.w/2 ) {
position.y = -boundingBox.w/2;
velocity = 0;
}

}

function jump() {
touchingGround = false;
if (!alive)
Expand Down

0 comments on commit 1199f4b

Please sign in to comment.