You are on page 1of 3

27/12/13

Collision Detection - RB Whitaker's Wiki

RB Whitaker's Wiki
A Game Development Launchpad
Home Tutorials Forum Software Resources News Help

Create account Search this site

My Books

Collision Detection
Introduction

Collision detection is often a big deal in games. It is important to know when objects in a game collide. In this tutorial, we will cover the basics to get you going with collision detection. We will look at some of the techniques that are used for collision detection, and even do little of our own.

Collision Detection Methods

There are a lot of ways that collision detection can be done. The most obvious method is to take every single vertex in a model and chec see if any of them are inside of another model. This method, however, is very time consuming to do, especially if you have lots of model models with a lot of detail in them. To deal with this problem, game programmers will use an approximation of the model that is easier check for collisions. The two methods that are most commonly used are bounding boxes and bounding spheres. With these methods, yo would basically build a box or sphere around a model that completely covers the model. There will obviously be area that is outside of t model, but still inside of the bounding box or sphere, but an ideal bounding region will limit this as much as possible.

My Software

We will focus on bounding spheres in this tutorial, because of their simplicity, and because XNA has a lot of built in support for boundin spheres. The basic idea is that for each model you are using, or for each mesh in a model, you will construct a sphere from it. You deter the middle point of the mesh, which will become the center point for your bounding sphere. You then figure out the farthest vertex in t model or mesh from this centerpoint, and the distance to it is the radius of the bounding sphere.

You can do all of this work outside of the game, so there really isn't a time penalty during the game. During the game, you get the bound spheres for the models that you are checking, and see if the distance between their centerpoints is less than the radii of the two bound spheres. If it is, then there is a collision. We will see, in a minute, that XNA really takes care of most of this for us, and simple collision detection is pretty easy to do.

One drawback to this method is that a sphere (or even a bounding box) may not be a good approximation for an object. For instance, imagine you have a long object, like the arrow in the image below. A bounding sphere might be a very bad approximation, because it ne to be large enough to cover the entire object, but this allows a lot of extra space inside of the bounding sphere that is not actually part o arrow.

One commonly used solution to this problem is to approximate the object with multiple spheres, rather than one. This allows for a bette approximation of the object, as shown below.

The drawback to this method is that now you are comparing with many bounding spheres, which may be inefficient if the other object is away from the arrow.

A step beyond this is to have a hierarchical model. In this case, you would use both the large sphere, and the small spheres. When chec to see if the arrow has collided with another object, you would first compare with the big bounding sphere. If this is not a collision, you that there is no collision, and you can continue with the game. However, if it is a collision, then you go ahead and compare with the mor detailed spheres. This way, you only have to do this extra work when it is needed. If the object lies within these more detailed spheres, it is recognized as a collision. In fact, you can have as many levels as you want, which may come in handy in a very intricate model. Anot improvement to this method is to use multiple types of approximation techniques together. For instance, you can combine the boundin spheres with bounding boxes. For some objects, bounding spheres are a better approximation of the object. For others, bounding boxes A system that allows either will be more accurate than one that strictly uses one or the other. While a more advanced system like this is ideal, for now we are simply going to work with the basic bounding sphere method.

Collision Detection with Bounding Spheres

The code to actually do collision detection is fairly simple, and it is easy to add to any game that you are already working on. I've include some code below in case you don't have a place to already put it. This is the code that I will be using in this tutorial, as well. It relies on that we did in the using 3D models tutorial, as well as the simple 3D animation tutorial, and to a lesser degree, the BasicEffect lighting tutorial. If you haven't gone through those tutorials, it might be helpful to go through them now. You should be able to copy and paste t code into a new game project. Additionally, this code uses the SimpleShip model from the 3D Model Library, which you can download an include in your project like we have done before. Add both the .fbx file and the texture file to your project, and then exclude the texture your project so that it doesn't get handled twice by the content pipeline. You can use a model of your own instead, too, if you want.

rbwhitaker.wikidot.com/collision-detection

1/3

27/12/13

Collision Detection - RB Whitaker's Wiki


u s i n gS y s t e m ; u s i n gS y s t e m . C o l l e c t i o n s . G e n e r i c ; u s i n gS y s t e m . L i n q ; u s i n gM i c r o s o f t . X n a . F r a m e w o r k ; u s i n gM i c r o s o f t . X n a . F r a m e w o r k . A u d i o ; u s i n gM i c r o s o f t . X n a . F r a m e w o r k . C o n t e n t ; u s i n gM i c r o s o f t . X n a . F r a m e w o r k . G a m e r S e r v i c e s ; u s i n gM i c r o s o f t . X n a . F r a m e w o r k . G r a p h i c s ; u s i n gM i c r o s o f t . X n a . F r a m e w o r k . I n p u t ; u s i n gM i c r o s o f t . X n a . F r a m e w o r k . M e d i a ; u s i n gM i c r o s o f t . X n a . F r a m e w o r k . N e t ; u s i n gM i c r o s o f t . X n a . F r a m e w o r k . S t o r a g e ; n a m e s p a c eC o l l i s i o n D e t e c t i o n { p u b l i cc l a s sG a m e 1:M i c r o s o f t . X n a . F r a m e w o r k . G a m e { G r a p h i c s D e v i c e M a n a g e rg r a p h i c s ; S p r i t e B a t c hs p r i t e B a t c h ; M o d e lm o d e l ; V e c t o r 3s h i p 1 L o c a t i o n=n e wV e c t o r 3 ( 0 ,2 0 ,0 ) ; V e c t o r 3s h i p 2 L o c a t i o n=n e wV e c t o r 3 ( 0 ,0 ,0 ) ; M a t r i xv i e w=M a t r i x . C r e a t e L o o k A t ( n e wV e c t o r 3 ( 1 0 ,1 0 ,1 0 ) ,n e wV e c t o r 3 ( 0 ,0 ,0 ) ,V e c t o r 3 . U n i t Z ) ; M a t r i xp r o j e c t i o n=M a t r i x . C r e a t e P e r s p e c t i v e F i e l d O f V i e w ( M a t h H e l p e r . T o R a d i a n s ( 4 5 ) ,8 0 0 f/6 0 0 f ,0 . 1 f ,1 0 0 f ) ; p u b l i cG a m e 1 ( ) { g r a p h i c s=n e wG r a p h i c s D e v i c e M a n a g e r ( t h i s ) ; C o n t e n t . R o o t D i r e c t o r y=" C o n t e n t " ; } p r o t e c t e do v e r r i d ev o i dI n i t i a l i z e ( ) { b a s e . I n i t i a l i z e ( ) ; } p r o t e c t e do v e r r i d ev o i dL o a d C o n t e n t ( ) { s p r i t e B a t c h=n e wS p r i t e B a t c h ( G r a p h i c s D e v i c e ) ; m o d e l=C o n t e n t . L o a d < M o d e l > ( " S h i p " ) ; } p r o t e c t e do v e r r i d ev o i dU n l o a d C o n t e n t ( ) { } p r o t e c t e do v e r r i d ev o i dU p d a t e ( G a m e T i m eg a m e T i m e ) { / /A l l o w st h eg a m et oe x i t i f( G a m e P a d . G e t S t a t e ( P l a y e r I n d e x . O n e ) . B u t t o n s . B a c k= =B u t t o n S t a t e . P r e s s e d ) t h i s . E x i t ( ) ; s h i p 1 L o c a t i o n+ =n e wV e c t o r 3 ( 0 ,0 . 1 f ,0 ) ; s h i p 2 L o c a t i o n+ =n e wV e c t o r 3 ( 0 ,0 . 0 0 3 f ,0 ) ; b a s e . U p d a t e ( g a m e T i m e ) ; } p r o t e c t e do v e r r i d ev o i dD r a w ( G a m e T i m eg a m e T i m e ) { G r a p h i c s D e v i c e . C l e a r ( C o l o r . B l a c k ) ; M a t r i xs h i p 1 W o r l d M a t r i x=M a t r i x . C r e a t e T r a n s l a t i o n ( s h i p 1 L o c a t i o n ) ; M a t r i xs h i p 2 W o r l d M a t r i x=M a t r i x . C r e a t e T r a n s l a t i o n ( s h i p 2 L o c a t i o n ) ; D r a w M o d e l ( m o d e l ,s h i p 1 W o r l d M a t r i x ,v i e w ,p r o j e c t i o n ) ; D r a w M o d e l ( m o d e l ,s h i p 2 W o r l d M a t r i x ,v i e w ,p r o j e c t i o n ) ; b a s e . D r a w ( g a m e T i m e ) ; } p r i v a t ev o i dD r a w M o d e l ( M o d e lm o d e l ,M a t r i xw o r l d ,M a t r i xv i e w ,M a t r i xp r o j e c t i o n ) { f o r e a c h( M o d e l M e s hm e s hi nm o d e l . M e s h e s ) { f o r e a c h( B a s i c E f f e c te f f e c ti nm e s h . E f f e c t s ) { e f f e c t . E n a b l e D e f a u l t L i g h t i n g ( ) ; e f f e c t . W o r l d=w o r l d ; e f f e c t . V i e w=v i e w ; e f f e c t . P r o j e c t i o n=p r o j e c t i o n ; } m e s h . D r a w ( ) ; } } } }

When you run this code, you should see something similar to the image below, where two ships are moving along, one faster than the o When the fast one catches up to the slow one, it will pass through it and come out in front of it. A screenshot of the program running is below:

rbwhitaker.wikidot.com/collision-detection

2/3

27/12/13

Collision Detection - RB Whitaker's Wiki

The actual code for collision detection is fairly simple. We first need the two models that represent the objects that we are working with These will include BoundingSphere objects for each of the meshes in the model. In addition, we will need to know how the model has bee transformed in the world, since the bounding spheres of the model are in model coordinates. That is, if the object in the game is locate units down the x-axis, we will need to move our bounding sphere from the model 20 units down the x-axis so that it is in the right spot. the method that we create for collision detection will require two model objects and two Matrix objects for the transformations. We will compare each of the bounding spheres in one model to the bounding spheres in the other. If any overlap, then we have a collision. If w through them all without any overlaps, then there is no collision. So here is the code to do this:
p r i v a t eb o o lI s C o l l i s i o n ( M o d e lm o d e l 1 ,M a t r i xw o r l d 1 ,M o d e lm o d e l 2 ,M a t r i xw o r l d 2 ) { f o r( i n tm e s h I n d e x 1=0 ;m e s h I n d e x 1<m o d e l 1 . M e s h e s . C o u n t ;m e s h I n d e x 1 + + ) { B o u n d i n g S p h e r es p h e r e 1=m o d e l 1 . M e s h e s [ m e s h I n d e x 1 ] . B o u n d i n g S p h e r e ; s p h e r e 1=s p h e r e 1 . T r a n s f o r m ( w o r l d 1 ) ; f o r( i n tm e s h I n d e x 2=0 ;m e s h I n d e x 2<m o d e l 2 . M e s h e s . C o u n t ;m e s h I n d e x 2 + + ) { B o u n d i n g S p h e r es p h e r e 2=m o d e l 2 . M e s h e s [ m e s h I n d e x 2 ] . B o u n d i n g S p h e r e ; s p h e r e 2=s p h e r e 2 . T r a n s f o r m ( w o r l d 2 ) ; i f( s p h e r e 1 . I n t e r s e c t s ( s p h e r e 2 ) ) r e t u r nt r u e ; } } r e t u r nf a l s e ; }

In the Update() method, we will want to add some code to check for collisions between objects. In the original code above, there are only ships in the scene that we are checking. If you have more objects, you will need to check each pair of objects. Add the following code to Update() method, which will call the collision detection method, and if a collision is found, restart the fast ship at its beginning location. way you can tell if the collision detection is working.
M a t r i xs h i p 1 W o r l d M a t r i x=M a t r i x . C r e a t e T r a n s l a t i o n ( s h i p 1 L o c a t i o n ) ; M a t r i xs h i p 2 W o r l d M a t r i x=M a t r i x . C r e a t e T r a n s l a t i o n ( s h i p 2 L o c a t i o n ) ; i f( I s C o l l i s i o n ( m o d e l ,s h i p 1 W o r l d M a t r i x ,m o d e l ,s h i p 2 W o r l d M a t r i x ) ) { s h i p 1 L o c a t i o n=n e wV e c t o r 3 ( 0 ,2 0 ,0 ) ; }

Once this is done, you should be able to run the game again, and see that when the ships touch each other, the fast ship restarts back where it did originally, which means your collision detection is working correctly! Below is the completed code for the collision detection. view source code Click here for the completed project: CollisionDetection.zip

What's Next?

Now that you've done this simple collision detection, you might want to try out some of the more advanced collision detection methods, bounding boxes and hierarchical models. As of right now, I don't have any other game physics tutorials, but you might want to go back a Feedback check out some of the other more advanced tutorials.

Having problems with this tutorial? Try the troubleshooting page! Powered by Wikidot.com

Help | Terms of Service | Privacy | Report a bug | Flag as obje

rbwhitaker.wikidot.com/collision-detection

3/3

You might also like