Very nice work. Just spotted this though: the length() function will use a sqrt() which is time consuming in itself. You have accidentally if LastPos.... inside the for loop. Move this and you gain a lot of extra cpu time. Even a much longer tick list.
@grandmastertaco302217 күн бұрын
Oh dang thanks for the assist
@thomradeАй бұрын
really interesting stuff! I've been toying with similar stuff for a long time just for fun. it's really interesting to see how you're approaching it!
@user-om3lm7er3jАй бұрын
Looking forward to the delicious links. :P
@YourMom-rg5jkАй бұрын
im sure python will handle 100,000 particles just fine 😅
@Deckardb253642 ай бұрын
I just want to say before even starting, I literally have spent an embarrassing amount of time playing Dwarf Fortress, Rogue, Nethack, CDDA, NeoScav, Rimworld, Warsim:KoA So, so many of these games. I genuinely can't believe that me trying to learn how to do this myself completely raw no experience leads my first video to being this, and you open with that. I can't explain the coincidence here, I just rambled with my fingers.
@Vermino5 ай бұрын
I'm not gonna lie. I would love to see the code of this.
@grandmastertaco30225 ай бұрын
No you would not. I wasn't as concerned about readability back then.
@Vermino5 ай бұрын
@@grandmastertaco3022 Ehh your taco-project was a good jumping off point with map generation. Especially for people trying to learn how to build their own engine versus using libraries or out of box solutions. Or at least that is for my case.
@ClassyBaxy6 ай бұрын
Bro the fact my reason to do this too was because of dwarf fortress as well 💀
@STaSHZILLA4207 ай бұрын
Very cool!
@STaSHZILLA4207 ай бұрын
My guy, you are a neat dude. You know how to do everything I want to learn. This is looking like some Wargames type of effect.
@STaSHZILLA4207 ай бұрын
This is great! I really enjoyed this video and your procedural generation video. You have a very direct way of speaking and I think that is crucial in coding tutorials. I really hope you do more videos. I'm still learning python and you help out alot. I would like to see a bit more on noise and how to manipulate the maps to achieve a desired look (mostly lakes similar to the game Intergalactic Fishing). I also want to find a way to make that star map from Starbound. Its basically a procedural map of stars that you could hover over a star and see the random planets and moons inside. Anyways, keep it up! you have something here.
@thetruth456789 ай бұрын
You can just make your own smooth noise function and use that as a base for an fbm loop. You could have also explored various cellular automata solutions, or the newfangled wave function collapse. Still, it works.
@grandmastertaco30229 ай бұрын
Yep all of your suggestions are other ways to make it work, however the point of this was to spend 30 minutes with zero experience and not to set an educational goalpost. Anyone can do anything with just a little bit of time and creativity, but it is true there are more elegant and efficient solutions.
@rubenneurink24689 ай бұрын
I like it, keep it up!
@breadman773311 ай бұрын
Your channel is dope
@isaachiew4906 Жыл бұрын
How do you expand the radius of orbit in the window ?
@UnderArea51 Жыл бұрын
Beautifully done! I have been working out a similar Python problem. I will share mine - once I have completed a solution.
@alboris8203 Жыл бұрын
Excellent vid
@s.l.9602 жыл бұрын
This is very nice but the result is not realistic at all. Particles behave very different than they should. I think n-body is usefull only when one particle is is much larger than all the others.
@grandmastertaco30222 жыл бұрын
Right, the physical parameters such as G and the density and the inelasticity of the particles are not strictly accurate and have been simplified. The point was to illustrate the activity of an inverse-square system, which is what the n-body problem actually is. You can make it more realistic by tweaking it, however the attractive property and the accelerative inertia is accurate to newtons laws. For example, particles just phase through each other rather than aggregating or reflecting elastically. This is fine, because that has nothing to do with the n-body problem. For another thing, this is a Euler approximation, so the integration over time obviously cannot be perfect. Whether the masses of particles identical or disparate is irrelevant. The n-body problem is the same in either case. Perhaps the system fails to conform to your expectations because gravitational systems are counter-intuitive, which is why we call it the n-body PROBLEM. Hope this was informative.
@s.l.9602 жыл бұрын
@@grandmastertaco3022 Yes it was. Thank you. I think I should study more this n-body system to better understand it how it works.
@grandmastertaco30222 жыл бұрын
@@s.l.960 Well the important thing is that things pull harder the closer they are. When 1 body is much bigger than any other the others your really looking at a 2 body problem. For example, in our solar system none of the planets really affect each other in a meaningful way because the sun is so massive, even Jupiter is mostly irrelevant. When you add other bodies that are closer in mass that aren't dominated by something huge, the system becomes chaotic and unpredictable. It's basically the jointed pendulum, operating by apparently simple rules but very complex when you actually do something with it. So as long as you understand those two points, proximity and chaos, you know every you really need to know about the N-body problem, everything else is really just trivia if you ask me.
@Mix1822 жыл бұрын
wow, helped me a lot, I knew how to get the velocity, but I couldn't figure out how to get the direction :) thanks
@grandmastertaco30222 жыл бұрын
Glad it helped!
@alexanderthorbrugge64892 жыл бұрын
If you import sqrt by writing from math import sqrt, it is significantly faster. Don’t ask me why but it is.
@andriko72352 жыл бұрын
im still very much a beginner but cant you just put (x)**0.5 every time you sqrt, since its the same characters etc?
@alexanderthorbrugge64892 жыл бұрын
@@andriko7235 yes it does compute to the same thing but math.sqrt is still faster
@grandmastertaco30222 жыл бұрын
yeah, I've done a number of experiments to try and speed up the program, including a quad tree. I've found that the most significant delay is actually the tkinter canvas live-rendering the little dots. It runs about 25x faster if you just dump a numerical output, and then plug in the graphics later.
@judelog13812 жыл бұрын
The reason why this is true is because the python math library is written in the C programming language which is several times faster than python.
@alexanderthorbrugge64892 жыл бұрын
@@judelog1381 yeah I know
@ivolol2 жыл бұрын
You should record the video in 720p/1080p
@7sdfsdfs82 жыл бұрын
Thank you very much for the detailed explanation. I have compared your code with other options. yours is much shorter. the fewer lines the better. I tried to write down the same code, exactly. can you send me your code via mail or share github? Because my code doesn't run. I'll attach my code if anything from tkinter import * import numpy as np import random import math fig = Tk() fig.wm_title('N body problem') canvas = Canvas(fig, width =700, height=700, bg='black') canvas.grid(row=0, column=0) G= 1 bodies= [] class body: def __init__(self, Mass, Px, Py, V): self.Mass = Mass self.Px = Px self.Py =Py self.P = np.array([self.Px, self.Py], dtype = float) self.V = np.array(V, dtype = float) self.Logx = [] self.Logy = [] self.R = (self.Mass / 3.14)**(1/3) def Fg(self, other): if self == other: self.dV = np.array([0,0], dtype= float) else: self.dist =math.sqrt((self.P[0] - other.P[0])**2 + (self.P[1] - other.P[1])**2) self.dV = np.array([0,0], dtype= float) if self.dist == 0: self.dV = 0 else: self.Fg = [-G * self.Mass * other.Mass / self.dist**2] * (self.P - other.P) / self.dist self.dv = self.Fg / self.Mass if self.dist < self.R + other.R +5: self.dV *= -0.5 self.v +=self.dv if self.P[0] < 10 + self.R: self.V[0] *= -0.9 self.P[0] +=1 if self.P[0] > 698 - self.R: self.V[0] *= -0.9 self.P[0] -=1 if self.P[1] < 10 + self.R: self.V[1] *= -0.9 self.P[1] +=1 if self.P[1] > 698 - self.R: self.V[1] *= -0.9 self.P[1] -=1 BODIES = 25 for i in range (0, BODIES): BODIES.append(Body(random.randrange(10, 500), random.randrange(50, 650), random.randrange(50, 650), [(random.randrange(-10,10)/20), random.randrange(-10,10)/20])) T = 0 Tx = True while Tx ==True: T+=1 canvas.delete ('all') for Body1 in Bodies: for Body2 in Bodies: Body1.Fg[Body2] Body1.P +=Body1.V if Body1.P[1] < 5: Body1.V[1] *= -0.5 if Body1.P[1] > 695: Body1.V[1] *=-0.5 if Body1.P[0] < 5: Body1.V[0] *= -0.9 if Body1.P[0] > 695: Body1.V[0] *=-0.9 canvas.create_oval(Body1.P[0] -Body1.R, Body1.P[1] - Body1.R, Body1.P[0] + Body1.R, Body1.P[1] + Body1.R, fill = 'yellow') canvas.update() if T ==1000000: Tx = False mainloop()
@grandmastertaco30222 жыл бұрын
Okay so just looking over it briefly I think your problem is that BODIES is an integer when I should be a list: BODIES = 25 BODIES.append This will not work. Instead try BODIES = 25 Body_list =[] Now replace BODIES.append with Body_list.append. Also when you run your main loop, it is better to have a specific condition rather than while x = true. Try wrapping your mainloop in a function and writing while x <= y: Main_loop() I'll look over this in more detail later.
@gzaymanyt52 жыл бұрын
Body() takes no arguments
@Theeoldmann2 жыл бұрын
This awesome and very powerful, I appreciate it all.
@derrickakwasiamankwah22712 жыл бұрын
hello! how do you create another circle around the earth orbital? I try scrolling and nothing is working
@mythicalwhalers3 жыл бұрын
This is pretty excellent
@jarredeagley17483 жыл бұрын
Did you ever end up doing the 4D sim? Sounds interesting
@grandmastertaco30223 жыл бұрын
I did yes, the issue is that visualization of a 4D space requires you to keep track of multiple apparently contradictory but equally true, codependent reference frames. For example, if you have axes' XYZW (I hate that W is the standard notation for a 4th axis) you have to visualize XYZ, XYW, XZW, and YZW spaces, none of which make sense on their own. It's even more convoluted if you want to break it down further into 2D frames for display. If I made the video, it would just be to explain the headache involved, and we're still in purely euclidean space. In short it sounds cooler than it is.
@vitor-a123 жыл бұрын
Awesome!
@busterdafydd30963 жыл бұрын
I don't feel like this is properly addressing the N-body problem, surely you would find the center of gravity/mass of the field of particles then for each "particle" you would find it's acceleration towards that singular point. Your process is iterating through each particle individually which is kind of wrong
@grandmastertaco30223 жыл бұрын
This is a naive, newtonian euler approximation. You're thinking of the legrangian, or perhaps a particle mesh method or the barnes hut algorithm. There are multiple ways to preform N-Body calculations, each of which has upsides and down. This method gets good accuracy for a given delta-T, but requires order N^2 calculations, so it's not very computationally efficient. You can find more info here in the section "Direct Gravitational N-Body Simulations" en.m.wikipedia.org/wiki/N-body_simulation Edit: I just realized you're referring to a barycentric model, which is actually less accurate at calculating small scale interactions between individual particles. It has to be less accurate because it is a less direct approximation of range dependant forces. Very, very, very few N-Body simulations are entirely barycentric. I cannot think of any. Hope that clears this up.
@iJamesGuo3 жыл бұрын
good video! but why not 720p or above?
@grandmastertaco30223 жыл бұрын
Limited by the constraints of the tkinter canvas, which pixilates curves
@0xzwayne7722 жыл бұрын
@@grandmastertaco3022 pretty sure he meant the video quality
@muhammadfaridbinabdulwahab98473 жыл бұрын
good vid mate, but can i ask something. how can we make the result reproducible?
@grandmastertaco30223 жыл бұрын
Yes, you have to preset the position, velocity, and mass of given bodies. This simulation is totally deterministic, so identical starting parameters give identical results. This particular program uses random iteration to create the initial parameters, otherwise you'd get the same thing over and over again. To reproduce a given result just have the program print the start position, velocity, and mass of all interacting bodies. I hope that answers your question. I guess I can put up another video explaining that, but motivation is hard to come by lately.
@muhammadfaridbinabdulwahab98473 жыл бұрын
@@grandmastertaco3022 just now I used random.seed and u give me the result that I want. thank you sir again for this vid its help a lot with my final year project. since my fyp is to develop an ANN for N-body simulation
@feetus65483 жыл бұрын
w...what is that abomination? i dont get the reference
@grandmastertaco30223 жыл бұрын
It's from a very old, very bad movie. 10 points to whoever manages to recognize it.
@feetus65483 жыл бұрын
why must you torment me like this
@feetus65483 жыл бұрын
nice video by the way
@grandmastertaco30223 жыл бұрын
Much appreciated
@zahed11813 жыл бұрын
how to visualize using this simulation code?
@grandmastertaco30223 жыл бұрын
I'm sorry, can you rephrase the question?
@zahed11813 жыл бұрын
@@grandmastertaco3022 how can i visualize through python?
@grandmastertaco30223 жыл бұрын
@@zahed1181 This is in Python, using Tkinter, a GUI framework that comes prepackaged with Python.
@zahed11813 жыл бұрын
@@grandmastertaco3022 i need visualizing of gravitational n body particles. how can i do that?
@zahed11813 жыл бұрын
@@grandmastertaco3022 please provide me some links of visualizing n body gravity
@deepkantawala3 жыл бұрын
hey man can you show this with 3d plotting maybe opencl or something thanks :D
@grandmastertaco30223 жыл бұрын
It can be done with MatPlotLib, but not animated easily. Other option is to use some math and perform a 2D projection of a 3d space, which could be plotted and/or animated. I could make a video on either option. Python has openGL support, but idk how to use it.
@seudonym24673 жыл бұрын
@@grandmastertaco3022 u can use visual python
@grandmastertaco30223 жыл бұрын
@@seudonym2467 I have looked into visual python, however my system is Linux and VPython is most easily installed on windows. I decided it wasn't worth the effort then, and now I do not have wifi. Someday, maybe.
@muhammadfaridbinabdulwahab98473 жыл бұрын
this is technically my whole fyp lol
@vin_71783 жыл бұрын
Very nice mate, liked how you didnt type the code as you explained but rather just explained it. Very informative. Look forward 2 more stuff
@grandmastertaco30223 жыл бұрын
Hey I appreciate it. Most coding tutorials are garbage. I have a stripped down RTS engine coming up soon.