Wednesday, May 18, 2011

Raycasting and Collision Model Querying

Over the last few days I've added ray casting and querying to the AbstractCollisionModel class.  Ray casting is the method of tracing a line through a collision model and determining what objects the ray intersects.  Querying is a way to test the collision model for objects which satisfy a certain condition.  Ray casting can be used for quickly determining if something, like a bullet, would impact an object.

Using Bresenham's line algorithm, I calculate all of the points along the ray and then test the objects within the model against those points.  If an object is determined to be colliding with the point, another test is performed.  The test is supplied to the ray casting method, but can also be excluded.  Excluding the test function will return a CollisionData structure for the first object that the ray collides with.  If the test is provided, only the first object which satisfies the test will be returned.  Otherwise the method returns null.

Querying the collision model is a way to look at all of the objects in the model and determine if they satisfy a certain condition.  Objects which satisfy the condition will be returned in an array.  This general method is used by the more specific method "queryNear" which will look for objects within a specified distance of a point.  Neither of these querying methods is particularly fast because it requires testing the entire set of objects in the model.

Further improvement can be made to ray casting when looking at the method from the perspective of the SpatialGrid.  In the future I hope to implement some of these improvements because it will result in extremely fast testing using ray casting.  Overall the current ray casting method isn't slow when there aren't a lot of objects to test against as only nodes which fall along the ray have their objects tested.

No comments:

Post a Comment