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
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 👍
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
The thumbnail is click bait.😑 I thought you are gonna make a 3D simulation.
@@HypnosisBear Yeah me too
HEY CAN YOU POST CODE TOO//
@@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).
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!
It's called manifestation bro
@@harshmirdhwal Lol, sure bro
Ai maybe??
@@ryanchowdhary965 force greater than ai
tim hacked u and used alll your data to create the perfect video for you
Physics modeling is so interesting, look forward to see more like this !
f = ma .. NOT f = m/a .. force equals mass multiplied by acceleration
Great catch! Big brain fart from me
@@TechWithTim no big deal! you still promote Kyte? that auto complete coding help thing?
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
But is gravity a force at all?
@jackyisking gravity is a field and gravitational force is a force :>
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
You’re about to hit a million subs. Congrats man. Your videos are great and as someone that’s new to python
amazing tutorial 👍
just a correction at 45:00
F = m times a (not m/a)
@Harsh 🤣
or F = Mg
@@hxdx6950 no it's true en Earth only sometimes
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"?
@@Mickymauserius The formula is F = m * a, so a = F / m ; therefore self.x_vel += total_fx / self.mass * self.TIMESTEP works
You are awesome, bro. Every time producing different ideas.. Keep going Tim
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.
Great! A fascinating simulation. I would love to see some more physics simulations using forces.
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.
I can finally get into data simulation now that you made the perfect introduction for me.
I would really enjoy seeing a 3D version of this.
Nice video.
This was a great introduction to the pygame library with a helpful foray into authentic simulation. Thank you for putting this together!
You are amazing, Tim! Thank you so much for this project and for the way you explained it.
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
there's a great guy, explains the science behind planets in simplest way, claims we're confused
like NO PROBLEM.
Awesome project Tim. I love physics and astronomy this is the perfect project to increase my skills in python :)
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?
This is just pure level genius, great help for a python project. Loving it 🙌
Loved this project. Thanks Tim
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.
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.
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.
We really look forward to your "exact solution"
@@felicytatomaszewska2934 He literally said : "You CAN'T get an exact solution". He's not saying it's a bad simulation, just an approximation
@@felicytatomaszewska2934 I think you can mathematically prove that it's impossible to get an analytic solution for a n-body problem
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
Really enjoy this type of content, keep it up
Awesome! Please do more of these!
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.
He did mistakes, but the idea and basic explanation is very good for the project
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! 😊
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.
ngl i've been waiting for this
Very nice video TIM!
ok this is sick, thanks for this one !
great tutorial! thanks tim :D
this also made me understand classes and stuff
Great tutorial Tim. Thankyou
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
YOUR OPINION IS VALUED I LOVE THIS
No surprise you have so many subscribers. Lots of interesting stuff in that channel, although I am not a python guy.
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!!!!
Ty for the hard work. I learned a lot from this tutorial! :D
this was awesome! very cool idea for a simulation
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.
Omg I was thinking about this for a long time
ill take this a step and a half further, thank you!
love you'r videos :D they are great I learn so much
well the thumbnail helped me revising the force formula thanksss
Great tut man.
Great work man.
cool tutorial, has been fun to program it!
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
This is a brilliant idea
Wow, high quality information...
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 😊
did u find a fix? my python doesnt run in windowed mode and its annoying..
amazing, you're really a genius
I really enjoyed the tutorial I am 12 years coder and I understood very thing eaisly thanks to you
Amazing video. Freaking sweet!
thank you tim 🙏
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 !
it was really satisfying even by copying you step by step, thank you
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.
It's amazing 😀🤩 bro
Thank you for this ❤️
Good job ! And thanks from Algeria !
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.
Great. Thank you.
Thanks for the tutorial.
*sees the title, screams:* HELL YESS!
Thanks!
awesome tutorial 👍
that's amazing, thank you
45:00 - you made an error here, F = m / a [supposed to be F = m*a]
Hell yeah another Python tutorial 😃
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
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.
wow that was good and hard. thanks!
I had the same idea, i was going to make this on my final project
i love you so much tim your videos are amazing. I hope you clsoe time you will 1 millon subs
Thanks for the math lesson 👍
It is really interesting!
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?
It will collapse in 49 years 56 days
Awesome!!!
fortunately i knew the math before hand otherwise this would be a lot harder ;D greate video
some day, tim will do "Filosophically Functional Life Forms In Python - Tutorial"
Now let's see uranus' simulation lol
Great vid btw
Fantastic
The title just drew me in!
Awesome ty!
Superb!
You can use strings for common colors in pygame now. Since 2.0
This is so nice 😮
This man have magical powers to know what we are thinking, believe me.
very cool
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
Thanks a lot
Really unexpected project like every Time 😉.
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
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.
Nice!
Hey Tim, I enjoyed the video, one query .. instead of color is it possible to use the images of the planets..
awesome!
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
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.
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.
Amazing
Thanks.
more physics with python please 👏