Raycasting efficiency?

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
chebob69
Posts: 40
Joined: Thu Aug 16, 2012 7:27 pm

Raycasting efficiency?

Post 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?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Raycasting efficiency?

Post 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.
chebob69
Posts: 40
Joined: Thu Aug 16, 2012 7:27 pm

Re: Raycasting efficiency?

Post 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)
Post Reply