How do I make a RayCast more like a Collidable?

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Spankenstein
Posts: 249
Joined: Wed Nov 17, 2010 1:49 pm

How do I make a RayCast more like a Collidable?

Post by Spankenstein »

I've created a TriggerVolume class that reacts to other ColliableObjects that are of the type 'ITrigger'.

The TriggerVolume will react when an object collides with it and matches the TriggerType for the TriggerVolume:

Code: Select all

        public void Events_InitialCollisionDetected(EntityCollidable info, Collidable entry, CollidablePairHandler handler)
        {
            ITrigger t = entry as ITrigger;

            if (t == null)
            {
                return;
            }

            // Only respond to trigger types that match the trigger types for this class
            if ((t.TriggerType & triggerType) != t.TriggerType)
            {
                return;
            }

            int numTriggerIDs = triggerIDs.Count;

            // Trigger all associated objects
            for (int i = 0; i < numTriggerIDs; ++i)
            {
                // Dictionary of ITriggerable with a TriggerID as the key
                //game.ITriggerable[triggerIDs[i]].Trigger();
            }
        }
The problem is that a ray cast uses a completely dissimilar approach to collision detection:

Code: Select all

            List<RayCastResult> results = new List<RayCastResult>();

            if (game.Space.RayCast(new Ray(origin, direction), length, results))
            {
                int index = -1;
                TriggerVolume o = null;
                float tMin = float.MaxValue;

                int numObjectsHit = results.Count;

                for (int i = 0; i < numObjectsHit; ++i)
                {
                    var result = results[i].HitObject.Tag as TriggerVolume;

                    // Find the closest TriggerVolume
                    if (result != null && results[i].HitData.T < tMin)
                    {
                        index = i;
                        o = result;
                        tMin = results[i].HitData.T;
                    }
                }

                // Going to have to create a separate method to handle a Ray :(
                //o.Events_InitialCollisionDetected(
            }
So my question is how can I create a Ray that behaves more like a Collidable, so that it can be inserted into space and can trigger a 'Events_InitialCollisionDetected' event like any other Collidable would?

I understand I could use a very fast moving Collidable instead, but I wondered what approach you would suggest?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: How do I make a RayCast more like a Collidable?

Post by Norbo »

So my question is how can I create a Ray that behaves more like a Collidable, so that it can be inserted into space and can trigger a 'Events_InitialCollisionDetected' event like any other Collidable would?
There is no good, direct way if the Ray is long. The BroadPhase was specifically designed for AABB's, and the AABB of a long Ray is exceptionally bad in many cases. Collidables, being BroadPhaseEntries, are bounded by AABB's and are not good candidates for containing Ray shapes.

Ray casting against the acceleration structure of AABB's is very fast in comparison because the Ray can be used to traverse the hierarchy efficiently, unlike the barely-bounding box that would be used to contain the Ray.

Further, InitialCollisionDetected is conventionally based on contact points, so the Ray object should generate contacts somehow. You would end up implementing custom narrow phase pair handlers. This is doable, but it would certainly add complexity.

My recommendation would be to abstract the trigger volume a little more such that it doesn't rely entirely on contact events. Then, just handle Collidables in their most natural form, and ray casting in its separate most natural form.

You could also externally ray cast and then fire your own 'collision events' based on the result.
Spankenstein
Posts: 249
Joined: Wed Nov 17, 2010 1:49 pm

Re: How do I make a RayCast more like a Collidable?

Post by Spankenstein »

Your absolutely right. I've thought about this the wrong way around. I've made changes thanks to your suggestions.
Post Reply