Hi there, I've been using BEPU for several months now and I really appreciate it. However I can't find any informations on how to do sphere casts. I have a sphere in my 3D world and would like to cast a bigger sphere at the same location and retrieve the list of entities hit by the sphere cast in all directions. How should I do that with BEPU? I saw the Entity.CollisionInformation.ConvexCast() but can't figure how to use it :s.
Thanks for your help!
How convex casts work?
Re: How convex casts work?
Here's an example of how to do a convex cast. Basically, compute the bounding box of the shape cast, ask the broad phase what's in that volume, and test those objects.
However:
But, if you truly want to sweep the shape, the convex cast is indeed what you need.
Code: Select all
//Create the shape to cast.
var sphereShape = new SphereShape(.5f);
//Where is the does the shape start?
var startingTransform = new RigidTransform(new Vector3(0, 12, 0), Quaternion.Identity);
//Which way is the cast going?
var sweep = new Vector3(0, -5, 0);
//Compute a rough sweep volume so we can ask what is inside it.
BoundingBox boundingBox;
//Don't get tripped up about the 'local' portion of this method. It's used for things like InstancedMeshes and MobileMeshes which
//have to perform sweeps against their own separate version of reality. Since we're just casting against the untransformed regular
//world, the local transform is just the identity transform.
//This sweeping process could be done manually pretty easily. Just ask the shape for the bounding box and then use the sweep to push out the bounds.
var identity = AffineTransform.Identity;
sphereShape.GetSweptLocalBoundingBox(ref startingTransform, ref identity, ref sweep, out boundingBox);
//Use the broad phase's acceleration structure to find all the objects which might be hit by the convex cast.
//Since a bounding box is used here, care must be taken to avoid long convex casts in diagonal directions which might
//gather many candidates.
var candidates = Resources.GetBroadPhaseEntryList();
Space.BroadPhase.QueryAccelerator.BroadPhase.QueryAccelerator.GetEntries(boundingBox, candidates);
var entities = Resources.GetEntityRawList();
foreach (var candidate in candidates)
{
//This will check to see if the broad phase entry is associated with an entity since we're only interested in entities.
//(Could do testing based on the Tag of the BroadPhaseEntry as well.)
var entityCollidable = candidate as EntityCollidable;
if (entityCollidable != null) //Could do other filtering here too.
{
//Found one! Does our convex cast hit it?
RayHit rayHit;
if (entityCollidable.ConvexCast(sphereShape, ref startingTransform, ref sweep, out rayHit))
{
//Yup!
entities.Add(entityCollidable.Entity);
}
}
}
That implies you might be looking for something besides a convex cast. Do you just want to know what objects touch the larger sphere? If so, then using something like the CharacterController's QueryManager would work. Basically, it performs some narrow phase tests between an object and the environment to find contacts and touched objects. There's other ways to do this too; another collidable could persist within the space and detect contacts over time.I have a sphere in my 3D world and would like to cast a bigger sphere at the same location and retrieve the list of entities hit by the sphere cast in all directions.
But, if you truly want to sweep the shape, the convex cast is indeed what you need.
Re: How convex casts work?
Thanks for the fast answer! Looks like the QueryManager is what I was looking for 
