-
-
Notifications
You must be signed in to change notification settings - Fork 131
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
Add spatial queries #53
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This allows rays to be cast relative to the position and rotation of the entity or its parent.
This is preference. but I like hit more. E.g. `RayHit` is nicer than `RayIntersection`.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request adds a
SpatialQueryPlugin
that is responsible for ray casting, shape casting, point projection, and intersection tests. There is also aSpatialQueryFilter
that can be used to select specific collision layers or to exclude entities.The
SpatialQueryPipeline
resource has aQbvt
that is used as an acceleration structure. The implementation of the pipeline and the queries is mostly based on Rapier'sQueryPipeline
, but modified to fit Bevy better and to provide a more consistent API.API
There are two types of APIs for spatial queries: the
SpatialQuery
system parameter and the component basedRayCaster
andShapeCaster
.SpatialQuery
SpatialQuery
is a new system parameter that provides similar querying methods as Rapier's global context. It is the most feature-rich option and great when you need a lot of control over when and how you want to ray cast for example.It looks like this:
There are similar methods for shape casts, point projection and intersection tests.
RayCaster
RayCaster
allows for a more component-based approach to ray casting that can often be more convenient than manually calling query methods. A system is run once per physics frame to fill theRayHits
component with the intersections between the ray and the world's colliders.Unlike the methods in
SpatialQuery
,RayCaster
uses a localorigin
anddirection
, so it will move with the body it is attached to or its parent. For example, if you cast rays from a car for a driving AI, you don't need to compute the new origin and direction manually, as theRayCaster
will follow the car.Using
RayCaster
looks like this:You can configure the ray caster's properties using various builder methods.
One caveat that isn't instantly clear is that the hits aren't in order by default because of the underlying
Qbvh
traversal algorithm. If the number of hits is larger than the ray caster'smax_hits
property, some hits will be missed, and this could be any hit including the closest one. If you want to guarantee that the closest hit is included,max_hits
must either be 1 or a value large enough to hold all of the hits.ShapeCaster
ShapeCaster
is very similar toRayCaster
, but it also has an associated shape and rotation, and it can only get the first hit.Using
ShapeCaster
looks like this:SpatialQueryFilter
The
SpatialQueryFilter
can be used to only include colliders with specificCollisionLayers
or to exclude specific entities from spatial queries. There are noflags
orpredicate
yet unlike in Rapier.Using a
SpatialQueryFilter
looks like this: (although this example doesn't make sense usage-wise)Todo
SpatialQuery
point_intersections
aabb_intersections_with_aabb
shape_intersections