Skip to content
Swizec Teller - a geek with a hatswizec.com

Livecoding #17: Particle physics is hard

This is a Livecoding Recap – an almost-weekly post about interesting things discovered while livecoding ?. Shorter than 500 words. With pictures. You can follow my channel, here. New content almost every Sunday at 2pm PDT. There’s live chat, come say hai ?

Did you know particle physics was hard? I had no idea.

I wanted to inject interestingness into the animated particles experiment. Take a random animation of a bunch of particles and turn it into a simulation of the forces between charged particles. It would look so totally cool!

It did not go so well.

Sure, I had some idea that deriving electrostatic force equations from scratch was hard, but it’s 2016, we know this stuff! Teachers show it to us in middle school and high school and college too. How hard can it be?

Pretty hard.

The Wikipedia article on electric fields did not help. It has a bunch of equations, but none that seem useful. Or maybe my reading comprehension is bad when I’m on camera.

The article does link to Coulomb’s law, which looks a lot like the equation a theoretical physicist explained in the live chatroom. I think his name was zimaguy, but livecoding.tv won’t let me scroll back in the chatroom to check.

How cool is it that we had a real life theoretical physicist on the live stream? Wow.

The formula he gave says that force between a static charge and a particle is equal to 1/r^2, where r is the euclidian distance between two points. We get it using Pythagorus’ formula:

dx = x1 - x2
dy = y1 - y2
r^2 = dx^2 + dy^2

Remember middle school maths and asking the professor “But miiiissss when will I ever need this in real life?”. When you’re coding cool stuff, that’s when.

The best part of this formula is that we use r^2 and don’t need a square root. Roots are expensive, multiplication is cheap.

The tricky part is turning this scalar into a vector. The debate with zimaguy went back and forth and I had to settle for a rough approximation. It only understands 4 directions (Up/Down, Left/Right) and misses all of the beauty of vector mathematics, but the end result looks good enough.

We use the calculation for each static charge. Like this:

// reducer that moves each particle
p.x += vx * multiplier;
p.y += vy * multiplier;
state.charges.forEach((charge) => {
let dx = p.x - charge.x,
dy = p.y - charge.y,
r2 = dx * dx + dy * dy,
rX = (dx < 0 ? -(1 / r2) : 1 / r2) * charge.strength,
rY = (dy < 0 ? -(1 / r2) : 1 / r2) * charge.strength;
p.x += rX;
p.y += rY;
});

First we calculate each particle’s new position, then go through the list of charges, and calculate electric force effects. Movement has to happen first, otherwise the charges have a delayed effect that looks weird.

And voila.

This week I want to play with an interesting dataset of some sort. Something that takes a lot of moving parts to showcase … ideas?

PS: the edited and improved versions of these videos are becoming a video course. Readers of the engineer package of React+d3js ES6 get the video course for free when it’s ready.

Did you enjoy this article?

Published on August 4th, 2016 in Front End, Livecoding, react, Technical

Learned something new?
Want to become a high value JavaScript expert?

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️