Making a Better Particle Simulation in C++ (Part 2)

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ม.ค. 2025

ความคิดเห็น • 73

  • @josealvim1556
    @josealvim1556 17 วันที่ผ่านมา +46

    One thing I love about circular particle simulations like those is the topological defects that arise when they all settle down, you can clearly see them around 24:17 just before the transition; just like microcrystaline grains, you can see the domain walls separating the big cells where the spheres are tightly packed. Very cool.

    • @keyframe41
      @keyframe41  17 วันที่ผ่านมา +3

      Nice observation, but I think it might just be the video compression though. A defect I noticed is how the very bottom particles with a lot of pressure are inexplicably shifting to the right slowly, which doesn't happen with less particles/pressure, which is probably due to the collision detection

    • @Tkk-i6m
      @Tkk-i6m 17 วันที่ผ่านมา +1

      also if you vibrate them the chunks become bigger, like heat treatment of metals

    • @aqua-bery
      @aqua-bery 17 วันที่ผ่านมา +3

      ​@@keyframe41 defects are supposed to form, that's just how circles pack together. It's very hard for them to pack perfectly after being continually launched like water lol

  • @jpgallegoar
    @jpgallegoar 20 วันที่ผ่านมา +65

    The algorithm has delivered yet again

  • @Tkk-i6m
    @Tkk-i6m 17 วันที่ผ่านมา +15

    your ability to get so far into a project without quitting is insane

    • @mattmmilli8287
      @mattmmilli8287 13 วันที่ผ่านมา

      I always do the first 80% so fast and then.. yea. Truly inspiring stuff

  • @ratchet1freak
    @ratchet1freak 17 วันที่ผ่านมา +13

    instead of storing a list of particle indices for each grid you can sort the particles by grid index with counting sort. That way all particles in each gridcel are contiguous and you are not thrashing the cache looping over all particles in each cell.

  • @thesquee1838
    @thesquee1838 17 วันที่ผ่านมา +17

    Cool video!
    One thing to clarify is that std::move just casts to an r-value, which allows for move semantics if they are implemented for a type. You mention perfect forwarding but for that you would use std::forward() with a templated function of that takes in the r-value type T&&. Perfect forwarding has more to do with how types are converted when passed to a function (look up reference collapsing), and allows for the type that is passed in to be conserved and not changed.
    Also some additional things to think about for speedups is focusing on making the size of all structs/classes as small as possible; if you can compress the size of the particle for instance you will likely see even more speedups due to better cache coherence. For me this had one of the largest effects on my particle simulator.

    • @keyframe41
      @keyframe41  17 วันที่ผ่านมา +2

      Added this to the video description, very informative

    • @thesquee1838
      @thesquee1838 17 วันที่ผ่านมา +2

      ​@@keyframe41 I made a Barnes-Hut zero gravity particle simulator using SFML like 2 years ago and always get excited about related videos haha. Nice work with this one!

    • @keyframe41
      @keyframe41  16 วันที่ผ่านมา +1

      @@thesquee1838 No way, that's exactly what I want to try make for part 4. Thanks for your advice!

    • @thesquee1838
      @thesquee1838 16 วันที่ผ่านมา

      ​@@keyframe41 Awesome! I'm curious to see how yours goes. For mine I was re-inserting particles into the quadtree every frame and it ultimately was less efficient than just a generic grid for spatial partitioning; but it looked way cooler as I used SFML to visualize the sections dividing into more subsections. If you figure out a good way to keep the structure of the quadtree and just "shake" off dead branches and reinsert particles that move into another cell you may get better performance. People also have algorithms for doing lots of the quadtree related calculations on the GPU, or using SIMD intrinsics for things like collisions which help performance.

  • @nezznh
    @nezznh 12 วันที่ผ่านมา +1

    Awesome video, the algorithm has blessed me with a W. As for the multithreading performance issue, I may have a solution you can try when you're feeling up to it : Your problem right now is that you have a fixed thread assignment structure, which effectively means that in geometrically diverse simulations (that being the beginning simulation you showed), it's still effectively up to one thread to simulate MOST of the particles. Add that to the extra overhead required to assign, mutex and synchronize the threads themselves, a greater performance overhead is expected.
    What I advice you try doing (im an amateur myself, so this may not work, but it sounds right) is looking over the cells in the grid themselves to determine how many particles are in them. Then, u can use define a constant that determines how many particles each thread can handle at a time (this constant will take some tweaking to figure out, dw if you dont get it right away), and then assigning as many cells as you can to each thread to fill up that count. This means that one thread will get a bunch of empty cells which will take him the same amount of time to calculate out as one thread that handles one cell that has TONS of particles. Essentially, you are making sure that a bunch of threads handle a bunch of particles, while the other threads handle the almost empty parts of the simulation. This means you are actually parallelizing things correctly, and it should mean that they will take roughly the same amount of time to finish all the calculations. Hopefully, this should work!
    I'm really impressed with the project so far, it's motivated myself to code in C++ again. Keep it up man!

  • @rickard1200
    @rickard1200 7 วันที่ผ่านมา +1

    Very cool stuff man!

  • @zhabiboss
    @zhabiboss 10 วันที่ผ่านมา +1

    7:42 *vector* OHHH YEAH

  • @splits8999
    @splits8999 17 วันที่ผ่านมา +3

    oh wow, wow this looks like great quality and very inspiring, im blessed to have found this!

  • @SobTim-eu3xu
    @SobTim-eu3xu 14 วันที่ผ่านมา +1

    Damm, man, your vid is fire
    Love that, I need to subscribe
    Will love to watch more parts of it, and wish you good at your exams
    You deserve more views and subs

  • @nuke_clear
    @nuke_clear 18 วันที่ผ่านมา +5

    I was recommended your first video just now and I really enjoyed watching both the parts. I would love to finish my particle physics code some day but I always get too busy with my main projects. Looking forward to more such content.
    PS : It's nice to find somebody who watches the exact same youtubers I do lol

  • @davidbrettell5687
    @davidbrettell5687 3 วันที่ผ่านมา +1

    Very impressive. I could take a guess at your final image location :-)

  • @mt2yo
    @mt2yo 13 วันที่ผ่านมา +1

    Excellent work! Keep them coming!

  • @hmmmidkkk
    @hmmmidkkk 17 วันที่ผ่านมา +2

    Danm man you're underrated af , I'd love to see more of your work ❤

  • @EmergentLifeArchive
    @EmergentLifeArchive 16 วันที่ผ่านมา +3

    Amazing work! Im absolutly implementing all your optimization techniques!! You should look into SOA (Structure of arrays) vs AOS (array of structures). Its a small optimization boost due to its cashe performance increase, which I found quite useful when making my particle systems!

  • @sohailu8058
    @sohailu8058 20 วันที่ผ่านมา +9

    Great video man! Please dont make the music too loud.

  • @hurmpel
    @hurmpel 3 วันที่ผ่านมา +1

    you could make a circle with just one triangle and drawing the circle inside the triangle using shaders

  • @trombecher
    @trombecher 16 วันที่ผ่านมา +1

    Cool video! Love to see more

  • @Adamplaysbadminton
    @Adamplaysbadminton 18 วันที่ผ่านมา +4

    Great vid! You should make some tutorials for people optimistic about coding 😁

  • @paulkanja
    @paulkanja 16 วันที่ผ่านมา +1

    you can probably save like a millisecond if you make the grid cells overlap by the diameter of the particles,, that way, you only need to check one cell

  • @Thegreatmattimo
    @Thegreatmattimo 13 วันที่ผ่านมา +1

    Actual beast ❤❤

  • @RayznGames
    @RayznGames 20 วันที่ผ่านมา +2

    Wonderfull! Amazing work!

  • @RichardEricCollins
    @RichardEricCollins 17 วันที่ผ่านมา +3

    Nice work. Would love Jason from c++ weakly to have a look at your threading code. He could well make it go a lot faster. He's like a c++ whisperer. Also watch out for thread::yeald. I've been stung by it in the past. Just sleeping the thread for 10ms can be a lot faster. 🤷‍♂️

  • @Flourish38
    @Flourish38 16 วันที่ผ่านมา +1

    I wonder if a hexagonal (or, really, offset square) grid might be more efficient, since then each cell would only have 6 neighboring cells instead of 8.

  • @alizohaibibn
    @alizohaibibn 20 วันที่ผ่านมา +1

    This is fire bro!

  • @Reanore
    @Reanore 20 วันที่ผ่านมา +1

    awesome video brody

  • @DYELB1
    @DYELB1 13 วันที่ผ่านมา +2

    12:07 “Calamitas even.” wait, say that again…

    • @keyframe41
      @keyframe41  11 วันที่ผ่านมา

      calamitous even

  • @hugocusson6496
    @hugocusson6496 16 วันที่ผ่านมา +2

    you should think about mixing your audio and compressing the music track so it doesnt distort as much and allows more room to your voice

  • @RedstonekPL
    @RedstonekPL 17 วันที่ผ่านมา +1

    so i guess i'll share my 2 cents and basically say that an rvalue is something that can appear _only_ on the right of the assignment of the operator
    e.g. you can do `foo = 3 + 5` but cannot do `3 + 5 = bar` or `&foo = bar`
    iirc thecherno made a video about these, great channel if you want to learn some c++ stuff like this

    • @keyframe41
      @keyframe41  16 วันที่ผ่านมา +1

      got it!

  • @wedding_photography
    @wedding_photography 16 วันที่ผ่านมา +4

    Next: move the simulation to the GPU. For 1 million particles.

    • @konstantin2296
      @konstantin2296 14 วันที่ผ่านมา

      I did that a few months ago :)
      It is able to handle around 1.3 mil objects at 60 fps. Been using CUDA for that

  • @tacolands
    @tacolands 15 วันที่ผ่านมา +2

    Is that a picture of the hugs building by lumphini park in bangkok?

  • @melanin_m85
    @melanin_m85 วันที่ผ่านมา

    make the window like a container box for your particles, it will be fun when you resize and drag the window around 😂

  • @williamist
    @williamist 19 วันที่ผ่านมา +1

    i did not see your first video but particle simulations are pretty cool...

  • @ak-gi3eu
    @ak-gi3eu 17 วันที่ผ่านมา +3

    Timestamps plz .really helps your video and channel 😊

    • @keyframe41
      @keyframe41  17 วันที่ผ่านมา

      done, thanks for reminding!

  • @OddlyTugs
    @OddlyTugs 15 วันที่ผ่านมา +1

    Could you rearrange to produce completely different images. like by normalizing the color between two images so they have the exact same pixels just what ever order

  • @QWERTIOX
    @QWERTIOX 20 วันที่ผ่านมา +3

    Algorithm gave me second video but not first

  • @ldlework
    @ldlework 17 วันที่ผ่านมา +1

    all particle sims are in slow mo

  • @baldi1640
    @baldi1640 17 วันที่ผ่านมา +1

    Well done, I've got a simple question tho, wouldn't it be easier to draw the image using a simple fragment shader?

  • @u2b842
    @u2b842 17 วันที่ผ่านมา +2

    Awesome work here !! There is just something i don't really understand, verlet Integration is usable for a constant timestep (because of the fact i uses the 3 different instants so 2 timesteps) but when using it in a simulation like you did, I get results defying the laws of physics, and I think it might be related to timesteps 🤷, so can you help me a bit on that please ?(I am only starting to loearn C++, which is the reason i don't just read the code). Once again, love your work

    • @keyframe41
      @keyframe41  17 วันที่ผ่านมา +1

      I'm not really getting what you're trying to say, but the code for part 1 is considerably easier to read, you can give it a try (the files are main_original, renderer_original, and solver_original)

    • @hmmmidkkk
      @hmmmidkkk 17 วันที่ผ่านมา +2

      it could be because he is checking for collisions for a single frame, more than fps which he mentioned in the part 1 and your simulation might not be doing that , I'm no expert tho

  • @sionsion1991
    @sionsion1991 13 วันที่ผ่านมา +2

    Instead of yielding you could do:
    auto localRamainingTasks = remaining_tasks.load(std::memory_order_relaxed);
    while (localRamainingTasks > 0){
    remaining_tasks.wait(localRamainingTasks);
    localRamainingTasks = remaining_tasks.load();
    }
    then in the complete tasks after the atomic sub if remaining tasks is 0 then remaining_tasks.notify_one()
    the perma yielding on an atomic is wasteful.

  • @defeatSpace
    @defeatSpace 10 วันที่ผ่านมา +1

    holy shit I SUCK!!!

  • @alexroznowski9376
    @alexroznowski9376 17 วันที่ผ่านมา +1

    Hi, could you share what VSCode theme you use and which extension you use to get the folder icons? I loved these videos

    • @keyframe41
      @keyframe41  16 วันที่ผ่านมา

      I use vscode-icons and atom one dark theme (both extensions).

  • @nathangonzales-hess6569
    @nathangonzales-hess6569 16 วันที่ผ่านมา +1

    when do we get a kerbal video?

  • @TV-jm3vq
    @TV-jm3vq 16 วันที่ผ่านมา +1

    Hmmmmmm, try changing the particles into a fluid? (The movement of the particles is very much like water.)

    • @keyframe41
      @keyframe41  16 วันที่ผ่านมา +2

      Stick around for part 3 which will be fluid simulation!

  • @RandomSWGStuffWithGames
    @RandomSWGStuffWithGames 14 วันที่ผ่านมา +1

    fix the mic take out the bottom 144hz with eq audacity to record voice has it

  • @vmarzein
    @vmarzein 15 วันที่ผ่านมา +1

    That image has gotta come from southeast asia lol

  • @HarryLarsson-b2n
    @HarryLarsson-b2n 20 วันที่ผ่านมา +2

    why does the wallpaper change every scene
    WHY IS YOUR TASKBAR ON THE TOP

    • @keyframe41
      @keyframe41  19 วันที่ผ่านมา +1

      its the menu bar

    • @trashpanda2214
      @trashpanda2214 17 วันที่ผ่านมา +1

      It's macos my man

  • @ericb314
    @ericb314 20 วันที่ผ่านมา +2

    Only 93 views? What

  • @只是約翰紐約市
    @只是約翰紐約市 20 วันที่ผ่านมา +1

    Great!

  • @ghcstyyy
    @ghcstyyy 20 วันที่ผ่านมา +1

    was on my fy

  • @Z0ctB0x
    @Z0ctB0x 17 วันที่ผ่านมา +1

    Dam dis epic as f*ck

  • @shade4467
    @shade4467 20 วันที่ผ่านมา +2

    Bruh my laptop can barely run 1000 particles😭

  • @VILED_DuDoSiK
    @VILED_DuDoSiK 16 วันที่ผ่านมา +2

    Very nice, go 3D)

  • @ttchef09
    @ttchef09 20 วันที่ผ่านมา

    🔥

  • @All_the_arcs
    @All_the_arcs 15 วันที่ผ่านมา +1

    Inspiring ✔
    Underrated ✔
    Entertaining ✔
    Subscribing? ✔✔✔✔✔✔