Planet Simulation In Python - Tutorial

Поделиться
HTML-код
  • Опубликовано: 26 июн 2024
  • Welcome back to another tutorial video! In this video I am going to be showing you how to make a planet simulation using Python! The goal of this tutorial is to simulate the orbits of different planets around the sun using real astronomical values! We're also going to apply the force of gravity on each of the planets so we get an accurate elliptical orbit! I hope you enjoy!
    💻 Thanks to BackTrace for sponsoring this video! Build better games with less bugs today for FREE by signing up for a developer account: hubs.la/Q012hTT00
    💻 ProgrammingExpert is the best platform to learn how to code and become a software engineer as fast as possible! Check it out here: programmingexpert.io/tim and use code "tim" for a discount!
    📄 Resources 📄
    Inspiration For This Video: fiftyexamples.readthedocs.io/...
    Code In This Video: github.com/techwithtim/Python...
    Fix Pip (Windows): • How to Install Pygame ...
    Fix Pip (Mac): www . • How to Install Pygame ...
    ⭐️ Timestamps ⭐️
    00:00 | Planet Simulation
    01:15 | Sponsor
    02:12 | Setup & Installation
    03:46 | Pygame Window Setup
    09:28 | Creating Planets
    18:50 | Initializing Planets (Using Real Values)
    27:33 | Moving Planets Explanation (Math & Physics)
    36:45 | Implementing Movement Physics
    51:58 | Drawing Orbits
    55:38 | Drawing Distance To Sun
    59:18 | Conclusion
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    👕 Merchandise: teespring.com/stores/tech-wit...
    📸 Instagram: / tech_with_tim
    📱 Twitter: / techwithtimm
    ⭐ Discord: / discord
    📝 LinkedIn: / tim-ruscica-82631b179
    🌎 Website: techwithtim.net
    📂 GitHub: github.com/techwithtim
    🔊 Podcast: anchor.fm/tech-with-tim
    🎬 My RUclips Gear: www.techwithtim.net/gear/
    💵 One-Time Donations: www.paypal.com/donate?hosted_...
    💰 Patreon: / techwithtim
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    ⭐️ Tags ⭐️
    -Tech With Tim
    -Planet Simulation
    -Planet Simluation In Python
    -How to Simulate Planets
    -Python Simulation Tutorial
    ⭐️ Hashtags ⭐️
    #TechWithTim #Python #PlanetSimulation

Комментарии • 438

  • @TechWithTim
    @TechWithTim  2 года назад +153

    Hey guys! Glad so many of you are enjoying this topic. Just wanted to note that I made a bunch of small physics/math errors in this video and I am by no means a physicist. Some people have politely pointed out the flaws in the simulation and that it is indeed a very rough approximation. Regardless, I think there is still a ton of value here and you can tweak/fix some parts to make it more accurate 👍

    • @randomdude7386
      @randomdude7386 2 года назад +18

      I dont think I could spot any major errors
      on either the maths or physics side in your explanations, but some times I as a physics major felt like you had no idea how to bring it accross as you seemed frightened of the possibility of saying something factually incorrect as the important part in this is, to bring the fundamental concept accross to those who came here not knowing how any of this worked.
      TLDR:
      Next time you do something like this, maybe try to be more relaxed and think less about the possibility of details being of in the grand scheme of things, as this has not stopped you from building this audience, as mistakes are human and it matters how we address them

    • @HypnosisBear
      @HypnosisBear 2 года назад +5

      The thumbnail is click bait.😑 I thought you are gonna make a 3D simulation.

    • @mr.whiteboard6357
      @mr.whiteboard6357 2 года назад +1

      @@HypnosisBear Yeah me too

    • @Hiyouri005
      @Hiyouri005 2 года назад +1

      HEY CAN YOU POST CODE TOO//

    • @rafigasparyan6584
      @rafigasparyan6584 2 года назад +1

      @@Hiyouri005 the code, along with all of the resources he used and credited, are in the description (press SHOW MORE, the button is under the channel name and subscribe line/section).

  • @lissankoirala
    @lissankoirala 2 года назад +254

    It's not the first time so pretty sure it's not just a coincidence. I was thinking about this project and here you go 3 days later posting the exact thing I want. You really seem to know what your audience needs, thanks Tim!

    • @harshmirdhwal
      @harshmirdhwal 2 года назад +6

      It's called manifestation bro

    • @heelercs
      @heelercs 2 года назад +4

      @@harshmirdhwal Lol, sure bro

    • @ryanchowdhary965
      @ryanchowdhary965 2 года назад +1

      Ai maybe??

    • @harshmirdhwal
      @harshmirdhwal 2 года назад

      @@ryanchowdhary965 force greater than ai

    • @speedybenzedrex6786
      @speedybenzedrex6786 2 года назад +1

      tim hacked u and used alll your data to create the perfect video for you

  • @matheotrouille9085
    @matheotrouille9085 2 года назад +28

    Physics modeling is so interesting, look forward to see more like this !

  • @renegadezed
    @renegadezed 2 года назад +139

    f = ma .. NOT f = m/a .. force equals mass multiplied by acceleration

    • @TechWithTim
      @TechWithTim  2 года назад +63

      Great catch! Big brain fart from me

    • @renegadezed
      @renegadezed 2 года назад +4

      @@TechWithTim no big deal! you still promote Kyte? that auto complete coding help thing?

    • @Mohd_Zaid_khan
      @Mohd_Zaid_khan Год назад +16

      Funny thing is f ≠ ma, rather it's always dp/dt where p is linear momentum. for mass rate = 0, you can say net force along a line = mass multiplied by acceleration along that line

    • @jackyisking
      @jackyisking 5 месяцев назад +1

      But is gravity a force at all?

    • @youber3200
      @youber3200 5 месяцев назад +1

      ​@jackyisking gravity is a field and gravitational force is a force :>

  • @hyphenbruh7376
    @hyphenbruh7376 2 года назад +7

    That was awesome! I love and appreciate how you explained the logic behind each line of code and the concepts behind the physics of the orbits :D

  • @kevinbacon8716
    @kevinbacon8716 2 года назад +3

    You’re about to hit a million subs. Congrats man. Your videos are great and as someone that’s new to python

  • @vishsinghania
    @vishsinghania 2 года назад +167

    amazing tutorial 👍
    just a correction at 45:00
    F = m times a (not m/a)

    • @joseph2073
      @joseph2073 2 года назад +3

      @Harsh 🤣

    • @hxdx6950
      @hxdx6950 2 года назад +1

      or F = Mg

    • @gillesphilippedeboissay109
      @gillesphilippedeboissay109 2 года назад +4

      @@hxdx6950 no it's true en Earth only sometimes

    • @Mickymauserius
      @Mickymauserius Год назад +1

      But why does "self.x_vel += total_fx / self.mass * self.TIMESTEP" work. Shouldnt it be "self.x_vel += self.mass / total_fx * self.TIMESTEP"?

    • @rohan9739
      @rohan9739 Год назад +2

      @@Mickymauserius The formula is F = m * a, so a = F / m ; therefore self.x_vel += total_fx / self.mass * self.TIMESTEP works

  • @emirhanu8655
    @emirhanu8655 2 года назад +4

    You are awesome, bro. Every time producing different ideas.. Keep going Tim

  • @danadasachan7208
    @danadasachan7208 Год назад

    Thank Tim. I really enjoyed this project. I remember studying these planetary equations when I was in my undergraduate mechanical engineering program back in 1985 and I remember coding the orbit of a planet in Turbo Pascal. Crazy.

  • @devbites77
    @devbites77 2 года назад +3

    Great! A fascinating simulation. I would love to see some more physics simulations using forces.

  • @amaarquadri
    @amaarquadri 2 года назад +6

    Nice tutorial! The x and y variables are shadowing each other at you suspected at the end. The reason that it doesn't break the program is because x and y ended up being the last values in the orbit array, which happens to be the same.

  • @googldeed
    @googldeed 2 года назад

    I can finally get into data simulation now that you made the perfect introduction for me.

  • @phase0400
    @phase0400 Год назад +4

    I would really enjoy seeing a 3D version of this.
    Nice video.

  • @NickHanson
    @NickHanson 4 месяца назад

    This was a great introduction to the pygame library with a helpful foray into authentic simulation. Thank you for putting this together!

  • @nadeeshani
    @nadeeshani Год назад

    You are amazing, Tim! Thank you so much for this project and for the way you explained it.

  • @universodacomputacao
    @universodacomputacao Год назад

    This is so awesome that made me understand more about Space Engine, the best Universe Simulator that I've ever seen. Thank you so much

  • @NishantCosmos
    @NishantCosmos 2 года назад +1

    there's a great guy, explains the science behind planets in simplest way, claims we're confused
    like NO PROBLEM.

  • @xdaniels13
    @xdaniels13 2 года назад +8

    Awesome project Tim. I love physics and astronomy this is the perfect project to increase my skills in python :)

    • @_xiliam_7708
      @_xiliam_7708 5 месяцев назад

      If u love physics how u didn't notice f= m/a? I don't even mean that his planets make rotation around the sun in 1 sec while timestep is only 1 day?

  • @Heisenberg_99101
    @Heisenberg_99101 2 года назад +36

    This is just pure level genius, great help for a python project. Loving it 🙌

  • @lupamo_Arnold
    @lupamo_Arnold 2 года назад

    Loved this project. Thanks Tim

  • @bobmorone461
    @bobmorone461 11 месяцев назад

    Five Star! Excellent. Thank you for providing detailed explanations as you are introducing the code. Simulations like this offer a good way to learn and provide a platform upon which to build more interesting simulations.

  • @DevilisshDan
    @DevilisshDan 2 года назад +68

    I think it's important to mention that what you're doing is just an approximate solution. When using the acceleration from the force and multiplying it with the timestep, you're taking a shortcut and calculating the average speed over a timestep. Since it's a many body problem, you can't get an "exact" solution.

    • @arkie87
      @arkie87 2 года назад +9

      yeah, it is also not energy conserving, as demonstrated when running without a vertical velocity component-- the planets just approached the sun and launched off to infinity.

    • @felicytatomaszewska2934
      @felicytatomaszewska2934 2 года назад +2

      We really look forward to your "exact solution"

    • @exar6765
      @exar6765 2 года назад +23

      @@felicytatomaszewska2934 He literally said : "You CAN'T get an exact solution". He's not saying it's a bad simulation, just an approximation

    • @mastershooter64
      @mastershooter64 2 года назад +4

      @@felicytatomaszewska2934 I think you can mathematically prove that it's impossible to get an analytic solution for a n-body problem

    • @aurelia8028
      @aurelia8028 2 года назад +3

      There's literally no other way to do it. I don't get what you mean by "shortcut". This method is forward euler integration, but yes there are more complex integration schemes like runge-kutta or verlet integration, but I don't think he knows anything about that

  • @danielkursitis9031
    @danielkursitis9031 2 года назад +1

    Really enjoy this type of content, keep it up

  • @reubenthomas1033
    @reubenthomas1033 2 года назад +1

    Awesome! Please do more of these!

  • @annajuliagiza5916
    @annajuliagiza5916 6 месяцев назад +1

    Thank you for the wonderful tutorial. You do an excellent job of showing and explaining each of the steps, what you're using in Python, and what each part of the program does. I just completed the CS50 Intro to Python course and was looking for a project to practice the skills taught. I have an avid interest in space so coming across this tutorial was excellent.

    • @_xiliam_7708
      @_xiliam_7708 5 месяцев назад

      He did mistakes, but the idea and basic explanation is very good for the project

  • @PayneMaximus
    @PayneMaximus 11 месяцев назад +17

    A more realistic simulation would be somewhat more complex than what you've done, since it would depend on the relative positions of each body and their velocities in a single instant, so you can use it as a starting point for the calculations.
    Still, good job! 😊

  • @flashypepo
    @flashypepo 2 месяца назад

    Thanks, Tim. It is a very nice tutorial and introduction to astronomy, physics and Python, despite some minor flaws. I've made it, add the name of planet/sun to it and re-factored it in classes PlanetSimulation and Planet (which goes beyond your tutorial). It gave me a nice step up to go further in astronomy and astrophysics (when I was as young as you, I studied astronomy, years ago). kind regards, Peter.

  • @wowgodz-5349
    @wowgodz-5349 2 года назад +1

    ngl i've been waiting for this

  • @domingezu4687
    @domingezu4687 2 года назад +1

    Very nice video TIM!

  • @Sk8erMorris
    @Sk8erMorris 2 года назад +1

    ok this is sick, thanks for this one !

  • @yudianimations
    @yudianimations Год назад

    great tutorial! thanks tim :D
    this also made me understand classes and stuff

  • @martin-xq7te
    @martin-xq7te 2 года назад

    Great tutorial Tim. Thankyou

  • @Greatbob-qu2ip
    @Greatbob-qu2ip 6 месяцев назад

    Coming from a physicist your explanation is by no means perfect but it's good enough for what you want to accomplish and that's more than enough keep up the good work

  • @toul100
    @toul100 2 года назад

    YOUR OPINION IS VALUED I LOVE THIS

  • @Stl71
    @Stl71 2 года назад +1

    No surprise you have so many subscribers. Lots of interesting stuff in that channel, although I am not a python guy.

  • @philosopherPsy
    @philosopherPsy 7 месяцев назад

    While watching your lecture, I immediately entered the code and tried it out on my computer.
    It is perfect and beautiful.
    You have been a great inspiration to me.
    I will also try Earth's satellite, the Moon, the asteroid belt and other large outer planets.
    It was a really fun weekend. Thank you!!!!

  • @DoctorSoulis
    @DoctorSoulis 2 года назад

    Ty for the hard work. I learned a lot from this tutorial! :D

  • @J_GB
    @J_GB 2 года назад

    this was awesome! very cool idea for a simulation

  • @quyennguyen6725
    @quyennguyen6725 2 года назад +2

    Honestly, I love videos like this. this is a 2d model, can you upgrade it to a 3d model. I look forward to it. Thank you for your contribution to the community.

  • @efschannel3043
    @efschannel3043 2 года назад +1

    Omg I was thinking about this for a long time

  • @Ansh-Adonis
    @Ansh-Adonis 4 месяца назад

    ill take this a step and a half further, thank you!

  • @jdefar3243
    @jdefar3243 2 года назад

    love you'r videos :D they are great I learn so much

  • @shubhaggarwall
    @shubhaggarwall 2 года назад

    well the thumbnail helped me revising the force formula thanksss

  • @satyadev2011
    @satyadev2011 6 месяцев назад

    Great tut man.

  • @risveyhasan8931
    @risveyhasan8931 2 года назад

    Great work man.

  • @Felix-wo7qz
    @Felix-wo7qz 2 года назад

    cool tutorial, has been fun to program it!

  • @marcrindermann9482
    @marcrindermann9482 2 года назад +8

    when you want to express 1 AU in metres, just increase the exponent by 3: 1 AU = 149.6e9 metres; rather than 149.6e6 km × 1000 m/km

  • @chrisdanny5180
    @chrisdanny5180 2 года назад

    This is a brilliant idea

  • @gidartsproduction5439
    @gidartsproduction5439 2 года назад +1

    Wow, high quality information...

  • @MilkenGamer42
    @MilkenGamer42 2 года назад

    This was a really fun tutorial to follow! I just started to learn Python and I found this video very informative. My problem with my code is that even though I was following this video, for some reason no matter what resolution I set the window, it would only do full screen and now windowed, so I'd have to hit window then X to close the program.
    You did an excellent job explaining how everything worked 😊

    • @itz_ronatime7527
      @itz_ronatime7527 2 года назад

      did u find a fix? my python doesnt run in windowed mode and its annoying..

  • @user-yt6of5mt4c
    @user-yt6of5mt4c Год назад +1

    amazing, you're really a genius

  • @Burdiga_memes
    @Burdiga_memes 2 года назад

    I really enjoyed the tutorial I am 12 years coder and I understood very thing eaisly thanks to you

  • @ConsultingjoeOnline
    @ConsultingjoeOnline 2 года назад

    Amazing video. Freaking sweet!

  • @muhammadmz8791
    @muhammadmz8791 Год назад +1

    thank you tim 🙏

  • @jessysavard2133
    @jessysavard2133 2 года назад

    your vid help me a lot no matter if some shit false, that give us something to fix and keep going lil watch and like every vid !

  • @malibaturhan
    @malibaturhan 11 месяцев назад

    it was really satisfying even by copying you step by step, thank you

  • @timbeard8457
    @timbeard8457 2 года назад +3

    Super project! Have just bookmarked it for reference.
    Excellent that you included the interplanetary gravity too (from a coding and physics perspective). I agree that it was probably best not to use the general relativity description of gravity. The maths would have been hell! Newtonian physics was also good enough to get to the moon (I think)
    Questions:
    - Is there a way to only refresh / move just the changed items and not redraw everything? If there were lots of static objects, wouldn't that have a big performance impact? It would also mean that the complete history of the orbits wouldn't need redrawing each time, just the last point.
    - How would you approach having multiple sections in the app? Example: the canvas WIN on the left and some kind of control panel on the right for user input, plus a title / description area above both.
    - Would it be possible with pygame to dynamically change the window size, e.g. by the user grabbing the corner of the window?
    Please point me to any other videos or projects on GitHub you've done that address any of the points above.

  • @tusharpatel2003
    @tusharpatel2003 2 года назад +1

    It's amazing 😀🤩 bro
    Thank you for this ❤️

  • @vfx7t
    @vfx7t 2 года назад +1

    Good job ! And thanks from Algeria !

  • @leonellg3167
    @leonellg3167 3 месяца назад

    The way the logic of the orbits is implemented means the orbit (and updated_points) lists will keep growing without end if the simulation is kept running.
    A slight modification to add a max number of points, and then an if statement to remove older points once the list has reached this max number would mean the simulation can run forever without issues.

  • @RAMESHNEUPANE-jg2uu
    @RAMESHNEUPANE-jg2uu 2 месяца назад

    Great. Thank you.

  • @GordonFreeman69
    @GordonFreeman69 Год назад

    Thanks for the tutorial.

  • @AstroBene
    @AstroBene 2 года назад +1

    *sees the title, screams:* HELL YESS!
    Thanks!

  • @josephrios4437
    @josephrios4437 2 года назад

    awesome tutorial 👍

  • @antoine2571
    @antoine2571 2 года назад

    that's amazing, thank you

  • @lissankoirala
    @lissankoirala 2 года назад +6

    45:00 - you made an error here, F = m / a [supposed to be F = m*a]

  • @norb3695
    @norb3695 2 года назад

    Hell yeah another Python tutorial 😃

  • @GregoryCarnegie
    @GregoryCarnegie 2 года назад +3

    I played around with this and realised a slight simplification you can make.
    Since all objects feel the same acceleration regardless of their mass, you can write this script in terms of acceleration rather than force.
    e.g. velocity self.x_vel += total_ax * self.TIME_STEP

    • @aurelia8028
      @aurelia8028 2 года назад

      Yup using force is a rather "naive" way of doing it. division is also expensive for computers, so by calculating the acceleration directly, you can save the computer from doing quite a lot of unecessary work.

  • @rverm1000
    @rverm1000 2 года назад

    wow that was good and hard. thanks!

  • @feelsadgeman
    @feelsadgeman 2 года назад +1

    I had the same idea, i was going to make this on my final project

  • @ykslooper
    @ykslooper 2 года назад

    i love you so much tim your videos are amazing. I hope you clsoe time you will 1 millon subs

  • @unknownman5296
    @unknownman5296 2 года назад

    Thanks for the math lesson 👍

  • @senaysew8231
    @senaysew8231 2 года назад

    It is really interesting!

  • @EarlWallaceNYC
    @EarlWallaceNYC 2 года назад

    Thanks for the fun video. I was impressed that you included the planet-planet interactions.
    I wonder how long you can run this before the small errors kill it?

    • @Anonymous-8080
      @Anonymous-8080 2 года назад

      It will collapse in 49 years 56 days

  • @techzenik
    @techzenik 2 года назад

    Awesome!!!

  • @jdefar3243
    @jdefar3243 2 года назад +1

    fortunately i knew the math before hand otherwise this would be a lot harder ;D greate video

  • @si8915
    @si8915 2 года назад +2

    some day, tim will do "Filosophically Functional Life Forms In Python - Tutorial"

  • @lp2045
    @lp2045 2 года назад

    Now let's see uranus' simulation lol
    Great vid btw

  • @cs_armenglish
    @cs_armenglish 5 месяцев назад

    Fantastic

  • @saranyaghosh2003
    @saranyaghosh2003 2 года назад

    The title just drew me in!

  • @alexandermardis8179
    @alexandermardis8179 2 года назад

    Awesome ty!

  • @jakovbilic4556
    @jakovbilic4556 2 года назад

    Superb!

  • @bvg129
    @bvg129 2 года назад +5

    You can use strings for common colors in pygame now. Since 2.0

  • @Totema.
    @Totema. 2 года назад

    This is so nice 😮

  • @absar_ahmad
    @absar_ahmad 2 года назад +1

    This man have magical powers to know what we are thinking, believe me.

  • @spaceflight
    @spaceflight 2 года назад +1

    very cool

  • @frankhaug1517
    @frankhaug1517 Год назад

    15 minutes in. great video so far, i just hit all the buttons. however, you can do AU = 149.6e9 instead, same effect. edit: and since you mentioned consistency: 1*10**X = 1eX

  • @imen9464
    @imen9464 2 года назад

    Thanks a lot

  • @StarboyWithTech
    @StarboyWithTech 2 года назад

    Really unexpected project like every Time 😉.

  • @fun-damentals6354
    @fun-damentals6354 6 месяцев назад +3

    this was a really informational video. to all new programmers, let me teach a way to partially escape tutorial hell. if you want to learn a language, but dont wanna worry about the logistics of a project, follow along with a tutorial in another language. i am learning c++, and it is about as far from python as it gets, and it helped me learn the language while still following along with the video

  • @tiagosolano9728
    @tiagosolano9728 7 месяцев назад

    Maybe someone already pointed this out, but the distance from the center of mass of the earth to astronauts in orbit IS NOT the reason why they do not experience the force of gravity.
    The reason is that they are on orbit around the earth, which is equivalent to being on free fall.
    Earth's gravitational field goes way beyond low earth orbit, as you can reason from the fact that the moon is in orbit around the earth at a far greater distance.

  •  2 года назад

    Nice!

  • @1803harshi0033
    @1803harshi0033 Год назад

    Hey Tim, I enjoyed the video, one query .. instead of color is it possible to use the images of the planets..

  • @fredericoamigo
    @fredericoamigo 2 года назад

    awesome!

  • @taradavies3073
    @taradavies3073 2 года назад

    Tim, great video but the physics was a little off. You can’t take that force into components to find the velocity. The gravitational force acts as a centripetal force such that GmM/r^2 = v^2/r * m (F = ma)
    Also, the distance should be from radius to the other radius so we should have added this.radius + other.radius

  • @thatego9417
    @thatego9417 2 года назад

    hey can you make a tutorial on using pyglet ,pygam ,numpy or sympy , to create some physics model ?like cars crashing , or a pulley system with weights and stuff.

  • @novidtoshow
    @novidtoshow Месяц назад

    Just a little nit: the gravitational force weakens as the inverse square of the distance. It doesn't weaken "exponentially". The word, "exponentially", has a very specific mathematical meaning. You're gonna want to have that meaning available to you one day when you really need it.

  • @artyom9149
    @artyom9149 2 года назад

    Amazing

  • @petruthegamer9777
    @petruthegamer9777 2 года назад

    Thanks.

  • @sandeepnag5180
    @sandeepnag5180 Год назад

    more physics with python please 👏