I wanted to implement damage or loss of life on a fall. That could be done with the
on sprite of kind Player hits wall if I can determine the impact velocity. I wasn’t sure what the value of the sprite’s
vy would be when the event fires. Empirically, it works and
vy is set to what looks like a reasonable
vy impact velocity.
Reading Game Loop I am not sure if this is behaviour that is well defined. In the
Physics Step it says:
- If the sprite is not a ghost and a tile map exists, the sprite is checked for collisions with the tile map. If it has hit something, the sprite may be stopped and an collision event with that type of wall is run.
That could be read as “stopped” meaning
vy is set to be stationary and then the collision event is processed. That’s not what happens. It would be useful if those documents could address exactly when
vy are altered based on collisions.
Here’s my egg-breaking demonstration code:
And here’s first run:
And a second run where I’m messing with the sim a bit by opening another tab to a popular video site whilst it’s running:
This is of lower importance to me but there are a few mysteries in that data:
- The first two times the event fires both have the impact velocity. The variation is reasonable but the fact the impact velocity is in two events is peculiar and non-intuitive.
- When the sprite stops
vyis not set to 0 - it has a value of around 0.4. This is clearly not a floating point rounding issue but something else.
- The rest value of
vyfluctuates here, more so when other things are going on in the browser.
Perhaps 2 is due to
ay being set and it attempting to move the object but the object is not moving so the non 0
vy isn’t really correct. I’d imagine 3 is just 2 exacerbated due to a larger delta-t?