- For more information, see Projectile motion on Wikipedia.
Projectile motion describes the motion of an object launched upwards and let to fall under the force of gravity. Without any other forces such as air resistance, the object's path forms a parabola.
Example Uses
Objects experiencing the force of gravity are abundant in Scratch projects. Examples include:
- Balls thrown in sports
- Arrows and cannonballs in fighting games
- The player character jumping and falling in a platformer
- Particles and debris scattered from explosions
Simulation
Simple Iterative Example
The following is a simple 2-dimensional example script for how an projectile motion can be simulated.
when gf clicked go to x: (-50) y: (-50) // initial position set [velocity.x v] to [2] // initial horizontal velocity set [velocity.y v] to [8] // initial vertical velocity forever change [velocity.y v] by (-0.3) // decrease velocity due to gravity change x by (velocity.x) change y by (velocity.y) end
Units of Measurement
The simple example shown above makes many assumptions on the units of measurement used. The loop runs at a fixed frame rate of 30 FPS to look correct.
This next example shows how real-world units of measurement can be used, namely meters for length and seconds for time. A variable, (dt) will be used to store delta time — the time since the last loop iteration.
when gf clicked set [position.x v] to [-50] // initial position in meters set [position.y v] to [-50] // initial position in meters set [velocity.x v] to [60] // initial horizontal velocity in meters/sec set [velocity.y v] to [240] // initial vertical velocity in meters/sec forever set [dt v] to (timer) // get the time since the last frame, which is about 0.033 sec when running at 30 FPS reset timer change [velocity.y v] by ((-9.8) * (dt)) // decrease velocity due to gravity, -9.8 m/s/s change [position.x v] by ((velocity.x) * (dt)) change [position.y v] by ((velocity.y) * (dt)) go to x: (position.x) y: (position.y) // assuming that 1 pixel on the screen is 1 meter end
Simulating Air Resistance
Velocity Limit
Unrealistic but often seen in game player characters. Strongly controls behavior and can for example prevent collision detection issues from moving too fast.
if <(velocity.y) < [-10]> then // prevent falling too fast set [velocity.y v] to [-10] end
Velocity Decay
Again unrealistic but provides a limit on speed that is less sudden then the above.
set [velocity.x v] to ((velocity.x) * (0.99)) set [velocity.y v] to ((velocity.y) * (0.99))
Drag
- For more information, see Drag (physics) on Wikipedia.
Example Projects
- Example: Projectile Motion by kriblo_test
- Falling Particles by awesome-llama-test
- Ball Trajectory by awesome-llama-test
- Bloomfire by s-studios