Skip to content

Commit f8159a2

Browse files
committed
Add gravity to bring the player back down
1 parent 802d331 commit f8159a2

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

public/scripts.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ scene.add( obstacle );
2525
camera.position.z = 5;
2626

2727
var jumping = false;
28+
var jump_distance = 0.1;
29+
var acceleration = 0.0025;
30+
var acceleration_step = 0.0025;
2831

2932
var animate = function () {
3033
requestAnimationFrame( animate );
@@ -40,9 +43,25 @@ var animate = function () {
4043
}
4144

4245
if (jumping) {
43-
player.position.y += 0.02;
46+
acceleration_step = acceleration_step * (1 + acceleration);
47+
48+
jump_distance -= acceleration_step;
49+
50+
player.position.y += jump_distance;
4451
}
4552

53+
if (player.position.y < initial_y) { // hit the "ground"
54+
jumping = false;
55+
56+
// reset our acceleration calculations
57+
jump_distance = 0.1;
58+
acceleration_step = acceleration;
59+
60+
// may have overshot, hard reset
61+
player.position.y = initial_y;
62+
}
63+
64+
4665
for (var vi = 0; vi < player.geometry.vertices.length; vi++) {
4766
var localVertex = player.geometry.vertices[vi].clone();
4867
var globalVertex = localVertex.applyMatrix4( player.matrix );

0 commit comments

Comments
 (0)