-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bounding volumes for primitive shapes #10570
Comments
I've seen people get confused about the name "aabb2d" before because bounding rectangle is the more reasonable term, but I also like the consistent naming between the 2d and 3d variant. Working with actual coordinates and including the rotation on the aabb function seems like the right call, I'm actually not sure how bevy currently handles that with the aabbs for meshes (maybe @superdump can give us some insights) I think we might need to split the trait up into two parts:
As for the proposed trait:
A reference for the methods I use on my bvh's Aabb trait: fn center(&self) -> Self::Pos; // Like the proposed origin
fn area(&self) -> f32;
fn merge(&self, other: &Self) -> Self; // Gives a volume that contains both aabbs
fn padded(&self, padding: &Self) -> Self; // Like the proposed expand, but not uniform and two methods to get intersections with a ray or other aabbs (which should probably be in the other trait) |
Yeah I agree, they were just placeholder names. I mainly wanted to avoid Parry's If there is an expansion method, then I feel like it just logically makes sense that there would also be a size reduction method, just not sure what it should be called. For now, I fixed
This should be possible with the trait I proposed, since the associated
Yep, agreed. This could be on the AABB types themselves though since 2D AABBs don't have volume. Or maybe separate
Yup
Yup I updated my rough proposal to match most of the feedback. |
Published my BVH, since it might be an interesting reference: https://github.com/NiseVoid/ploc-bvh. It has this Aabb trait here, I think it's fairly similar to what was proposed, but the proposal here would much more general. If we do it well data structures like a BVH, and many other things, could just take a generic argument of type |
# Objective Implement bounding volume trait and the 4 types from #10570. I will add intersection tests in a future PR. ## Solution Implement mostly everything as written in the issue, except: - Intersection is no longer a method on the bounding volumes, but a separate trait. - I implemented a `visible_area` since it's the most common usecase to care about the surface that could collide with cast rays. - Maybe we want both? --- ## Changelog - Added bounding volume types to bevy_math --------- Co-authored-by: Alice Cecile <[email protected]>
# Objective Closes #10570. #10946 added bounding volume types and traits, but didn't use them for anything yet. This PR implements `Bounded2d` and `Bounded3d` for Bevy's primitive shapes. ## Solution Implement `Bounded2d` and `Bounded3d` for primitive shapes. This allows computing AABBs and bounding circles/spheres for them. For most shapes, there are several ways of implementing bounding volumes. I took inspiration from [Parry's bounding volumes](https://github.com/dimforge/parry/tree/master/src/bounding_volume), [Inigo Quilez](http://iquilezles.org/articles/diskbbox/), and figured out the rest myself using geometry. I tried to comment all slightly non-trivial or unclear math to make it understandable. Parry uses support mapping (finding the farthest point in some direction for convex shapes) for some AABBs like cones, cylinders, and line segments. This involves several quat operations and normalizations, so I opted for the simpler and more efficient geometric approaches shown in [Quilez's article](http://iquilezles.org/articles/diskbbox/). Below you can see some of the bounding volumes working in 2D and 3D. Note that I can't conveniently add these examples yet because they use primitive shape meshing, which is still WIP. https://github.com/bevyengine/bevy/assets/57632562/4465cbc6-285b-4c71-b62d-a2b3ee16f8b4 https://github.com/bevyengine/bevy/assets/57632562/94b4ac84-a092-46d7-b438-ce2e971496a4 --- ## Changelog - Implemented `Bounded2d`/`Bounded3d` for primitive shapes - Added `from_point_cloud` method for bounding volumes (used by many bounding implementations) - Added `point_cloud_2d/3d_center` and `rotate_vec2` utility functions - Added `RegularPolygon::vertices` method (used in regular polygon AABB construction) - Added `Triangle::circumcenter` method (used in triangle bounding circle construction) - Added bounding circle/sphere creation from AABBs and vice versa ## Extra Do we want to implement `Bounded2d` for some "3D-ish" shapes too? For example, capsules are sort of dimension-agnostic and useful for 2D, so I think that would be good to implement. But a cylinder in 2D is just a rectangle, and a cone is a triangle, so they wouldn't make as much sense to me. A conical frustum would be an isosceles trapezoid, which could be useful, but I'm not sure if computing the 2D AABB of a 3D frustum makes semantic sense.
What problem does this solve or what need does it fill?
Bounding volumes such as Axis-Aligned Bounding Boxes (AABBs) and bounding spheres are commonly used to speed up broad phase collision detection, spatial queries, and even rendering, and they are also used in Bounding Volume Hierarchies (BVHs). They are very lightweight primitives that can be used for quickly computing intersections.
Primitive shapes were just added in #10466, and it would be very useful to be able to compute bounding volumes for them. This would facilitate the usage of primitive shapes for a lot more things and help unify the engine's APIs.
What solution would you like?
Note: This is just my rough proposal for the API. Details can and should be changed for the final implementation.
Add
Aabb2d
andAabb3d
primitives:There could also be bounding spheres/circles:
(Oriented Bounding Boxes, or OBBs, could also be added, but they are less common)
Notice how these store the actual coordinates too. They will be required pretty much everywhere where bounding volumes are used (e.g. BVHs), so I think it's sensible to include. Feel free to correct me!
There could be a
BoundingVolume
trait shared by them:Now, each shape primitive should have methods for computing the bounding volumes:
These methods could be in traits like
Bounded2d
andBounded3d
:Potential use cases
These bounding volumes could be used for a wide variety of things:
While these are already possible, it is very useful to have a unified set of bounding volumes that can be used for all of them and can be computed from the existing set of primitive shapes.
The text was updated successfully, but these errors were encountered: