Part 4 of 13 in Ghostveil
Dropping a stone into a pit
A stone in Ghostveil now drops into a pit like a slab of concrete, and counting its bounces once froze the game.
A stone pushed into a pit in Ghostveil used to slide down into it at an even speed while the filled pit's slab dithered in over it. I wanted it to land the way a concrete slab does when it's dropped on the ground: one hard hit, two small bounces, each lower than the last, dust at every impact, and then it settles. Here it is at full speed, in the corner of a room where the stone goes into the pit beside the door.
It's over in 0.9 seconds.
The sums
The stone starts at rest and has to reach the bottom of the pit in a fixed time, s. Measuring the drop as a share of the way down, from 0 at the top to 1 at the bottom, gravity has to be
and the stone hits the bottom at speed . Each bounce leaves with 45 per cent of the speed it came down with, , which keeps the bounces low. Bounce leaves at , stays up for , and rises of the drop. With these numbers the impacts land at
where is the landing and and are the moments the first and second bounces come back down. The two bounces rise 20 per cent and 4 per cent of the drop. In code the fall is a pure function of time:
/// The impacts: landing, then each bounce coming back down.
static final List<double> impacts = () {
final out = [fallTime];
var v = _g * fallTime;
for (var i = 0; i < 2; i++) {
v *= restitution;
out.add(out.last + 2 * v / _g);
}
return List<double>.unmodifiable(out);
}();
/// How far down it is [a] seconds into the fall, as a share of the way
/// to where it rests: falling under gravity, then each bounce a smaller
/// arc under the same gravity.
static double fallen(double a) {
if (a <= 0) return 0;
if (a < fallTime) return 0.5 * _g * a * a;
var v = _g * fallTime;
for (var i = 1; i < impacts.length; i++) {
v *= restitution;
if (a < impacts[i]) {
final t = a - impacts[i - 1];
return 1 - (v * t - 0.5 * _g * t * t);
}
}
return 1;
}
Drawn out over the first 0.3 seconds, that's this curve:
The view multiplies the curve by how far the stone has to go, 19 art pixels for a stone with no fire in it, and rounds down, so the stone moves in whole art pixels like everything else in the room. After the last impact it rests for 0.2 seconds, sinks the last two pixels until its top is flush with the floor, and the floor's own slab fades in over it, done at 0.8966 seconds.
The bounce that froze the game
The first version of this counted the impacts as it went. Each frame, the stone's view checked whether the clock had passed the next impact, told whoever was listening, and moved its counter on:
while (_impact < _impacts.length && a >= _impacts[_impact]) {
onLand?.call(this, _impact++);
}
That works while something is listening. In a Daily race the ghost replays a recorded run on a board of its own, and nothing listens to the stones on that board. In Dart, onLand?.call(...) with onLand null skips the whole call, arguments included, so _impact++ never ran. The counter stayed where it was, the loop's condition stayed true, and the game froze in the middle of the race the moment the ghost's stone landed.
Rather than moving the ++ out of the call, I took the counting out of the view altogether. It now asks the fall which impacts happened between the last frame and this one:
for (final impact in SlabFall.impactsBetween(fall, a)) {
_sink = 1;
onLand?.call(this, impact);
}
impactsBetween reports an impact when its time falls after the previous frame and by the current one, so each impact turns up exactly once however long or short the frames are. A test runs the fall at frames from a 240th of a second up to half a second apart and checks it sees impacts 0, 1 and 2, once each, every time.