Now that we've covered the basics of classical physics, let's put it all together. In this recipe, we'll simulate particle physics by modeling gravity, boundary conditions, collision damping, and floor friction.
Follow these steps to launch a particle inside the canvas and observe it's projectile path as it bounces on the walls, gradually falls down to the floor due to gravity, and then slows to a stop due to floor friction:
Link to the
Animation
class:<head> <script src="animation.js"> </script>
Define the
applyPhysics()
function which takes a particle as input and updates its position and velocity based on physics variables such as gravity, collision damping, and floor friction:function applyPhysics(anim, particle){ // physics globals var gravity = 1500; // pixels / second^2 var collisionDamper = 0.8; // 80% velocity lost when collision occurs var floorFriction = 100...