Raycasting hit point

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Senlaar
Posts: 12
Joined: Mon Jun 22, 2020 6:11 am

Raycasting hit point

Post by Senlaar »

Hello! Having a great time with the library, really well done.

I'm looking to implement a very primitive "is the body airborne" check by simply ray casting straight down, and if a hit occurs check whether the distance to the hit point is greater than some arbitrary value. Two questions there:

1. I see how the IHitHandler interface will provide the Collidable that was hit, but I don't see the actual Vector3 point of the hit. Is that available?

2. I'm assuming Simulation.Raycast reports all collidables that would be struck by the ray. For performance sake is there a way to call it as "report ONLY the closest hit, and then stop considering others" ?

Thanks much!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Raycasting hit point

Post by Norbo »

1. I see how the IHitHandler interface will provide the Collidable that was hit, but I don't see the actual Vector3 point of the hit. Is that available?
IRayHitHandler.OnRayHit provides a 't' value which is how far along the ray the hit was. So, rayHitLocation = ray.Origin + ray.Direction * t. (It's not output directly because some intersection tests don't need to compute a hit location to figure out the t value; just a very minor optimization.)
2. I'm assuming Simulation.Raycast reports all collidables that would be struck by the ray. For performance sake is there a way to call it as "report ONLY the closest hit, and then stop considering others" ?
That sort of behavior is left up to the IRayHitHandler; note that the OnRayHit function passes the maximumT by reference, so it can be set. The OnRayHit callback is called from within the traversal, so modifying the maximumT means that future traversal or intersection tests will obey the new maximumT. Combined with the fact that the traversal visits the closer bounding box first, this turns out exactly equivalent to a 'first hit only' traversal.

In other words, in OnRayHit, set maximumT = t.
Senlaar
Posts: 12
Joined: Mon Jun 22, 2020 6:11 am

Re: Raycasting hit point

Post by Senlaar »

That's perfect, thanks! And for such a quick reply.
Senlaar
Posts: 12
Joined: Mon Jun 22, 2020 6:11 am

Re: Raycasting hit point

Post by Senlaar »

Also SUPER small thing: the in-code documentation for the t param is

Code: Select all

/// <param name="t">Hit time of the sweep test.</param>
I'm not familiar with those terms so it threw me for a bit of a loop - in case other folks aren't as well you could just add "(or the scalar distance down the ray from its origin to the hit point.)"

Thanks again!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Raycasting hit point

Post by Norbo »

it is done 🐍
Post Reply