p5.js Coding Tutorial | Flocking Simulation with Quadtree 🐀🐀🐀 @pattvira
p5.js Coding Tutorial | Flocking Simulation with Quadtree 🐀🐀🐀  @pattvira
Uploaded September 2024 | Updated November 2026, 1 hour ago
🌴 Watch this first if you have no idea what a quadtree is: youtu.be/7pxyIC_ZEwA
--
CODE UPDATE:
πŸ–‹οΈ Inside sketch.js β€” draw() function:
- Created two for loops, instead of one, to separate inserting and querying points in the quadtree. With just one for loop, all the boids did not have the chance to interact with a fully updated quadtree.
πŸ–‹οΈ Inside boid.js β€” Boid's class & update() method:
- Switched the order of this.velocity.add(this.acceleration) and this.position.add(this.velocity). Velocity should be updated first, then apply to the position vector.

LET'S CONNECT
✨ Sign up for my newsletter at pattvira.com
✨ Instagram: instagram.com/pattvira
✨ Twitter: twitter.com/pattvira
--
Links:
πŸ”— p5.js editor: editor.p5js.org
πŸ”— Quadtree (Part 2 of 2) Code: editor.p5js.org/pattvira/sketches/v_tmN-BC5
πŸ”— Introduction to Creative Coding Playlist: youtube.com/playlist?list=PL0beHPVMklwgMz4Z-mNp4_udo9mjBk7pn&si=XhvQaojd6cIrxvdn

References:
πŸ”— Boids by Craig Reynolds: red3d.com/cwr/boids
πŸ”— Nature of Code - Chapter 5: Autonomous Agents: natureofcode.com/autonomous-agents

Credits:
πŸ”— Thumbnail: raw.githack.com/mrdoob/three.js/dev/examples/webgpu_compute_birds.html

Timestamps:
0:00 Intro
0:23 Quadtree quick recap
0:51 What is a flocking simulation?
02:20 Agenda and approach
05:05 Create a Boid class
16:16 Write an Alignment method
24:39 Write a Cohesion method
32:37 Write a Separation method
40:30 Change from p5.js. to p5.min.js to improve performance
41:59 Create a QuadTree class
45:55 This is where the magic happens ✨
p5.js Coding Tutorial | Flocking Simulation with Quadtree 🐀🐀🐀p5.js Coding Tutorial |  Interactive Grid with Mouse Locationp5.js Coding Tutorial | Interactive Hexagonal Truchet Tiles with ml5.js (Part 3/3)Creative Coding Math: Oscillation - Simple Harmonic Motionp5.js Coding Tutorial | NileRed Logo (BZ Reaction)p5.js Coding Tutorial |  Draw & Color ShapesJoy Divisions Unknown Pleasures album cover #p5js #javascript #creativecoding #coding #shortsp5.js Coding Tutorial | Menger Spongep5.js Coding Tutorial | Collisions Without a Physics Library!First Year on Youtube & Whats Next!Sound Mini Series | EPISODE 1 - p5.Oscillatorp5.js Coding Tutorial | Image Manipulation - Pixelated Techniques
Patt Vira |

p5.js Coding Tutorial | Flocking Simulation with Quadtree 🐀🐀🐀

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER