VB:Tutorials:Building A Physics Engine:Collision Detection

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Tutorial by Nicholas Gorski [GMan]

This will extend the previous example to include moving objects.

Intersection Detection versus Collision Detection

In the previous article, we learned how to test for intersecting polygons. The engine COULD be used for collision detection, but it would be horribly inaccurate. Why is that so? After all, we just have to see if they are intersecting and react to that.

Intersections are static. It is as if you took a snapshot and compare polygons. You have no knowledge of whether the objects are moving fast or slow, or in any direction, or if they are even moving at all. Collision detection takes into account the motion of the polygons. Take a look at the following pictures:

With Intersection Detection: Files:VB_PhysEng_Intersect.GIF

Here, we see two boxes. Because this is intersection detection, we do not know if they are moving, or where they are going, or even how fast. From here, we cannot tell if they will collide within the second. So we conclude they are not hitting.

With Collision Detection: Files:VB_PhysEng_Collide.GIF

Here we can see the same picture. This is what a collision detection routine sees, however. Note how it takes into consideration the direction and speed of the boxes. Now we know the red box is moving fast enough to hit the blue block.

Adding Time

Motion could not exist without time. For an object to move, there has to be time for it to move. So before moving onto the collision detection, we need a way of implementing time. There are many ways of implenting a Timer Class, as explained in the following articles:

I have chosen to use QueryPerformanceCounter. I think the trade-off between accuracy and precision is good enough.

The class will return the time passed in seconds, which is useful because units for time in physics are seconds, so no adjustments are needed.

Extending for Collision Detection

In the previous lesson, we checked