r/programming • • Jun 25 '20

A bug with a surprisingly cool side effect

https://youtu.be/us1IqknNYmw
5.0k Upvotes

249 comments sorted by

View all comments

463

u/KillTrot Jun 25 '20

I couldn't even program this on purpose

Edit: He says the same thing in the end of the vid^^

170

u/PezzzasWork Jun 25 '20

Me neither :D

16

u/TerrorBite Jun 26 '20

I agree with one of the YouTube commenters: you accidentally made a form of machine learning, it's essentially a physical problem solving algorithm. With no change in velocity possible, each collision adjusts the position of a ball away from the one it collided with, and the entire system naturally seeks out a solution where no collisions occur.

0

u/[deleted] Jun 25 '20

[deleted]

101

u/gredr Jun 25 '20

Except that here in the real world, things having the same orbital period pretty much isn't a thing, and orbits are perturbed by anything and everything. This "simulation" doesn't consider all the factors that make stuff hard.

→ More replies (11)

36

u/crazyfreak316 Jun 25 '20

I have a hunch you know nothing about simulations, airspace or satellites.

1

u/RaferBalston Jun 25 '20

Or "programming" :D

-2

u/[deleted] Jun 26 '20

I have a hunch you use your personality as birth control :)

42

u/anyonethinkingabout Jun 25 '20

I guess if you would want to program this, you would first create N random paths, and then increase the sizes of the bubbles iteratively (you can increase a bubble's size to the maximum size it could be by checking it's minimal distance to every other bubble, and then subtract the other bubble's size)

7

u/lazyl Jun 25 '20 edited Jun 25 '20

No, I think the best way would be to do exactly this, only intentionally. Just start with random paths and sizes and then simulate the collisions until reaching a steady state.

10

u/KillTrot Jun 25 '20

Did the bubble size change in the video? I thought it was only the paths which changed

28

u/anyonethinkingabout Jun 25 '20

no, you calculate the bubble sizes beforehand, before rendering

4

u/KillTrot Jun 25 '20

Well yes, thats a point. But still, you need an algorithm which self-corrects their path afterwards or am I missing smt?

13

u/anyonethinkingabout Jun 25 '20

no, you first generate the paths for the bubbles, then you enlarge every bubble such that they never touch when traveling on those paths

2

u/KillTrot Jun 25 '20

You mean you would make the collision in the beginning artificial and then with every collision you would artificially correct their path to the beforehand calculated one?

36

u/Piranha771 Jun 25 '20 edited Jun 25 '20

No.

  • You make a bunch of tiny tiny dots.

  • Now you let them orbit around the gravity center with random start positions/velocities (simulation).

  • Delete all tiny dots in simulation that collide.

  • Check for every tiny dot that is left, what was the nearest distance to any other dot during orbit. Increase the radius of tiny dot to that minimal distance.

  • Run simulation again with same positions/velocities but without the dots that had collision and with increased dot size as calculated.

EDIT: I know that it's not how it actually works. It was an answer to "How could I implement this, if I want that behavior on purpose"

33

u/efstajas Jun 25 '20 edited Jun 25 '20

Tbh it would probably be simpler to start with exclusively large dots and every time there is a hit, simply shave off exactly as much radius as is needed to prevent that collision the next time. If the dot becomes too tiny, delete it, just to make it not look messy.

If you reduce the radius instead of increasing, you get rid of the whole complexity of figuring out the maximum possible radius based on other dots orbits.

Anyway, both of these approaches might take quite a while to run if the system isn't periodic over a reasonable time-frame.

13

u/PezzzasWork Jun 25 '20

It could work I think. It is funny to see how complex it is compared to a bug :D

6

u/Salamander014 Jun 25 '20

That is a genius way of getting the same affect, but I would be much more interested in how the above actually works. The above comes from some sort of physics, the ball sizes are predetermined and the engine figures out the optimal paths. That is way more interesting than just forcefully generating this effect.

3

u/FVMAzalea Jun 25 '20

Wouldn’t you want to increase the radius by half of that tiny distance between them, at least in some cases? Because for the dot that you were minimally close to, it may form a pair where you were the dot that that dot was also minimally close to, so you’d both increase by the distance between you and end up colliding.

Unless it can be proven that no such pair will ever exist (which I don’t think is the case - I mean certainly not with just 2 dots for example because you’d always be minimally close to the other dot because there is no other option).

And then because you’re only increasing by half the distance, you wouldn’t be maximally big in the cases where you didn’t form a pair...maybe you could mitigate this by checking for pairs and adding half when you’re in a pair and full when you’re not which would add a little complexity to each dot calculation (O(the number of dots)) but not much.

2

u/soniiic Jun 25 '20

you're missing that at the start there is a collision at the centre, and then the dots all begin their non-colliding paths.

3

u/Kzrysiu Jun 25 '20

I had exactly the same thought while watching the video.

1

u/DanJOC Jun 25 '20

Create two sets, one of sin waves of n members, and one of radii r also of n members. Then set the speeds and phases for the particles across the sines such that for each n, only that n is in the neighbourhood +/-r. You'd end up with a bunch of possible speeds and phases for each n, according to their r. You're then limited by the number of particles to include and the speeds you can display. You could probably set it up in 20 or so lines.

1

u/Multipoptart Jun 25 '20

I couldn't even program this on purpose

Pretty easy actually. Two things caused this:

  1. No energy loss on collisions means that the total kinetic energy of the system will always be the same. In a real simulation you want kinetic energy losses turning into heat.

  2. Instead of using the formula for gravity, in which the force applied towards the center is proportional to the square of the distance, he used a force that's directly proportional to the distance, linearly. This makes it so that the orbital period of every object will always be the same, so they naturally tend to find a harmony.

So basically as soon as the balls collide with each other, it puts them on different orbits, but since they all orbit harmoniously that dramatically reduces the chances of collisions. If there are any more remaining collisions, they keep moving the balls apart until they no longer collide, so in a few seconds you end up with a system where no balls can collide anymore.