This website is inspired by Daniel Shiffman’s excellent book/website The Nature of Code. I came across this when I was investigating coding tools that might allow me to create a little project I had in mind to simulate bouncing balls. Well, The Nature of Code was an awesome introduction to many of the things I was trying to do, and introduced me to Processing and P5JS. But it did not quite get to the nitty gritty of handling collisions. Daniel discussed this topic in the book, and despite covering many helpful concepts, he demurred and instead bypassed the question and moved on to how various physics libraries allowed us to handle the problem.
In many ways this is sensible, as the physics of collisions and dynamics can get complicated pretty quickly. But I thought there was an opportunity to give a little taste of the topic with simple explanations and code examples. So that is what I have tried to present here.
I remember learning about some of the physics concepts in year 12 of school – conservation of energy and momentum etc…
There was even a question about it in my Year 12 Physics exam – I have no idea why I remembered this. Here it is – straight from the South Australian State Records Centre:
Anyway, this website is for anyone who is interested in the simple physics of collisions and how to simulate them in code. I would recommend some familiarity with The Nature of Code, especially Chapter 1 – Vectors and the beginning of Chapter 6 – Physics Libraries. Like The Nature of Code, all the code I present is written in P5JS, so some familiarity with that would also be useful. Finally, I indulge in a bit of simple algebra to derive the functions we need to handle the collisions. It’s not complex, but you may need to take it slowly if your algebra muscles are as rusty as mine.
Below is a sketch to give you an idea of where we are heading. We will build up to it piece by piece. Enjoy…