chrisdickinson/voxel-physical — pull request #10 by deathcap (2014-05-03 20:00:57+00:00)
Title: Fix fall distance calculation

With the changes for https://github.com/chrisdickinson/voxel-physical/pull/9 in https://github.com/chrisdickinson/voxel-physical/commit/731b9338414b17b0d7cd663c5dc12366a740c148 I'm not seeing the fall getting detected. With this PR it is, though I'm happy to iterate further (it reverts a couple of the other changes). A few notes:

* Added a new variable `this.rest_avatar_y`, to store the avatar Y position (previously `lastRestY` before 731b9338414b17b0d7cd663c5dc12366a740c148). Separate from the `old_rest_y` and `last_rest_y` (`old_old_rest_y`) variables — which store the last two `this.resting.y` values (needed to detect the alternating `this.rest.y` values when standing on the ground, but maybe there is a cleaner way..)

* `this.old.resting_y` was assigned twice in 731b9338414b17b0d7cd663c5dc12366a740c148:

```
- this.old_old_resting_y = this.old_resting_y
- this.old_resting_y = this.resting.y
+ this.old_resting_y = (this.old_resting_y << 1) >>> 0
+ this.old_resting_y = !!this.resting.y | 0
```

so the original value was never read. Now `last_rest_y` and `old_resting_y` store the past two values (didn't really understand what was going on with the bitshifting, but it may be possible to make that approach work, too).

Testing with https://github.com/deathcap/voxpopuli (apologies haven't come up with a smaller test case) - the player starts above the ground and immediately falls, which should trigger the `fell()` method (and ultimately play the hurt sound)