Does Bepu v1 supports query overlapped object?

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
starlaoxmelgs
Posts: 15
Joined: Mon Jan 13, 2020 8:26 am

Does Bepu v1 supports query overlapped object?

Post by starlaoxmelgs » Wed Mar 18, 2020 4:11 am

Similar to Unity's Physics.OverlapBox, Physics.OverlapSphere.
https://docs.unity3d.com/ScriptReferenc ... apBox.html
https://docs.unity3d.com/ScriptReferenc ... phere.html

Given a box, find all colliders touching or inside of the given box.

Norbo
Site Admin
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: Does Bepu v1 supports query overlapped object?

Post by Norbo » Wed Mar 18, 2020 7:03 pm

There are two stages: querying for potential overlaps, and then computing contact level data.

You can ask the broadphase for potential overlaps using a bounding sphere or bounding box with Space.BroadPhase.QueryAccelerator.GetEntries, and then you can walk through those, generate pairs, and then evaluate those pairs for contact data.

A related example can be found in the character controller's query manager: https://github.com/bepu/bepuphysics1/bl ... er.cs#L144

Rather than using the broad phase query, it is just enumerating nearby objects detected in the last frame, but you can easily enumerate results from GetEntries instead.

Notably, in many cases the bounding volume query is sufficient. Explosions, for example, don't require contact level fidelity for the most part- you can get away with a broad phase query and position tests.

starlaoxmelgs
Posts: 15
Joined: Mon Jan 13, 2020 8:26 am

Re: Does Bepu v1 supports query overlapped object?

Post by starlaoxmelgs » Thu Mar 19, 2020 2:56 am

Thanks!
That really helps :D

starlaoxmelgs
Posts: 15
Joined: Mon Jan 13, 2020 8:26 am

Re: Does Bepu v1 supports query overlapped object?

Post by starlaoxmelgs » Fri Mar 20, 2020 4:04 am

Code: Select all

List<Entity> OverlapBox(BEPUutilities.Vector3 position, float width, float height, float length)
{
    List<Entity> touchedEntity = new List<Entity>();

    Box detectBox = new Box(position, width, height, length);
    _space.Add(detectBox);

    BEPUutilities.BoundingBox boundingBox = detectBox.CollisionInformation.BoundingBox;
    RawList<BroadPhaseEntry> affectedEntries = new RawList<BroadPhaseEntry>();
    _space.BroadPhase.QueryAccelerator.GetEntries(boundingBox, affectedEntries);
    for (int i = 0; i < affectedEntries.Count; i++)
    {
        EntityCollidable entry = affectedEntries[i] as EntityCollidable;
        if (entry.entity == detectBox)
            continue;

        CollidablePair pair = new CollidablePair(detectBox.CollisionInformation, entry);
        CollidablePairHandler pairHandler = NarrowPhaseHelper.GetPairHandler(ref pair);
        if (pairHandler.CollisionRule == CollisionRule.Normal)
        {
            pairHandler.SuppressEvents = true;
            pairHandler.UpdateCollision(0);
            pairHandler.SuppressEvents = false;
            if (pairHandler.Contacts.Count > 0)
                touchedEntity.Add(entry.entity);
        }
        pairHandler.CleanUp();
        pairHandler.Factory.GiveBack(pairHandler);
    }
    _space.Remove(detectBox);
    return touchedEntity;
}
I have implemented OverlapBox(Find all touched enitites)
There are 2 stages
1、Find potential overlaps using boundsbox
2、Generate CollidablePair for detectBox and potential overlap entity, check if it has contact point, if has any, then it's overlaped

But something looks weird.
To Generate contact point, I have add the detectBox to Space, otherwise there won't be any contact point.
And because I don't want other Collidable to collide with detectBox in the later process, so I have to remove it after overlap detection is finished.

Add and Remove looks weird, but I don't know a better way.
You got any idea?

Norbo
Site Admin
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: Does Bepu v1 supports query overlapped object?

Post by Norbo » Fri Mar 20, 2020 11:25 pm

Adding the box to the Space is not required. The issue was that the bounding box of a new Box is not initialized until after the box is added to the broad phase by default.

So, with minimal changes to that snippet:

Code: Select all

List<Entity> OverlapBox(BEPUutilities.Vector3 position, float width, float height, float length)
{
    List<Entity> touchedEntity = new List<Entity>();

    Box detectBox = new Box(position, width, height, length);
    detectBox.CollisionInformation.UpdateBoundingBox();

    BEPUutilities.BoundingBox boundingBox = detectBox.CollisionInformation.BoundingBox;
    RawList<BroadPhaseEntry> affectedEntries = new RawList<BroadPhaseEntry>();
    Space.BroadPhase.QueryAccelerator.GetEntries(boundingBox, affectedEntries);
    for (int i = 0; i < affectedEntries.Count; i++)
    {
        EntityCollidable entry = affectedEntries[i] as EntityCollidable;
        if (entry.Entity == detectBox)
            continue;

        CollidablePair pair = new CollidablePair(detectBox.CollisionInformation, entry);
        CollidablePairHandler pairHandler = NarrowPhaseHelper.GetPairHandler(ref pair);
        if (pairHandler.CollisionRule == CollisionRule.Normal)
        {
            pairHandler.SuppressEvents = true;
            pairHandler.UpdateCollision(0);
            pairHandler.SuppressEvents = false;
            if (pairHandler.Contacts.Count > 0)
                touchedEntity.Add(entry.Entity);
        }
        pairHandler.CleanUp();
        pairHandler.Factory.GiveBack(pairHandler);
    }
    return touchedEntity;
}

starlaoxmelgs
Posts: 15
Joined: Mon Jan 13, 2020 8:26 am

Re: Does Bepu v1 supports query overlapped object?

Post by starlaoxmelgs » Mon Mar 23, 2020 6:08 am

Problem solved.
Thx again :D

starlaoxmelgs
Posts: 15
Joined: Mon Jan 13, 2020 8:26 am

Re: Does Bepu v1 supports query overlapped object?

Post by starlaoxmelgs » Wed Apr 01, 2020 1:34 am

How to distinguish what type of objects are overlapped?
For example, there are some boxes, spheres, staticmesh in scene.
I make a overlap detection, and 7 objects are detected.
I want to know they are box, or spheres, or staticmesh.

Norbo
Site Admin
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: Does Bepu v1 supports query overlapped object?

Post by Norbo » Wed Apr 01, 2020 1:39 am

You could try casting the BroadPhaseEntry to a Collidable, and examine the type of its Shape. Or you could store some custom metadata in the Tag property of the relevant BroadPhaseEntry instances. (Note that for entities, the entity.Tag is not the same as the entity.CollisionInformation.Tag; the latter is the BroadPhaseEntry tag.)

starlaoxmelgs
Posts: 15
Joined: Mon Jan 13, 2020 8:26 am

Re: Does Bepu v1 supports query overlapped object?

Post by starlaoxmelgs » Wed Apr 01, 2020 2:18 am

Cool :D
Thanks again

starlaoxmelgs
Posts: 15
Joined: Mon Jan 13, 2020 8:26 am

Re: Does Bepu v1 supports query overlapped object?

Post by starlaoxmelgs » Wed Apr 01, 2020 9:12 am

I can distinguish BoxShape now, but how to obtain its position, rotateion, scale?

Code: Select all

BEPUphysics.Entities.Prefabs.Box box = new BEPUphysics.Entities.Prefabs.Box(new Vector3(10, 10, 10), 1, 1, 1);
box.Orientation = new Quaternion(0.5f, 0.5f, 0.5f, 0.5f);
space.Add(box);
For example, I create a box, then use overlap to get it.
After casting Collidable.Shape to BoxShape, I can get it's width, height, length.
But how can I obtain its position, rotation, scale?

Norbo
Site Admin
Posts: 4884
Joined: Tue Jul 04, 2006 4:45 am

Re: Does Bepu v1 supports query overlapped object?

Post by Norbo » Wed Apr 01, 2020 9:44 pm

The BroadPhaseEntry type associated with an Entity is the EntityCollidable. You could try a cast then use the Entity property, then check the position/orientation.

For static colliders, you can cast the BroadPhaseEntry directly to the type. For example, StaticMesh is a BroadPhaseEntry.

Post Reply