Simulating particle physics

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.

Simulating particle physics

How to do it...

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:

  1. Link to the Animation class:
    <head>
        <script src="animation.js">
        </script>
  2. Define the applyPhysics() function which takes a particle as input and updates its position and velocity based ...

Get HTML5 Canvas Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.