52873.fb2 Collision detection tutorial - читать онлайн бесплатно полную версию книги . Страница 4

Collision detection tutorial - читать онлайн бесплатно полную версию книги . Страница 4

Special Effects

Explosions

Every time a collision takes place an explosion is triggered at the collision point. A nice way to model explosions is to alpha blend two polygons which are perpendicular to each other and have as the center the point of interest (here intersection point). The polygons are scaled and disappear over time. The disappearing is done by changing the alpha values of the vertices from 1 to 0, over time. Because a lot of alpha blended polygons can cause problems and overlap each other (as it is stated in the Red Book in the chapter about transparency and blending) because of the Z buffer, we borrow a technique used in particle rendering. To be correct we had to sort the polygons from back to front according to their eye point distance, but disabling the Depth buffer writes (not reads) does also the trick (this is also documented in the red book). Notice that we limit our number of explosions to maximum 20 per frame, if additional explosions occur and the buffer is full, the explosion is discarded. The source which updates and renders the explosions is

//render/blend explosions

glEnable(GL_BLEND); //enable blending

glDepthMask(GL_FALSE); //disable depth buffer writtes

glBindTexture(GL_TEXTURE_2D, texture[1]); //upload texture

for(i = 0; i < 20; i++) //update and render explosions

{

 if (ExplosionArray[i]._Alpha >= 0) {

  glPushMatrix();

  ExplosionArray[i]._Alpha -= 0.01f; //update alpha

  ExplosionArray[i]._Scale += 0.03f; //update scale

  //assign vertices colour yellow with alpha

  // colour tracks ambient and diffuse

  glColor4f(1, 1, 0, ExplosionArray[i]._Alpha);

  //scale

  glScalef(ExplosionArray[i]._Scale, ExplosionArray[i]._Scale, ExplosionArray[i]._Scale);

  // translate into position taking into acout the offset caused by the scale

  glTranslatef((float)ExplosionArray[i]._Position.X() / ExplosionArray[i]._Scale, (float)ExplosionArray[i]._Position.Y() / ExplosionArray[i]._Scale, (float)ExplosionArray[i]._Position.Z() / ExplosionArray[i]._Scale);

  glCallList(dlist); // call display list glPopMatrix();

 }

}

Sound

For the sound the windows multimedia function  PlaySound is used. This is a quick and dirty way to play wav files quickly and without trouble.