Every pulse a bat emits is a short chirp that spreads outward as a circular wavefront at the speed of sound. The bat measures nothing about the world directly โ only how long each echo takes to come back. Distance is recovered from time-of-flight alone:
d = v ยท ฮt / 2
v = 343 m/s (speed of sound)
ฮt = round-trip echo delay
Here that formula runs for real: at each pulse, 48 sample rays are cast outward from the bat and ray-marched against the cave wall (a smooth radial boundary), the rock obstacles and the moths. Whichever surface a ray meets first sets that ray's echo delay ฮt = 2d/v; the echo is drawn only once that much real time has actually elapsed, so what you see lags the true geometry by exactly the physical round-trip.
- Sonar view โ the cave stays dark; only active pulses and the echoes they trigger are drawn, fading out over the "echo persistence" you set. This is closer to what a bat actually perceives โ a decaying point-cloud sound map, not a picture.
- Full light โ the true cave, rocks and moths are shown for reference alongside the echoes, so you can check the sonar map against reality.
- Move the bat with the mouse/touch or arrow keys; get within catching range of a moth to register a catch.