swizec.com

#### Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

# 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.

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

Semantically similar articles hand-picked by GPT-4

### Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

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

Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

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 ❤️

Created by Swizec with ❤️