Page 1 of 1

[V2] Any sample for forcefield?

Posted: Sat Nov 28, 2020 12:28 am
by b1fg2
is there any sample for forcefield simulation? E.g a forcefield volume will affect any object that inside the volume.

Re: [V2] Any sample for forcefield?

Posted: Sat Nov 28, 2020 1:12 am
by Norbo
Not at the moment, but querying the broad phase for bodies with overlapping bounding boxes is a good start. Something like:

Code: Select all

            var broadPhaseEnumerator = new BroadPhaseOverlapEnumerator { Pool = BufferPool, References = new QuickList<CollidableReference>(16, BufferPool) };
            Simulation.BroadPhase.GetOverlaps(queryBoundsMin, queryBoundsMax, ref broadPhaseEnumerator);
where the BroadPhaseOverlapEnumerator is:

Code: Select all

        struct BroadPhaseOverlapEnumerator : IBreakableForEach<CollidableReference>
        {
            public QuickList<CollidableReference> References;
            //The enumerator never gets stored into unmanaged memory, so it's safe to include a reference type instance.
            public BufferPool Pool;
            public bool LoopBody(CollidableReference reference)
            {
                References.Allocate(Pool) = reference;
                //If you wanted to do any top-level filtering, this would be a good spot for it.
                //The CollidableReference tells you whether it's a body or a static object and the associated handle. You can look up metadata with that.
                return true;
            }
        }
You could then walk over the overlaps and modify the velocity of any detected bodies.

The collision query demo shows how to detect bodies with contact-level precision. It's more complicated since it involves the CollisionBatcher.