Page 1 of 1

Raycasting efficiency?

Posted: Sun Aug 19, 2012 8:06 pm
by chebob69
Just getting to grips with raycasting in bepu using space.RayCast and I'm seeing that there are quite a few overloads for it. I'm wondering what the most efficient method is for example, if I know my target body is always less than X metres away? Could you also give me a quick example of how the filtering works. For example, I'm using an RTS type camera that adjusts to the height of the terrain beneath it. How do I have a raycast that ignores the other bodies that are on the terrain and just returns the results for the terrain beneath them?

Re: Raycasting efficiency?

Posted: Sun Aug 19, 2012 8:17 pm
by Norbo
If you know something must be less than a certain distance away, use an overload which accepts a maximumLength parameter. Note that the length is specified in units of the ray direction's length. So, if the ray direction is 10 units long, a maximumLength of 5 would mean the ray can travel up to 50 units.

An example of the filter can be found in the BEPUphysicsDemos Camera class. The call looks like this:

Code: Select all

if (entityToChase.Space.RayCast(new Ray(lookAt, backwards), distanceToTarget, rayCastFilter, out result))...
The delegate rayCastFilter is created from the function:

Code: Select all

bool RayCastFilter(BroadPhaseEntry entry)
{
    return entry != entityToChase.CollisionInformation && (entry.CollisionRules.Personal <= CollisionRule.Normal);
}
This filter ignores the entity the camera is following and ignores intangible objects (as defined by their personal collision rule.)
How do I have a raycast that ignores the other bodies that are on the terrain and just returns the results for the terrain beneath them?
If you already know the specific object you'd like to ray cast against and you want to ignore all other objects, you can skip the Space.RayCast entirely. Instead, use that specific object's ray cast method. All entities have an EntityCollidable stored within the entity.CollisionInformation property; that EntityCollidable, like all Collidables, has a RayCast method. Terrains and other static geometry, being Collidables themselves, have RayCast methods as well.

Re: Raycasting efficiency?

Posted: Thu Aug 23, 2012 12:16 am
by chebob69
Excellent. Just wanted to check it was working before thanking you! Haven't got round to needing the filter yet but the terrain's working just fine. (the BEPU logo on my game's splashscreen keeps getting bigger)