Unity Tutorial - Solar System with Unity Physics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up coders i'm kaderius and today another quick video about space we will create the basic solar system and make stuff orbit each other physics and mathematics are complex topics but don't worry we'll just use easy formulas and our solar system will orbit forever before we start take a moment to like and subscribe and i see you in unity alright the sun you see on the screen was made in shadowgraph i explained it in the previous video that you can watch here by here i mean in the card which should pop up in the top right corner of the screen you can also just start an empty erp project and use a default sphere for the sun resizes in space are huge and when i mean huge i mean astronomical the sun could contain 1.3 million earth in terms of volume so to still be able to see objects we will cheat a bit with the distances and sizes i made a table with the sun and the planets with their respective mass in earth's mass radius in kilometers and distance to the sun in million kilometers let's say our sun has a size of 696 that's actually the radius so for the side we should double it to have the diameter but anyway as i mentioned we'll make some adjustments to have a nice visual we add a rigid body with a mass of 333 000 that's the mass of the sun in earth mass we'll code our own gravity so let's uncheck the use gravity checkbox let's add the earth at a distance of like 1510 it's the distance in million kilometer multiplied by 10. it's not real but at least we can build the solar system in a consistent manner it's the size will be 6.3 we had a rigid body with a mass of one as masses are in earth mass so the earth has a mass of one and we disable gravity as well okay we can also add a tag for object in order to retrieve them easily in the code later on i just call them celestials then we can add our solar system management for that we create an empty game object let's call it solar system and add a script to it with the same name open it in visual studio to start editing in many astrophysics formula we find the gravitational constant represented by the letter g it's a constant number that was empirically calculated to make newton and einstein formulas work in this example we will also adjust it to control the speed at which our space is running the real number is very small but we'll use a value of 100 in this example as we would need to wait a year for the earth to make a full circle then we need an array to store all our celestials in the start function we populate our array with all celestials with the function find game object with tag okay i told you we use only two formulas the first one is gravity so let's create a void function and call it gravity in there we will just iterate through all our objects with a double loop and apply the gravitational force as we've learned in school the gravity force is equal to g multiplied by the product of the masses divided by their squared distance of course an object is not orbiting itself so we apply gravity only if the object is not equal to itself we get the respective masses calculate the distance and add the force the rigid body component in the direction of the central object [Music] all right let's execute this function in the fixed update save and see how it goes [Music] great it seems to work the earth is falling into the gravitational field of the sun now how to make these things orbit each other i made some research and the instance orbital speed for circular orbit is the square root of the gravitational constant multiplied by the central object mass divided by the distance that's applicable when the mass of the orbiting object is negligible like the mass of the earth compared to the mass of the sun let's create a new void function and call it initial velocity we do the same and go through the list of celestials we retrieve the mass of the central object their distance and oriented the object to face the central object that will be easier to apply the velocity with the right vector actually all objects are subject to gravity relative to that distance and that's also true for the orbital speed so in this case for each object we will add velocity depending on the surrounding object as we are facing the central object we can use the right vector as a direction then we just multiply by the formula explained earlier ok save and back to the editor to see how it looks like awesome our planet is now orbiting the [Music] sun what is really cool with this formula is that if we add any object it will start orbiting we can add a smaller body around a planet represent the moon with a smaller size and smaller mass it will start orbiting the planet all this only with these two formulas from there we can really build an entire solar system with more planets and make them all orbit each other all right it's already the end of the video i hope you liked it if that's the case thank you very much for giving a thumbs up and subscribing to the channel thanks for watching keep coding and see you next time you
Info
Channel: Coderious
Views: 35,992
Rating: undefined out of 5
Keywords:
Id: kUXskc76ud8
Channel Id: undefined
Length: 6min 7sec (367 seconds)
Published: Fri Jun 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.