Microsoft MakeCode

Wall collisions and sprite vy

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:

  1. 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 vx/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:

  1. 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.
  2. When the sprite stops vy is not set to 0 - it has a value of around 0.4. This is clearly not a floating point rounding issue but something else.
  3. The rest value of vy fluctuates 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?

I observe in the sim that the latest version (v0.14.9) now only has the impact velocity once rather than twice for this. This change appears to make sense and will hopefully this behaviour will be preserved.

The vy values for a touching sprite still fluctuate a little around 0.4.