From 1199f4bbef88a7f621f816aed57dcdfd0db9e3f0 Mon Sep 17 00:00:00 2001 From: Philipp Abraham Date: Sun, 16 Feb 2014 19:05:22 +0100 Subject: [PATCH] improved gravity behaviour --- src/bird.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/bird.js b/src/bird.js index 9fb44e9..554a68d 100644 --- a/src/bird.js +++ b/src/bird.js @@ -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)