Advice sought on fastest method for raycasting below objects

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Alic
Posts: 39
Joined: Fri Jul 29, 2011 2:25 pm

Advice sought on fastest method for raycasting below objects

Post by Alic »

Hey Norbo, I just wanted to ask your advice on the relative speeds of these two possible methods of getting this job done.


I'm working on a graphically 2D legend of zelda style game, but with jumping and movable objects. Every creature and moving object has a shadow which is cast directly below it onto the ground. These shadows need to know when there is an object below their owner which they should be cast onto, instead of being drawn at ground height. In order to do this my simplest solution was to raycast down from the center of each object every frame, and check for which collision was the highest. However, this method makes use of the Space.Raycast method, the one which returns a whole list of results, which I'm afraid might test against every object in the space. Is this true, or does BEPU's spatial partitioning make this not such a big deal?

The other method (I can think of) would be to use a stripped down version of your character controller's support system, with a bounding box collecting possible supports below the object, and then a raycast being tested against only the support candidates gathered by the bounding box. The only issue here is that the bounding box would have to be quite tall, tall enough that something very high in the air could still detect and cast a shadow onto the ground.

Is the second one clearly better? It's a bit more work, but you must have created the collector box on the character controller for a reason, and if that reason is to eliminate unnecessary raycasts, I should probably steal the idea for something which is going to be done by every moving physics object in my game.

Thanks for reading Norbo,

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

Re: Advice sought on fastest method for raycasting below obj

Post by Norbo »

Space.RayCast uses the acceleration structure provided by the broad phase to make ray casts speedy. The time it takes to find which objects to test can take a matter of nanoseconds for a short ray. For longer rays against an acceleration structure containing many thousands of objects, this broad phase query to collect ray cast targets can take a handful of microseconds.

In other words, there's not much to be gained in attempting to pre-prune ray cast targets with something beyond the broad phase. The older character controllers which have a separate entity solely for collecting query targets no longer reflects best practices for performance. The newer CharacterController and SphereCharacterController still use a form of pre-pruning, but they only use the main body's bounding box to do this, not an extra 'foot' collector. Additionally, the modern character controllers focus almost entirely on analyzing contact points to find supports; the usage of its bounding box to bypass a broad phase query isn't very important for performance.
Alic
Posts: 39
Joined: Fri Jul 29, 2011 2:25 pm

Re: Advice sought on fastest method for raycasting below obj

Post by Alic »

Brilliant! I thought maybe I was overthinking it. Thanks for once again doing my thinking for me, Norbo! :)
Post Reply