Coding Challenge 184: Collisions Without a Physics Library!

The Coding Train · Beginner ·🌐 Frontend Engineering ·2y ago

About this lesson

What happens when two circles collide in a p5.js canvas? In this video, I examine the math and implement idealized elastic collisions in a JavaScript physics simulation. This video supplements the Nature of Code book series in Chapter 6 which uses 3rd party physics libraries to handle collisions rather than a direct implementation. Code: https://thecodingtrain.com/challenges/184-elastic-collisions 🚀 Watch this video ad-free on Nebula https://nebula.tv/videos/codingtrain-coding-challenge-184-elastic-collisions p5.js Web Editor Sketches: 🕹️ Elastic Collisions: https://editor.p5js.org/codingtrain/sketches/3DrBb8LCp 🕹️ Elastic Collisions - Quadtree: https://editor.p5js.org/codingtrain/sketches/z8n19RFz9 🎥 Previous: https://youtu.be/p7IGZTjC008?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH 🎥 Next: https://youtu.be/MazpwQNdJYQ?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH 🎥 All: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH References: 📕 The Nature of Code: https://natureofcode.com/ 🛒 Pre-order The Nature of Code: https://nostarch.com/nature-code 🔗 Elastic Collision: https://en.wikipedia.org/wiki/Elastic_collision 📃 2-Dimensional Elastic Collisions without Trigonometry: https://www.vobarian.com/collisions/2dcollisions2.pdf 📃 Elastic Collisions Formula Derivation: https://dipamsen.github.io/notebook/page/collisions.pdf Related Coding Challenges: 🚂 https://youtu.be/IIrC5Qcb2G4 🚂 https://youtu.be/OJxEcs0w_kE 🚂 https://youtu.be/PoW8g67XNxA 🚂 https://youtu.be/h5ZNcAPXxew Timestamps: 0:00 Introduction 0:20 The Nature of Code book 1:22 Review background material 4:15 Collision Resolution 5:42 Start Coding 8:48 Add collide() function 10:17 Momentum and kinetic energy 14:15 Line of impact 16:44 Add the formulas 21:25 Simplify the code 22:57 Check for overlap 24:31 Check the particle's kinetic energy 25:48 Fix error 27:30 Add more particles 30:11 Optimizations 30:50 Outro Editing by Mathieu Blanchette Animations by Jason Heglund Music from Epide

Original Description

What happens when two circles collide in a p5.js canvas? In this video, I examine the math and implement idealized elastic collisions in a JavaScript physics simulation. This video supplements the Nature of Code book series in Chapter 6 which uses 3rd party physics libraries to handle collisions rather than a direct implementation. Code: https://thecodingtrain.com/challenges/184-elastic-collisions 🚀 Watch this video ad-free on Nebula https://nebula.tv/videos/codingtrain-coding-challenge-184-elastic-collisions p5.js Web Editor Sketches: 🕹️ Elastic Collisions: https://editor.p5js.org/codingtrain/sketches/3DrBb8LCp 🕹️ Elastic Collisions - Quadtree: https://editor.p5js.org/codingtrain/sketches/z8n19RFz9 🎥 Previous: https://youtu.be/p7IGZTjC008?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH 🎥 Next: https://youtu.be/MazpwQNdJYQ?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH 🎥 All: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH References: 📕 The Nature of Code: https://natureofcode.com/ 🛒 Pre-order The Nature of Code: https://nostarch.com/nature-code 🔗 Elastic Collision: https://en.wikipedia.org/wiki/Elastic_collision 📃 2-Dimensional Elastic Collisions without Trigonometry: https://www.vobarian.com/collisions/2dcollisions2.pdf 📃 Elastic Collisions Formula Derivation: https://dipamsen.github.io/notebook/page/collisions.pdf Related Coding Challenges: 🚂 https://youtu.be/IIrC5Qcb2G4 🚂 https://youtu.be/OJxEcs0w_kE 🚂 https://youtu.be/PoW8g67XNxA 🚂 https://youtu.be/h5ZNcAPXxew Timestamps: 0:00 Introduction 0:20 The Nature of Code book 1:22 Review background material 4:15 Collision Resolution 5:42 Start Coding 8:48 Add collide() function 10:17 Momentum and kinetic energy 14:15 Line of impact 16:44 Add the formulas 21:25 Simplify the code 22:57 Check for overlap 24:31 Check the particle's kinetic energy 25:48 Fix error 27:30 Add more particles 30:11 Optimizations 30:50 Outro Editing by Mathieu Blanchette Animations by Jason Heglund Music from Epide
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

📰
Why Vanilla JS? In the article below, I am sharing my story of building SaaS product in vanilla js and explaining why I decided to go with this approach. https://guseyn.com/html/posts/why-vanilla-js.html
Learn why choosing Vanilla JS can be a great approach for building SaaS products and how it can simplify development
Dev.to · Guseyn Ismayylov
📰
How to Create a Cursor Tail Using HTML, CSS, and JavaScript
Learn to create a colorful cursor tail using HTML, CSS, and JavaScript for a unique user interface effect
Medium · JavaScript
📰
I built a landing page with Three.js, vanilla JS, and zero frameworks — here's what I learned
Learn how to build a landing page with Three.js and vanilla JS without relying on frameworks, and discover the key takeaways from this project
Dev.to · Ayush Shekhar
📰
Part 1: Why I Rarely Use useEffect Anymore (and what I use instead)
Learn why useEffect is not always the best choice in React and discover alternative solutions
Dev.to · Alejandro

Chapters (16)

Introduction
0:20 The Nature of Code book
1:22 Review background material
4:15 Collision Resolution
5:42 Start Coding
8:48 Add collide() function
10:17 Momentum and kinetic energy
14:15 Line of impact
16:44 Add the formulas
21:25 Simplify the code
22:57 Check for overlap
24:31 Check the particle's kinetic energy
25:48 Fix error
27:30 Add more particles
30:11 Optimizations
30:50 Outro
Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →