r/programming • u/PezzzasWork • Jun 25 '20
A bug with a surprisingly cool side effect
https://youtu.be/us1IqknNYmw463
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
→ More replies (13)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.
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
→ More replies (9)→ More replies (2)3
198
u/amaurea Jun 25 '20
The source code is here.
I agree that this is a cool bug, and if someone asked me to make an algorithm that achieves this, I'm not sure I could have done it. N-body systems like this tend to be very unstable, so I would have thought that fine-tuned orbits like these would be both hard to find and short-lived.
88
u/PezzzasWork Jun 25 '20 edited Jun 25 '20
The orbits are indeed surprisingly stable. However a slight change can lead to cascading degeneration very quickly as they are all very close to each other.
33
u/justausedtowel Jun 25 '20
Check out Veritasium's video on Butterfly Effect and Chaos Theory. The patterns of motion he talks about is surprisingly similar to the ones your software produces.
11
u/PezzzasWork Jun 25 '20
I will check this!
14
u/SayWoot Jun 25 '20
Destin from SmarterEveryDay also made a video about bird, that uses a similar Pattern motion when they fly in groups https://www.youtube.com/watch?v=4LWmRuB-uNU
6
48
u/loup-vaillant Jun 25 '20
This is not an N-body system.
All bodies have the same orbital period, regardless of their orbit's size.
The ellipses don't have any eccentricity. The centre of the system match the centre of the ellipses, not their foci.
The different objects quite clearly don't influence the trajectory of the others. Each object just turns around the centre.
Still pretty and impressive, but not physical at all.
10
u/PezzzasWork Jun 25 '20
Yes true, the initial aim of the code was just to create some animations for a data visualization project with no care about accuracy or physics. Just have a visually cool thing for a school project.
→ More replies (1)5
u/vektordev Jun 25 '20 edited Jun 25 '20
You're right in conclusion, but only the last reason is relevant. It's not a N-body-system because the objects don't interact gravitationally. There's an invisible "sun" that does all the graviticking, so all the chaos of n-body systems disappears... well, most of it anyway. That's what you meant with your last point, I think.
The fact that the gravity scaling is appropriate for 2d physics (scaling 1/r instead of 1/r2) doesn't really matter. I'm reasonably certain n-body systems with that kind of gravity aren't any less chaotic. (Gravity scales in 3d according to how hypothetical "graviton density" would scale. That is to say, according to the square of the radius, because the surface of a ball with the given radius is the area they would be spread over. Gravity that scales according to how physics would work in a 2d universe would scale with 1/r because the surface of the ball is now the circumference of a sphere. The resulting behavior is that of centered ellipses.) This is actual gravity at work here, just weird gravity.
The orbital periods are an interesting emergent behavior here. I'm not sure what's going on there, whether that's a natural consequence of "weird gravity", that suddenly now orbital periods are so different that we can have way different orbits result in the same period.
PS: Apparently, we've got a special case of weird gravity here: scaling with r, not with 1/r or 1/r2. Yeah, that'll do. At that point, equations for a generic pendulum will apply, and everything always has the same orbital period. https://github.com/johnBuffer/NoCol/blob/master/src/main.cpp - Line 98. Adjust my 2nd paragraph accordingly. Funny how they can appear so similar.
46
u/eras Jun 25 '20
I guess it's also self-correcting, so if a planet does collide another one a bit (in the video they are barely but still touching when going by), it would be automatically corrected to a better trajectory with a very small change.
31
u/PezzzasWork Jun 25 '20
Yes it will, sometimes going through a chaotic phase
0
Jun 25 '20
[deleted]
16
u/PezzzasWork Jun 25 '20
Here is the whole process with no speed up for 20 objects.
https://www.youtube.com/watch?v=mw1m-WGgaAk&feature=youtu.be
→ More replies (2)→ More replies (1)9
Jun 25 '20
Yeah, it's just a case of having that transient phase at the beginning which corrects the orbits until we end up in the steady-state. Very cool. I imagine that programming something which never collides at all would be an interesting challenge.
→ More replies (3)115
u/DuffMaaaann Jun 25 '20
Typically, n-body systems involve some attraction between individual particles. This attraction is chaotic for 3 or more bodies.
In the video it looks like bodies are only attracted to the center, not each other.
56
u/Miyelsh Jun 25 '20
That makes perfect sense! Then the elastic collisions of the particles keep happening until they all have independent orbits. Kind of like an orbital natural selection.
11
6
13
14
u/Perkelton Jun 25 '20
Does the system start off in a stable state or does it converge after a few iterations?
The latter is at least something I can see happening (the objects nudging each other into a stable path).
43
u/PezzzasWork Jun 25 '20
It stabilizes after some iterations, at the beginning everything is colliding
11
Jun 25 '20
So... it looks like the bug is "forgot to sap vel on collide" - so the objects are shifted away, but they keep moving? That's a relatively easy switch to pull if you want to use something like this as a neat particle effect. I wonder if the same omission would work in 3d.
3
9
u/Ramipro Jun 25 '20
It seems this is a result of all particles attracting each other linearly. That's why they all seem to be orbiting in ellipses around the CoM, because all their interactions are linear and equivalent to a single attractive force from the center.
I'm also hypothesizing that all the spheres have the same mass, that's why they seem to orbit with the same period.
7
u/PezzzasWork Jun 25 '20
They indeed have the same mass and they are actually just attracted to the center of the screen. It is a really basic simulation. It should be quite easy to tune it to include mass or attraction interactions though.
6
u/infk3y Jun 25 '20
Mate, that awesome music in the video! What is it?
10
u/PezzzasWork Jun 25 '20
It's from https://freepd.com/ (in this case it is https://freepd.com/music/Screen%20Saver.mp3) very nice site!
2
6
u/sephirothbahamut Jun 25 '20
Maybe you only coded the change in direction upon collision, but without any decrease in speed/momentum? In that case they'd keep colliding and changing directions until the system ends up in a situation of equilibrium.
5
5
3
3
u/mobydikc Jun 25 '20
This is all in 2D right? I think that's easily lost in this amazingness.
There may be something mathematically significant about that. Nice work.
4
u/PezzzasWork Jun 25 '20
Yes it is in 2d, It can be extended to 3d trivially though
5
u/mobydikc Jun 25 '20
Yeah, but it in 3d it also seems like it be more mundane.
To see your 2d simulation move and seemingly dodge and weave it makes my mind think I'm watching something that is moving in 3 dimensions. And then realizing its just 2d is like "whooah"
Watching it makes me think of this:
https://en.wikipedia.org/wiki/Evolutionarily_stable_strategy
5
2
u/Miyelsh Jun 25 '20
Is the potential in this system the inverse of distance from the center? Like someone else said, the potential doesn't seem to scale with radius of the balls, so is it something like V(r) = k/r ?
2
u/PezzzasWork Jun 25 '20
The force is indeed proportional to the distance and all the masses are the same but it can be tweaked quite easily
2
Jun 25 '20
Nice feature. Also CERN wants your resume.
1
u/PezzzasWork Jun 25 '20
Ha ha I don't think they want me to work on any simulation things after seeing this xD
2
u/alisey Jun 26 '20
I made a crude port to TypeScript if anyone wants to play in browser. Orbits stabilize in a couple of minutes.
1
100
u/kimcawin Jun 25 '20
a nice loading indicator
41
u/ShinyHappyREM Jun 25 '20
40
u/DrDuPont Jun 25 '20
That is an oddly suggestive name
7
u/sysop073 Jun 25 '20
Especially since they were definitely called spinners, and somebody felt the need to rename them
→ More replies (1)
71
u/Godof Jun 25 '20
8
u/bagtowneast Jun 25 '20
This is exactly what I was thinking. With, basically, no background in this sort of problem solving, I wonder if there's an reasonable collision avoidance algorithm lurking in here.
5
u/SirClueless Jun 25 '20
Yeah, probably. Take a reversible or periodic simulation. If anything collides, move it slightly (in either 3d space or in time) til it's not colliding. Get back to a new initial state either by reversing time or continuing for one period. Do this until nothing collides.
This probably doesn't work very well at all (would take a long time to converge if it ever does) for any chaotic simulation. But this simulation is extremely well-behaved in that every object is a perfect oscillator.
→ More replies (3)3
150
u/YeeetThisYeast Jun 25 '20
A bug with cool side effects = feature
56
u/dr1fter Jun 25 '20
Right, now you just have to slap some minimal game design on top and you're done.
29
0
→ More replies (1)3
65
u/defietser Jun 25 '20
You should crosspost this to /r/oddlysatisfying. Because it is.
3
u/HighGaiN Jun 25 '20
It might be more interesting to /r/physics or /r/maths. I know it's not exactly science but the way the system becomes stable could be something interesting to astronomy? :P
→ More replies (1)0
86
35
u/Johnothy_Cumquat Jun 25 '20
It's like you learned to fly by falling and missing the ground
6
13
u/crazymonmon Jun 25 '20
Reminded me of the video about flocking birds from smarter every day channel.
7
6
5
u/zhujik Jun 25 '20
Huh, this is really interesting. Is there some field in mathematics or physics that are studying specifically things like that (orbiting objects not colliding)?
4
5
Jun 25 '20
Coolest thing on reddit in the last decade or so. I could just run this code to watch it for hours and hours.
4
4
u/shakelfordbase Jun 25 '20
This is what I picture computer-controlled intersections of the future look like.
1
1
u/AbrocomaTop5436 Jun 26 '20
How many objects can you simulate?
1
u/PezzzasWork Jun 26 '20
A lot, complexity is O(n2) but since the computations are very light it can handle more than 100 or 200 easily (in C++ at least)
2
3
3
3
u/postblitz Jun 25 '20
Now increase the size to several million light years and increase the amount of particles to several trillion and boof: you've created the (metastable) universe.
2
u/RoguePlanet1 Jun 25 '20
Put a negative sign in front of those same numbers, and boom: particle physics.
2
2
2
u/kolumreto Jun 25 '20
Definition of dumb luck
5
u/Gumichi Jun 25 '20
I don't see luck being involved at all. Given that orbital parameters only ever change when there's a collision, a steady state of no collisions is a very likely outcome of a system like this. I see 2nd law of thermal dynamics at work.
3
2
2
u/KernowRoger Jun 25 '20
So I'm assuming once they bump each other into the correct orbit it's stable as they don't attract each other. There's something very satisfying about watching a chaotic system find balance like this. Great job!
2
Jun 25 '20
Oh wow, this is interesting. Thanks for providing the source, can’t wait to take a peak at this.
2
2
2
u/Necrophillip Jun 25 '20
The future of autopilots
Until some jackass gets an unnecessary bodykit going passt the mirrors
2
2
Jun 25 '20
He made a system to make them collide and got the opposite.
That seems a lot like my work outcome.
2
2
2
u/AvailableCobbler3 Jun 25 '20
happened something similar with me when I was coding a solar system physics simulation.
that day I learned about n-body problem
2
2
2
1
1
1
Jun 25 '20
Seems like they are slowing down when they go near eachother, as in a reverse gravity which ensures they don't collide and speed up when the path opens again.
1
1
1
u/NotARealDeveloper Jun 25 '20
I had the same thing happen to me when I wanted to make a simple RTS "go-to position X" command. At the end it was the same result you had: They would move towards the point, but then go further and at some point rotate in an arch going back to the point.
I made it the main feature of my game. It was in space with ships that would basically guard an area / bombard a point.
1
Jun 25 '20
Imagine this in 3D. A sphere of objects all in different orbital directions, not colliding. I guess this is because they don't have gravity on each other.
1
u/rebelsofliberty Jun 25 '20
If you can generalize the problem you might be able to enhance this into an iterative collision avoidance algorithm. This might need some research first, though.
1
u/PVNIC Jun 25 '20
Of course it takes a program designed to do collisions to make a program that has zero collisions. Maybe I should try writing a program that crashes?
1
1
u/the_notorious_beast Jun 25 '20
Dude did you just create a simulation of a solar system?! Friggin' nuts mate! I'd definitely call this a feature
1
u/jdmarino Jun 25 '20
Now do it in 3D and have the camera mounted on one of the spheres, GoPro style. ;D
1
u/M123Miller Jun 25 '20
The ball the camera follows at the end = me trying to avoid people at the supermarket when I venture out for groceries.
1
1
1
1
1
u/didzisk Jun 25 '20
One of 2019 Advent of Code days was about orbiting objects, that was my first thought when I saw this.
1
u/Perkovic15 Jun 25 '20
Pretty sure we are in recursive simulation... Random guys randomly inventing atoms. Whats next? Molecules, lets go
1
u/cw8smith Jun 25 '20
If I remember my physics correctly, an attractive force that varies linearly with distance will result in a single constant orbital period for all orbiting bodies. And if I'm reading the source correctly, it looks like that's how your attractive force works. Since the particles start with no velocity, i.e., at the top of their orbits, and they all have the same orbital period, the particles end up with synchronized orbits.
1
1
1
u/sintos-compa Jun 25 '20
I was 100% sure the bug was the infinite redirect loop I got on mobile clicking the video.
1
1
1
1
1
u/OldLadyUnderTheBed Jun 25 '20
Now we just need to upload this to a hundred self-driving cars and watch them go
1
u/ThePotatoLorde Jun 25 '20
Knowing the three body problem, this is kinda insane to look at, the simple explanation of what happened is that when they collide the velocity doesn't change when normally they slow down and stop, so after they hit eachother at the begining, they keep going and then never hit again, yet the objects don't have any mass so they aren't attracted to each other which is what causes the three body problem to be a problem.
1
1
u/e_mendz Jun 25 '20
This looks like a "bug" that can inspire a solution to collision free autonomous driving... or perhaps automated air traffic control, or satellite orbit management (if there's such a thing).
1
1
u/louknew17 Jun 25 '20
This is like how dark matter behaves, interacts only gravitationaly with itself and ordinary matter!
1
u/dumdedums Jun 25 '20
It's pretty cool. I think the bug is that what was supposed to happen was a completely inelastic collision and the bug was that all the objects had completely elastic collisions. The 'gravity' kept bumping the balls together until they oriented themselves where they wouldn't hit and since all kinetic energy was maintained, they never slowed down which ensured they would never stop.
1
1
1
u/bradrlaw Jun 25 '20 edited Jun 25 '20
Would be an interesting (and scary) algorithm to apply to a drone swarm. Could make them follow a path while moving about seemingly at random without colliding into each other.
Each drone would be the same size, but you would have a virtual diameter around each drone to simulate the mass. Then over time you have the "mass" randomly change over time at a rate fast enough to not cause collisions, but fast enough for a viewer to have a really hard time predicting where a drone is going to be making for a seemingly "random" swarm of drones moving on a path.
Edit: I see now its not really simulating gravity / attraction, but could still alter it to do the above.
1
1
1
1
1
1
u/Red5point1 Jun 26 '20
Would be neat to apply the logic to a swarm of drones and watch them fly around at high speeds criss-crossing each other without colliding.
I think you have inadvertently come up with a safety chaos navigation solution.
1
1
u/Dobias Jun 26 '20
Thanks for the nice post and the inspiration. I re-created your effect in three dimensions, and it works fine. :)
1
1
1
u/Bored_comedy Jul 10 '20
Aw I'm so confused someone please explain how this went from collisions to no collisions?
1
1
u/Fancy_Mammoth Jun 25 '20 edited Jun 25 '20
So, you basically created a simulation of electrons flying around the proton neutron core of an atom. That's pretty freaking cool.
Edit: Can somebody explain to me why I'm being downvoted for making a observation about what this looks like?
2
u/notyourmother Jun 25 '20
That was my first thought as well. The second was that all of existence might be a bug instead of a feature as well somewhere haha
1
0
0
0
Jun 25 '20
[deleted]
3
u/PezzzasWork Jun 25 '20
There are no energy bleed since velocities are not updated after a collision. The only way for an object to lose speed is to be pushed toward the center because this will make it to have a lower attraction force (since it is proportional to the distance)
0
Jun 25 '20
[deleted]
3
u/PezzzasWork Jun 25 '20
Hmm you're right. The whole energy of the system is conserved in this case.
581
u/viscence Jun 25 '20
The bug made the orbital periods for all objects the same, independent of mass, orbital distance etc. It also looks like the objects are only attracted to the system's centre of mass, not each other. The collision system effectively re-randomised all the objects on collision courses. As soon as all the objects miss each other for one orbit, the system can no longer change.