Skip to content
Permalink
Browse files

Add gravity to bring the player back down

  • Loading branch information
MattyRad committed Feb 8, 2020
1 parent 802d331 commit f8159a222bad72a67c95c2553576eec5b65afbf4
Showing with 20 additions and 1 deletion.
  1. +20 −1 public/scripts.js
@@ -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 );
@@ -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 );

0 comments on commit f8159a2

Please sign in to comment.
You can’t perform that action at this time.