-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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 RayTest2d and RayTest3d #11310
Add RayTest2d and RayTest3d #11310
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is more of a physics thing, and Bevy doesn't have a physics library yet. So I don't really think it's the right time to add something like this.
I can understand the confusion, but intersection tests aren't really specific to physics. Some examples would be raycasting to find the entity under your cursor, or a raytracing renderer. Other intersection tests can be even more common, like intersections between bounding volumes which can be used to find all nearby entities for enemy AI or a visibility system. |
out.mp4Made a modified version of Jondolf's intersection test example to test it, it seems to work now, even in some of the edge cases (like things being behind the ray origin, or the ray origin being inside the volume). |
It's weird that on the bottom AABB in the video, the hit seems to occasionally produce slightly wrong results (they're not on the surface of the AABB). All the other collisions seem correct and I couldn't find a mistake in the code. |
Noticed the same as @IQuick143, but for the middle bounding circles, at around 13 seconds. It seems to be wrong for multiple frames |
In this case the points are not actually wrong, the left one touches the surface of the left sphere, the right one is inside the right sphere so it stays at the origin. There is however some weirdness going on with the bottom AABB, which I think might just be an error in the example (the code isn't written in such a way that each volume is handled by the exact same code) |
let offset = self.ray.origin - sphere.center; | ||
let projected = offset.dot(*self.ray.direction); | ||
let closest_point = offset - projected * *self.ray.direction; | ||
let distance_squared = sphere.radius().powi(2) - closest_point.length_squared(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this value is actually a squared distance, though I can't figure out a better name for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, distance
is a bit of a weird term ... It's the distance from the outside surface to the projected point. It's like a reversed signed distance 🙃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems good to me now!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the code, but this definitely needs a test suite before I'm happy with merging it. Problems in this type of code are really nasty to detect and isolate downstream otherwise.
I guess for tests we could do
I think that would effectively cover most cases. Idk if point D) is a bit too much, if @NiseVoid wants, I could try implementing the tests myself. |
I'll add some tests as described by @IQuick143, that seems like a fairly decent set of cases. Adding an example in a later later PR for intersection tests would probably also help with the debugging process if users hit any weird issues |
# Objective Implement a raycast intersection test for bounding volumes ## Solution - Implement RayTest2d and RayTest3d types --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: IQuick 143 <IQuick143cz@gmail.com>
Objective
Implement a raycast intersection test for bounding volumes
Solution