Game Development Reference
In-Depth Information
We're calling the new hovercraft, very creatively, Craft2 . In the Initialize function,
we must now add the following code:
bool Initialize(void)
{
.
.
.
Craft2.vPosition.x = _WINWIDTH/2;
Craft2.vPosition.y = _WINHEIGHT/2;
Craft2.fOrientation = 90;
.
.
.
}
This new code sample positions the second hovercraft in the middle of the screen and
pointing toward the bottom.
There are a few required changes to UpdateSimulation as well. First, add Craft2.Up
dateBodyEuler(dt); right after the line Craft.UpdateBodyEuler(dt); . Then, add
DrawCraft(Craft2, RGB(200, 200, 0)); after the similar line that draws the first
Craft . Craft2 will be drawn yellow to distinguish it from the first Craft . Finally, add
the following lies at the end of UpdateSimulation :
if(Craft2.vPosition.x > _WINWIDTH) Craft2.vPosition.x = 0;
if(Craft2.vPosition.x < 0) Craft2.vPosition.x = _WINWIDTH;
if(Craft2.vPosition.y > _WINHEIGHT) Craft2.vPosition.y = 0;
if(Craft2.vPosition.y < 0) Craft2.vPosition.y = _WINHEIGHT;
Now, we can add the code to handle collision detection and response, allowing you to
ram your hovercraft into the new one we just added.
Linear Collision Response
In this section, we'll show you how to implement simple collision response, assuming
that the two hovercraft are particles. We're going to implement only bare-minimum
collision detection in this simulation; however, regardless of the level of sophistication
of your collision detection routines, there are very specific pieces of information that
you must collect from your collision detection routine(s) in order for your physics-
based collision response routines to work.
To revise the hovercraft example of the previous chapter to include simple collision
response, you'll have to modify the UpdateSimulation function and add a couple more
functions: CheckForCollision and ApplyImpulse .
Before showing you CheckForCollision , we want to explain what your collision de‐
tection function must do. First, it must let you know whether or not there is a collision
occurring between the hovercraft. Secondly, it must let you know if the hovercraft are
Search Nedrilad ::




Custom Search