Page 1 of 1

Raycasting hit point

Posted: Sat Aug 22, 2020 11:01 pm
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!

Re: Raycasting hit point

Posted: Sat Aug 22, 2020 11:26 pm
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.

Re: Raycasting hit point

Posted: Sat Aug 22, 2020 11:41 pm
by Senlaar
That's perfect, thanks! And for such a quick reply.

Re: Raycasting hit point

Posted: Sun Aug 23, 2020 12:22 am
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!

Re: Raycasting hit point

Posted: Sun Aug 23, 2020 1:36 am
by Norbo
it is done 🐍