Friday, February 15, 2013

Raycasting Group Collidables

This week I was able to implement and finish a system that BEPU supports called GroupCollisions.  I made different groups like buildings, enemies, characters, etc. that kept track of its collidable objects which would make it easier and more efficient to compute collisions between objects.  Before, our game frame-rates were being killed by looking through each collidable and having if statements that checked between collidables.  Now, we don't have to have a bunch of code that checks these flags and now we can check items by raycasting to specific groups or ignore specific groups.  I spent all of my time this week cleaning the code this week and making static methods that will make it easier on programmers to implement checks.

Here is my interesting piece of code.

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

return physicsSystem.RayCast(ray, dist, entry => entry.CollisionRules.Group == Game1.Instance.BuildingCollisionGroup, out result);

As you see this interesting piece of code, "entry => entry.CollisionRules.Group == Game1.Instance.BuildingCollisionGroup", you will notice that it is doing everything we need to do in one line of code.  This is called lambda inline statements.  Before, we had to call an entire function that would take care of a bunch of collision checks before returning the result.  Now, we only have to call a simple group test  which uses a binary tree to speed up the process as well as reading ability.  I then made simple method calls in a RayCastManager which is a static class where programmers don't have to worry about learning lambda calls and can easily call RayCastCollideWithGroup(Buildings) instead.

No comments:

Post a Comment