Skip to content

Commit

Permalink
Add gravity to bring the player back down
Browse files Browse the repository at this point in the history
  • Loading branch information
MattyRad committed Feb 8, 2020
1 parent 802d331 commit f8159a2
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion public/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ scene.add( obstacle );
camera.position.z = 5;

var jumping = false;
var jump_distance = 0.1;
var acceleration = 0.0025;
var acceleration_step = 0.0025;

var animate = function () {
requestAnimationFrame( animate );
Expand All @@ -40,9 +43,25 @@ var animate = function () {
}

if (jumping) {
player.position.y += 0.02;
acceleration_step = acceleration_step * (1 + acceleration);

jump_distance -= acceleration_step;

player.position.y += jump_distance;
}

if (player.position.y < initial_y) { // hit the "ground"
jumping = false;

// reset our acceleration calculations
jump_distance = 0.1;
acceleration_step = acceleration;

// may have overshot, hard reset
player.position.y = initial_y;
}


for (var vi = 0; vi < player.geometry.vertices.length; vi++) {
var localVertex = player.geometry.vertices[vi].clone();
var globalVertex = localVertex.applyMatrix4( player.matrix );
Expand Down

0 comments on commit f8159a2

Please sign in to comment.