Coding Adventure: Solar System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Very cool. Describes it specifically from 2:35 to 4:30 but the whole video is worth a watch.

👍︎︎ 7 👤︎︎ u/jfjlax 📅︎︎ Apr 18 2020 🗫︎ replies

Aren't there technically four bodies in the book? Three suns and one planet.

👍︎︎ 15 👤︎︎ u/spankymuffin 📅︎︎ Apr 18 2020 🗫︎ replies

Nice! His program looks fun to play with

👍︎︎ 4 👤︎︎ u/raresaturn 📅︎︎ Apr 18 2020 🗫︎ replies

Absolutely fabulous.

👍︎︎ 3 👤︎︎ u/Frogmarsh 📅︎︎ Apr 18 2020 🗫︎ replies

If you like this kind of thing and haven't played Kerbal Space Program, you are really missing out!

👍︎︎ 3 👤︎︎ u/gridpoet 📅︎︎ Apr 19 2020 🗫︎ replies

What program was that?

👍︎︎ 1 👤︎︎ u/catsloveart 📅︎︎ Apr 19 2020 🗫︎ replies
Captions
hi everyone welcome to another episode of coding adventures the space is frustratingly big if this is the earth the moon is all the way over here and Mars is several months travel further out it would be much more fun to explore if the moon were about here instead and other planets were a little more accessible too because of this among other grievances I've decided to make my own solar system first we need gravity so I'll start with this famous equation which says that the force with which two bodies attract one another is equal to big G the gravitational constant which in the real world is this tiny number but it can be whatever we like wryly that's then multiplied by the mass of each body and divided by the distance between their centers squared to translate this into code I made a little script which holds some information about each body like its mass radius and initial velocity I then have a function for updating the velocity where I just loop over all the other bodies and calculate the force using that equation from a moment ago acceleration is just force divided by mass so these masses actually cancel out but I'll keep it separate for clarity finally the velocity is increased by the acceleration I have one more tiny function in here and this one just moves the body using its velocity the reason I have these are separate functions is that I then have a simulation script which first updates all the velocities and then all the positions which I found was a lot more stable than updating each body's position immediately there are fancier ways of doing this for example I looked into something called leapfrog integration but it didn't seem to make any difference at all so I guess I was doing it wrong anyway let's mess about with this a bit so if we have a planet in a moon arranged like so the moon is of course just going to crash straight into the planet but if I raise its initial velocity on the y axis I can get it to sail further and further until eventually it misses the planet entirely and goes into orbit raising the initial velocity even more will make the orbit increasingly elliptical until finally it's going so fast that it escapes the gravitational pull of the planet I could also give the planet some initial velocity here so now both bodies are moving through this lastly I'll try raising the mass of the moon and we can slowly see the path of the planet begin to undulate a little [Music] so with two bodies the orbits are fairly simple and predictable but if you add in just one more suddenly there's an explosion of complexity [Music] [Applause] [Music] you I had a lot of fun playing around with different starting configurations but inevitably there would be a collision or one body would shoot off and I'd end up with just two bodies orbiting each other in the same repeating pattern and I began to wonder if there were any scenarios in which that wasn't the case so I did a little research reading the three-body program page on Wikipedia which led me to this paper in which has listed the conditions for one possible stable orbit of three bodies so I'll move the bodies to their described positions and starting velocities and you can see how this simple figure eight orbit emerges I'd been playing around for ages and just getting crazy chaos so I was pretty blown away by the fact that such a simple orbit as us actually exists [Music] okay yeah time to make a solar system so I'll start with the Sun in the middle and I'll duplicate that out here and turn it into a blue planet I'll of course set its radius to be a lot smaller and then I need to set its mass but I have no idea what that number should be so I've rearranged the equation from earlier to calculate mass given the radius and the acceleration due to gravity on the surface I want this planet to have a gravity similar to Earth's so I'll set the value to ten finally I'll increase its initial velocity until it's going in a nice orbit around the Sun now this causes the Sun to move as well so I've got an option in my displace worked to draw everything relative to a certain body which it just does by subtracting that body's movement each iteration and so if I set that to the Sun we can see the blue planets orbit more clearly now I'm going to add another planet nearby and my goal is to make these two orbit one another as they go around the Sun I am trying to do this by adjusting initial velocities but so far it's not being very cooperative I'll just keep patiently tweaking though until it behaves this is looking better but I want the orbit to be more circular so I'll raise each bodies velocity bit by bit until it looks right with some small tweaks we can get different patterns to emerge which i think is kind of cool it's not very clear though what the planets are actually doing here so I'll quickly go back onto my visualizer tool and just increase the number of iterations up from zero I guess I could also just let it play out and see what that looks like [Music] this is better quite a lot but I think it's looking good so far so zooming out I'll add in a little green planet and adjust its velocity to bring it into a circular orbit I'd like this one to have a moon so I'll zoom in on it make a copy and make that a lot smaller then as before I'll Mack about with the velocity to get it into orbit but it's pretty difficult to tell from here if I'm having any success so I'll set the orbit display to be centered on the Green Planet and I can then try talk it into going into a circle [Music] okay yeah I want one last planet for now a large purple one with the relatively high surface gravity I see it's making the green planet and its moon wobble a fair bit so I'll move it a little further up to where it can't cause so much trouble I think I'd like this one to have two moons just for variety so I'll go ahead and create those zooming out in seeing the orbits like this you can imagine how perplexing it must be for the purple planet people looking up at the sky and seeing these peculiar loop-de-loop opens one day they'll figure out that the Sun is actually at the center and it's gonna make so much more sense looking at the moons of the purple planet especially the inner one I have a hard time believing it's actually going around in a circle even though I know it's true I guess it's the way it appears to abruptly change direction that throws me off so I made a camera follow the powerful planner around just to be sure ah isn't it cool though how all of this arises essentially from this one kind of an impressive looking equation anyway I'd really like to be able to fly around and visit the planets so I opened a blender and created this rather ugly space exploration vehicle and if i zoom in over here you can see it as an equally ugly interior but there's a fire extinguisher so at least it's safe I then coded off a simple controller this is the input code here which just lets you thrust in all directions and also rotate the ship with the mouse then over here I make the ship be affected by the gravitational pull of all the planets and apply the thrust and rotation to the ship's rigid body so I can now hop into my ship and fly about [Music] it can be quite tricky to gauge how you're moving relative to the planets so I want some sort of UI to help with that I've recently been playing a brilliant game called outer wilds and that's essentially what inspired me to mess about with this stuff in the first place but more to the point I'm going to shamelessly steal the way that they handled the UI for this which is that as you point your ship towards a planet you can see these little markers around it and if you actually press on it then you see some important information like your distance from the planet how fast you're heading towards it and with these two arrows how it's moving relative to your ship as you can see I also added in some distant stars and and where they look pretty terrible it's literally just 3000 spheres born really far away I'd like to experimental something better in the future with not just stars but maybe distant nebulae and so on but for now I'm just going to attempt to make a graceful landing on the screen planet [Music] well that was anything but graceful but in any case I'll get out of my seat open up the hatch and step out onto the surface like the ship the player is of course also affected by the gravity of all bodies but one way it's different is that it finds which body currently has the strongest gravitational pull and aligns the players up direction without any way there's really not much to see on this planet it is just a sphere after all so I'll hop back over to my ship close the hatch buckle in and take off [Music] so obviously the solar system is not terribly interesting to explore at the moment but I'd like to change that by replacing the bland spheres with some procedurally generated planets and maybe adding in some little creatures or I don't know how far I'll take it yet but it seems I can be fun to work on I'm going to leave all that stuff for future videos though because they deserve their own episodes well here we are in the moon it looks just like the planet except it's grave and you can jump really high of course [Music] the last thing I'd like to talk about for now is precision issues it's a problem that plagues space games or any very large open world really the issue is that the launderer number gets the less precise the decimal part of it will be and this can lead to one key physics and ugly rendering when the player is very far away from the center of the world for example here I have the ship on my outermost planet 50,000 units from the origin and as you can see everything is jiggling like there's no tomorrow so my crude fix for this was just to make a little script that checks how far away the player camera is from the center of the world and if it exceeds some threshold just move everything in the scene back to put the player at the center the physics engine was not a big fan of that idea at first but with a little bit of tweaking I was able to get it to behave so in the editor I have that threshold value set to a thousand at the moment but just to demonstrate I'll sell it to something really small and now as I pilot the ship you can see that it's staying pretty much fixed at the center while the moon and everything else moves around me instead of course from the players point of view everything looks normal so that's all I have for now it's all very crude and patched together the planets don't even rotate yet because that made my player slide around weirdly but I've been having a lot of fun playing with this and I'm excited to take it a little further so I hope you've enjoyed watching and until next time Cheers you
Info
Channel: Sebastian Lague
Views: 529,458
Rating: 4.9871674 out of 5
Keywords: game development, space game, simulation, physics, programming, Unity, gravity, planets
Id: 7axImc1sxa0
Channel Id: undefined
Length: 12min 12sec (732 seconds)
Published: Fri Apr 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.