Neat AI does QuadTree Boids

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ย. 2024

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

  • @Midnotion
    @Midnotion 3 ปีที่แล้ว +86

    Calculating distance based on radius can be expensive, due to the need for a sqrt() call, but you can replace that check with squaring the other side of the equation (i.e. compare x^2 + y^2 vs radius^2), which is not only faster, but often easily cacheable since you end up using this value multiple times.

    • @jackb3493
      @jackb3493 2 ปีที่แล้ว +2

      Brilliant solution

    • @fwame6187
      @fwame6187 2 ปีที่แล้ว +10

      this is only true for much older computers, but in normal ones the square root instruction takes only a few cpu cycles

    • @touisbetterthanpi
      @touisbetterthanpi 2 ปีที่แล้ว +4

      Isn’t there silicon dedicated on the cpu these days to fast sqrt operations?

    • @chrstfer2452
      @chrstfer2452 2 ปีที่แล้ว +1

      @@fwame6187 not convinced sqrt isnt slower than multiplying

    • @jackb3493
      @jackb3493 2 ปีที่แล้ว

      @@chrstfer2452 depends on whether you are using a compute shader to do an obsene number of them for ray calc or shader physics

  • @dumnor
    @dumnor 2 ปีที่แล้ว +24

    Quadtrees (with tweaks) also work if you want to do gravitational calculations (planetary orbits, star systems). You can approximate masses far away being in single location in their quadtree address.

    • @neatai6702
      @neatai6702  2 ปีที่แล้ว +8

      good to know.. I'm going to mock up a sim of the solar system and see if an AI can plot a course to mars..

  • @phillipneal8194
    @phillipneal8194 3 ปีที่แล้ว +15

    Excellent !!! The coding train advice is spot on as well.

    • @neatai6702
      @neatai6702  3 ปีที่แล้ว +1

      Glad it was helpful!

  • @vb0t429
    @vb0t429 3 ปีที่แล้ว +6

    I love the quality of these videos - very simple yet entertaining!

    • @neatai6702
      @neatai6702  3 ปีที่แล้ว

      Glad you like them!

  • @Kraus-
    @Kraus- 3 ปีที่แล้ว +7

    The quadtree grid looks really neat in real time.

    • @neatai6702
      @neatai6702  2 ปีที่แล้ว

      its one of the reasons I made it.. It does look really good.. and its fast

  • @typicalhog
    @typicalhog 3 ปีที่แล้ว +4

    I don't think I knew about quadtrees, turns out they are really, really NEAT!

    • @neatai6702
      @neatai6702  3 ปีที่แล้ว

      Yup, they're probably my favorite for Boids.. and with lots of other applications..

  • @diophantine1598
    @diophantine1598 2 ปีที่แล้ว +4

    It should be possible for each boid to report if they have left a quad tree square. That could make a real time quad tree which has a lower performance overhead possible. It would be harder to implement though.

  • @TimmacTR
    @TimmacTR 3 ปีที่แล้ว +6

    Probably one method could also be to generate circles that grow slowly, and only take into consideration the first "x" number of boids around you (or stop when circle reaches vision limit)

    • @neatai6702
      @neatai6702  3 ปีที่แล้ว +3

      Yup, that was my first approach.. It still o(n^2) though as you need to check whos in the circle.. Combining it with the quadtree gives it a nice performance boost

  • @Qwerasd
    @Qwerasd 3 ปีที่แล้ว +4

    I've noticed you referring to what seems to be JavaScript as Java for some reason in several videos, are you aware of this mistake?

  • @grevel1376
    @grevel1376 ปีที่แล้ว

    Today I just came up with an algorithm for a dynamic quadtree and implemented it. Instead of rebuilding the quadtree each 100ms, it passes the moving boids around the nodes of the tree, and deletes useless children.
    I have two interchangeable implementations of a quadtree in the same project, dynamic and static. The static quadtree is 129 lines of code and the dynamic one is 187. I haven't tested the performance boost yet.
    I think it is much better, as it runs slightly faster (no hard data yet), and you do not loose any precision due to the limited rebuilding frequency.

    • @neatai6702
      @neatai6702  ปีที่แล้ว +1

      very nice, its on my list of things to do.. It was just easier to blank it and rebuild..

  • @DrDsHere
    @DrDsHere 2 ปีที่แล้ว +3

    Add more and more boids. It's unaboidable.

  • @nyscersul42
    @nyscersul42 2 ปีที่แล้ว

    Again, some lovely helpful thought shared here, thanks!

    • @neatai6702
      @neatai6702  2 ปีที่แล้ว +1

      Glad it was helpful!

  • @Kaepsele337
    @Kaepsele337 3 ปีที่แล้ว +4

    What is the advantage over using a fixed grid? Would that not reduce the amount of calculation that is needed?

    • @yowasupdud
      @yowasupdud 3 ปีที่แล้ว +6

      This system lets you have more detail in areas where more is going on. If you used a fixed grid there would be more sections where needless computations are being made.

  • @decodedbunny101
    @decodedbunny101 3 ปีที่แล้ว +1

    Love your vids lad!

    • @neatai6702
      @neatai6702  3 ปีที่แล้ว

      Thanks for the feedback !

  • @nastrimarcello
    @nastrimarcello 3 ปีที่แล้ว +1

    Great video again!

  • @AidanCadogan
    @AidanCadogan ปีที่แล้ว +1

    In the animation at 3:17, it looks like it's still storing points in the root node, instead of distributing them among the 4 new children nodes? not sure which way is correct

    • @Dalroc
      @Dalroc 11 หลายเดือนก่อน

      Pretty sure that's an error in the video as storing in the root doesn't do anything good.

  • @typicalhog
    @typicalhog 3 ปีที่แล้ว +5

    Yay, I'm famous now! 2:23

  • @VoidloniXaarii
    @VoidloniXaarii ปีที่แล้ว

    Wow, just wow

  • @sgreekcrown
    @sgreekcrown 3 ปีที่แล้ว +2

    Quadtree boids
    Take me home
    To the place
    Where I can code

  • @connorirwin8094
    @connorirwin8094 2 ปีที่แล้ว +1

    I’m not sure how easy it would be to do this in Java, but wouldn’t it work well to store pointers to the trees and which spatial location they are managing, so that when you need a new tree you can recycle an old, currently unused one instead of creating a new one?

  • @Madlion
    @Madlion 3 ปีที่แล้ว

    Your videos are very interesting

    • @neatai6702
      @neatai6702  3 ปีที่แล้ว

      Glad you like them!

    • @Madlion
      @Madlion 3 ปีที่แล้ว

      @@neatai6702 I also used rtNEAT for my master thesis, it's nice to see a channel called NEAT :)

  • @TheBestNameEverMade
    @TheBestNameEverMade 3 ปีที่แล้ว

    Spatial hash or layered spatial hash is gonna be a lot faster for this since it doesn't have expensive inserts.

  • @AGO339
    @AGO339 2 ปีที่แล้ว

    bit late to this but on the object avoidance (not the predator), couldnt you have a portion of that view range (distance forward) (so in terms, if 'object' is closer then ___ and further then ___ from "boid/me",) to have the boid move slower before completely turning away or going around the object, to give more realism to the object avoidance, and avoid the disjointed "forcefield" effet you currently have (not that forcefields arnt cool)
    so you end up with the boid growing slower when they get closer to an object they dont wanna crash into, and if they get too close then they avoid.

    • @neatai6702
      @neatai6702  2 ปีที่แล้ว

      yup, good idea.. I ended up just reducing the weight assigned to the avoidance factor for non boid objects.. If I drop it low enough they swoop around it instead of bouncing off it.

  • @jtrobotics5421
    @jtrobotics5421 3 ปีที่แล้ว +1

    No joke today i started implementing NEAT and was wondering about this exact problem of O(n²) and looked into quadtrees

    • @neatai6702
      @neatai6702  3 ปีที่แล้ว

      Great.... Spatial hashing is even quicker O(n).. I'm doing a video on that at the moment..

    • @5.0.5channelnotfound4
      @5.0.5channelnotfound4 3 ปีที่แล้ว

      Nice

  • @HenriqueErzinger
    @HenriqueErzinger 2 ปีที่แล้ว +1

    Javascript and Java are not the same, by a long margin.

  • @المحاربالثاقب
    @المحاربالثاقب 2 ปีที่แล้ว

    👍

  • @timseguine2
    @timseguine2 3 ปีที่แล้ว +2

    Another thing you could do especially for really large flocks would be to give up on exact calculations, and only check a random sample of boids for neighbors.

    • @2001herne
      @2001herne 3 ปีที่แล้ว

      That could even be faster than the quadtree method in some cases, given a large enough flock.

  • @grevel1376
    @grevel1376 2 ปีที่แล้ว

    at 1:51 you confused java and javascript, which is unacceptable

  • @overloader7900
    @overloader7900 2 ปีที่แล้ว +2

    What if instead of searching boids every frame from grid, the boids remembered their neighbors and only look at them until they are too far? And always try to have at least like 6-12 remembered neighbors, so that they collide less likely

    • @sebastianjost
      @sebastianjost 2 ปีที่แล้ว

      How would you handle new voids coming close with that method?
      You can't remember them if they were never close to each other before.

    • @overloader7900
      @overloader7900 2 ปีที่แล้ว

      @@sebastianjost Only search for new neighbor boids if the old ones went too far?

    • @sebastianjost
      @sebastianjost 2 ปีที่แล้ว

      @@overloader7900 but new voids can come close without others getting further away.
      Think of two groups meeting.

    • @overloader7900
      @overloader7900 2 ปีที่แล้ว

      @@sebastianjost If the distance is small enough, count of remembered high enough, then unlikely - all the possible spaces nearby would be occupied, and for new ones to come near, some would have to go. Those on the edges of boids will guard the insides.

    • @landru27
      @landru27 ปีที่แล้ว

      @@overloader7900 What about those boids who *are* on the edges? In a tight cluster, sure, the interior ones are surrounded, but the exterior ones by definition are not, but still have their full allotment of "nearest neighbors". If two such clusters drift close together, neither will be capable of detecting the other if they stop checking after checking only their "known neighbors". Also, what about a simulation with a far greater number of immobile objects -- e.g., birds flying through a forest; immobile objects by their nature are not going to be persistent "nearest neighbors" of any of the boids. How does a tight cluster of boids detect them? I'm genuinely curious how your scheme would handle these. Thanks.

  • @sureshsingh9880
    @sureshsingh9880 ปีที่แล้ว

    Provide source code please

  • @RavenAmetr
    @RavenAmetr 3 ปีที่แล้ว

    And where's the AI here?

    • @neatai6702
      @neatai6702  3 ปีที่แล้ว +2

      Some of my vid's are just smaller components of larger projects.. So they won't all have a direct AI element; but thats the general thrust of this channel..

  • @decodedbunny101
    @decodedbunny101 3 ปีที่แล้ว

    Am I early?