Тёмный

Quadtrees: Blazingly Fast Collision Detection 

Rust without rust
Подписаться 1,2 тыс.
Просмотров 22 тыс.
50% 1

#gamedev #gamedev #2d #coding
Detecting collisions can be done by going through each object for each object, but that'd be fairly slow. A beautiful and efficient way is using quadtrees.
Sound at the end: Noah Barger - Piano Tune.mp3
Demonstration link on GitHub: kul-sudo/quadtrees

Опубликовано:

 

16 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 65   
@DanDeebster
@DanDeebster 7 дней назад
That's some weird looking Rust code.
@rustwithoutrust
@rustwithoutrust 7 дней назад
that's C
@DanDeebster
@DanDeebster 7 дней назад
@@rustwithoutrust Indeed. C wasn't what I had in mind from Rust Without Rust!
@reo101
@reo101 7 дней назад
same, lmao
@rustwithoutrust
@rustwithoutrust 7 дней назад
kekw
@Argletrough
@Argletrough 20 часов назад
@@DanDeebster Why not? It's right in the name: *without* Rust!
@cobbcoding
@cobbcoding 9 дней назад
BLAZINGLY FAST 🔥🔥🔥
@rustwithoutrust
@rustwithoutrust 9 дней назад
it's actually all a scam
@strawberryutopia
@strawberryutopia 6 дней назад
Those last 10 seconds was something I never quite got with quad trees, until I saw this video. So like… because cells overlap, an object can be in multiple cells? That’s subtle but genius!
@rustwithoutrust
@rustwithoutrust 6 дней назад
Yes, if the cells overlap by 2 object radiuses, then collisions can be easily handled between several cells.
@strawberryutopia
@strawberryutopia 6 дней назад
@@rustwithoutrust 🌌🧠
@ClokworkGremlin
@ClokworkGremlin 5 дней назад
When I did my test implementation, I made it so that cells aren't split in half, they're split based on the bounds of the objects within them, so almost all cells overlap, but if two objects might be colliding then they're guaranteed to both be in at least one cell.
@a.lollipop
@a.lollipop 4 дня назад
this will be a great channel to follow as im making my own game engine haha, great content! I never thought of this approach, this is very nice :3
@rustwithoutrust
@rustwithoutrust 4 дня назад
thanks!
@NinetyUnderScore
@NinetyUnderScore 5 дней назад
quadtrees make me happy
@rustwithoutrust
@rustwithoutrust 5 дней назад
relatable
@BenjaminRosseaux
@BenjaminRosseaux 3 дня назад
As a physics engine developer, I want to point out that while QuadTrees and Octrees are often praised for their performance, they aren't always ideal for certain tasks, especially in physics engines that require advanced features like raycasting or shape/sphere casting. These data structures struggle with irregularly sized objects that exceed the size of a cell, and there's significant overhead when performing raycasting because of the DDA-style stepping required through the Quadtree or Octree cells. Physics engines like Bullet, Box2D, Jolt, and my own full-featured 3D physics engine KRAFT (written in Object Pascal) all use dynamic, self-balancing AABB BVH (Axis-Aligned Bounding Box Bounding Volume Hierarchies) trees for optimal performance. PhysX, on the other hand, relies on Sweep and Prune (S&P) for broad-phase pair management and also uses AABB BVH trees for efficient raycasting.
@rustwithoutrust
@rustwithoutrust 3 дня назад
do you think quadtrees are optimal for those 2d collision demonstrations?
@BenjaminRosseaux
@BenjaminRosseaux 3 дня назад
​@@rustwithoutrust For 2D and 3D collision demos with almost similarly sized objects (for example particles), QuadTrees and Octrees are pretty solid and get the job done nicely 😊. They’re a good choice when everything’s about the same size, but for more complex physics, raycasting or different-sized objects, other approaches might work better 😉.
@stefanalecu9532
@stefanalecu9532 День назад
Hey, didn't expect to see you here! I wanted to thank you for your work on Kraft, it's been a pleasure to go through the source code and learn a thing or two! Why haven't you split up the source, since kraft.pas is a really long file? Are you aiming for having a unit-only physics engine?
@brickmasterhunt4561
@brickmasterhunt4561 День назад
@@BenjaminRosseaux Would there be a way to dynamically effect the radius of each object in the quad tree?
@BenjaminRosseaux
@BenjaminRosseaux День назад
@@brickmasterhunt4561 Although it is possible to have objects with different radii in a Quadtree or Octree, this approach is not very optimal. The main issue is that objects would have to overlap into neighboring cells based on their new radius, complicating the structure. Additionally, neighboring cells would need to be constantly checked and updated based on the object's radius, increasing complexity and potentially leading to an O(n²) runtime in the worst case. This becomes particularly inefficient as the number of objects grows. Moreover, this approach introduces inefficiencies since Quadtrees and Octrees are designed more primarily for fixed bounding volumes, uniform element sizes, or otherwise purely static runtime lookups. Different radii and different object sizes (especially those larger than a Quadtree or Octree leaf cell) would negate the spatial partitioning benefits in the dynamic usage case. Alternatives like dynamic self-balancing AABB BVH/BIH trees or even loose Quadtrees/Octrees can handle varying object sizes and radii more efficiently.
@SmirkInvestigator
@SmirkInvestigator 5 дней назад
Wake me up when it’s oct trees
@rustwithoutrust
@rustwithoutrust 5 дней назад
ok, no need for an alarm clock
@SmirkInvestigator
@SmirkInvestigator 5 дней назад
@@rustwithoutrust lol. Definitely face palmed when reflecting on my own novice attempts at collision detection. Great video. Subscribed.
@rustwithoutrust
@rustwithoutrust 5 дней назад
thanks! yeah, most of those ways aren't that obvious initially
@alleycatsphinx
@alleycatsphinx 2 дня назад
octrees or octonion trees?
@rawallon
@rawallon 8 дней назад
Thats craaazy
@rustwithoutrust
@rustwithoutrust 8 дней назад
real
@superfliping
@superfliping День назад
Amazing thank you
@BreadProduct
@BreadProduct 7 дней назад
Hm dynamic area codes. Is this optimal for uniformly sized objects like you have here? Since they are all the same size you could scale the area the objects are located such that the radius of the objects you are testing is so small it might as well just be a location in space. Then you don't have to worry if any object is overlapping more than one area code and you can simplify the collision to two coordinate equality tests.
@HansMilling
@HansMilling 6 дней назад
I created map applications in my previous job. Having kilometres/miles of road polygons, buildings, forests, rivers, lakes, cables, lamp posts, side walks etc. a large region could consist of 5 gigabytes of vector data and having the quad trees in files with indexes to the vectors we could zoom anywhere on the map and draw the area in just 20ms. Large vectors would be clipped before drawing to optimise drawing speed. Also large polygons or lines would be stored on upper levels and these span multiple quads.
@ClokworkGremlin
@ClokworkGremlin 4 дня назад
Even for uniformly distributed objects, splitting the test area reduces the total number of collision checks which need to be made, so it does help a lot. Where quad trees should be most helpful is when you have irregularly distributed objects, which allows some of the branches to just be ignored entirely.
@gsestream
@gsestream 6 дней назад
sometimes you only need to sort bounding volume axis limits to get the collisions in O(n) time, faster than quadtrees.
@ClokworkGremlin
@ClokworkGremlin 4 дня назад
The thing about Bound and Sweep is that it's only O(n) *after* the bounds have been sorted. Nearly all known sorting algorithms are O(nlogn), which quad trees are too, so while Bound and Sweep is a lot faster than a brute force approach (where you test every possible collision) would be, it's still slower than most spatial hashing algorithms, including the one featured in this video. I actually just spent some time implementing and testing in my collision test bench, and while it's competitive at
@gsestream
@gsestream 4 дня назад
@@ClokworkGremlin use O(n) sorting algorithm, like radix binary tree sort, you can add and remove items from the tree super fast without a new sort also, I just also tested that sorting algorithm in java against Arrays.sort, holds up until like millions of elements, because of system sort native optimizations.
@gsestream
@gsestream 4 дня назад
@@ClokworkGremlin yes you can modify the axis sort tree just like with 2d quad tree and 3d octree.
@gsestream
@gsestream 4 дня назад
@@ClokworkGremlin in other words, you can separate the axes for the limit values completely, ie separately check x, y and z axes limit intersections. and use linear time sort. ironically radix binary tree sort is 1D binary tree.
@Tiparium_NMF
@Tiparium_NMF 5 дней назад
How well do quadtrees adapt to three dimensions? I have a project that handles hundreds of thousands of particle interactions in 3D space, the approach I used was fixed grid where particles only talk to other particles in their own or adjacent grid slots. But because the grid is fixed, it can still have fairly substantial performance slowdowns if the particles are forced to congregate.
@rustwithoutrust
@rustwithoutrust 5 дней назад
You'd use octrees for that
@mgostIH
@mgostIH 7 дней назад
Very nice! I want to use something similar but on GPUs, basically render all these points interacting with each other through gravity (or charge repulsion), I was wondering how fast a Rust implementation would be to render them in real time. How many points did you have in this video and how fast was the simulation? I was wondering what would it be like for ~10k points, and also whether you multithreaded this, I think quadtrees might be a bit hard to parallelize but the spatial partitioning idea seems simpler.
@rustwithoutrust
@rustwithoutrust 7 дней назад
kul-sudo/quadtrees on GitHub. Have it done with gravity.
@Stagnated541
@Stagnated541 5 дней назад
Can you implement this on scratch so I can see this more clearly? When working on quad-trees, I noticed that it is impossible to make it work when you have a dense map of small and big objects I just don't understand 2:02 to the end
@rustwithoutrust
@rustwithoutrust 2 дня назад
You can check out kul-sudo/quadtrees on GitHub for the implementation
@user-fj9hf4bu9f
@user-fj9hf4bu9f 17 часов назад
probably would have been better to do this in either C++ or Python, as the Rust code is clearly not comprehensible.
@rustwithoutrust
@rustwithoutrust 6 часов назад
that's C
@webkinskid
@webkinskid 5 дней назад
Does blazingly fast refer to the algorithm or the video??
@rustwithoutrust
@rustwithoutrust 5 дней назад
good question
@fizipcfx
@fizipcfx 5 дней назад
what is that weird effect where it looks like text is jittering
@rustwithoutrust
@rustwithoutrust 5 дней назад
blur
@leeroyjenkins0
@leeroyjenkins0 3 дня назад
​@@rustwithoutrustvery avant-garde 🧐
@rustwithoutrust
@rustwithoutrust 3 дня назад
@leeroyjenkins0 Yeah, have you seen the subtitles? I've been grinding French and decided it'd be good to make French subtitles. If that's what you mean.
@leeroyjenkins0
@leeroyjenkins0 2 дня назад
@rustwithoutrust oh! 😂 No sorry I just picked an artsy word for fun Best of luck with that! It really is a crazy language..! I happen to speak it fluently, it's very understandable! Other than working on common idioms. Some notes as an aside "here's the point" would probably be "en résumé" in this context, and often point would be more "argument" than "point" even if it's usually understood, especially in Canada I'd guess. "Move into the cell" would be more "Dans" than "dedans" if you specify the object of "what is it moving into" you don't use the "de" as that's what "de" is referring to. "Utile pour des collisions" would be more "utile pour les collisions" mostly because you're talking about "collisions" as a concept. Small details like that. Also, for this type of video you'd probably address the audience as a plurality so sticking more towards "vous" (not necessarily as the polite form, just addressing all viewers at once), but that's nuanced by the target audience, including the fact that "tu" is more prevalent in french Canadian. That aside neat bite-sized video ^^ Cheers!
@rustwithoutrust
@rustwithoutrust 2 дня назад
I'll probably make French subtitles for all my videos, because that could be good practice for me. Moreover, I'm soon starting to read a book in French that'll definitely boost me and give me more understanding of those little things!
@ClokworkGremlin
@ClokworkGremlin 5 дней назад
[Edit] Correction: the number in the corner is processor time, not point count, as per 1:30 [Original post]1:35 FOUL! Your brute force implementation used 500,000 points, but your quadtree implementation used only 50,000. It's easy to get a massive speedup when you reduce the test size by 90%.
@rustwithoutrust
@rustwithoutrust 5 дней назад
Excuse me, what? That is simply a lie without any base under it. Moreover, you can run it all yourself and see that difference.
@ClokworkGremlin
@ClokworkGremlin 5 дней назад
@@rustwithoutrust I assumed the numbers on the corner of the screen are the particle counts. Is that not what they are?
@rustwithoutrust
@rustwithoutrust 5 дней назад
It's said it's the processor time
@cobbcoding
@cobbcoding 5 дней назад
@@ClokworkGremlin you should maybe watch the video you're commenting on? He literally says what the number is in the video.
@ClokworkGremlin
@ClokworkGremlin 5 дней назад
@@rustwithoutrust Ah. I missed where that was mentioned at 1:30-1:31. How many points were in this test sample? I've found the overall gain varies wildly based on the density of points being tested. At 1,000 points, varying from 1 and 5 units in radius, across a 1,000x1,000 unit square, I have both a sparse grid-based system and a quadtree based system that run close to 20x as fast as the brute force solution. At 10,000 or more points under the same conditions, the brute force solution is no longer viable due to both time and memory constraints. Working on a bounds sorting solution now, to see how it compares. The dense grid works about the same as the sparse grid, but takes up considerably more memory.
Далее
AI can't cross this line and we don't know why.
24:07
Просмотров 435 тыс.
The Genius Behind the Quantum Navigation Breakthrough
20:47
The unexpected probability result confusing everyone
17:24
The Midpoint Circle Algorithm Explained Step by Step
13:33
Making Successful Indie Games Is Simple (But Not Easy)
12:08
Replace Is Number Saves 440GB A WEEK
9:54
Просмотров 172 тыс.
How I Beat The Password Game
39:53
Просмотров 66 тыс.
Visualizing 4D Pt.1
22:56
Просмотров 547 тыс.
I Hacked Diablo II To Use Modern Graphics
13:16
Просмотров 111 тыс.
PirateSoftware Breaks Down CrowdStrike Computer Issue
12:56