Tailoring Snap! scripts a certain way opens the door to simulating motion in the study of forces, momentum, projectiles, energy and more.
Here is the secret: The script to drive sprite motion must be based on the calculation of the change in position rather than the absolute position. Robust simulations apply physics in small discrete chunks. So, here is the kinematic equation to start your thinking:
Here is an example of calculating displacement in this way, using Snap!:
Small values of delta-t result in smoother looking motion. But don't make delta-t too small. I find setting delta-t less than 0.05 seconds results in motion that takes longer than the actual motion I am trying to simulate. For advanced applications, that might be okay, but for typical use, that should be avoided.
The change in the velocity (based on the acceleration) drives the displacement. Any physics that results in a change in velocity can now be linked to motion of a sprite! Think about a simulation that includes a net force and mass to calculate acceleration. Or a simulation could focus on impulse and momentum change. A whole new world opens up!
Here is a Snap! simulation using the format shown above. Car speeds up and then slows down
The car's motion could easily be scripted by calculating absolute position, rather than displacement, but this script is definitely made easier by calculating displacement with the change-x-by block. Arrow key controls acceleration
If you want to understand more about the advantage of using the change-x-by block to move the sprite rather than using the set-x-to block, here is a 9 minute video that gets into much more depth.
Comments