Выбрать главу

TVector pb1, pb2, xaxis, U1x, U1y, U2x, U2y, V1x, V1y, V2x, V2y;

double a,b;

pb1 = OldPos[BallColNr1]+ArrayVel[BallColNr1]*BallTime; // Find Position Of Ball1

pb2 = OldPos[BallColNr2]+ArrayVel[BallColNr2]*BallTime; // Find Position Of Ball2

xaxis=(pb2-pb1).unit(); // Find X-Axis

a=xaxis.dot(ArrayVel[BallColNr1]); // Find Projection

U1x=xaxis*a; // Find Projected Vectors

U1y=ArrayVel[BallColNr1]-U1x;

xaxis=(pb1-pb2).unit(); // Do The Same As Above

b=xaxis.dot(ArrayVel[BallColNr2]); // To Find Projection

U2x=xaxis*b; // Vectors For The Other Ball

U2y=ArrayVel[BallColNr2]-U2x;

V1x=(U1x+U2x-(U1x-U2x))*0.5; // Now Find New Velocities

V2x=(U1x+U2x-(U2x-U1x))*0.5;

V1y=U1y;

V2y=U2y;

for (j=0;j<NrOfBalls;j++) // Update All Ball Positions

 ArrayPos[j]=OldPos[j]+ArrayVel[j]*BallTime;

ArrayVel[BallColNr1]=V1x+V1y; // Set New Velocity Vectors

ArrayVel[BallColNr2]=V2x+V2y; // To The Colliding Balls

Moving Under Gravity Using Euler Equations

To simulate realistic movement with collisions, determining the the collision point and computing the response is not enough. Movement based upon physical laws also has to be simulated.

The most widely used method for doing this is using Euler equations. As indicated all the computations are going to be performed using time steps. This means that the whole simulation is advanced in certain time steps during which all the movement, collision and response tests are performed. As an example we can advanced a simulation 2 sec. on each frame. Based on Euler equations, the velocity and position at each new time step is computed as follows:

Velocity_New = Velovity_Old + Acceleration*TimeStep

Position_New = Position_Old + Velocity_New*TimeStep

Now the objects are moved and tested angainst collision using this new velocity. The Acceleration for each object is determined by accumulating the forces which are acted upon it and divide by its mass according to this equation:

Force = mass * acceleration

A lot of physics formulas :)

But in our case the only force the objects get is the gravity, which can be represented right away as a vector indicating acceleration. In our case something negative in the Y direction like (0,-0.5,0). This means that at the beginning of each time step, we calculate the new velocity of each sphere and move them testing for collisions. If a collision occurs during a time step (say after 0.5 sec with a time step equal to 1 sec.) we advance the object to this position, compute the reflection (new velocity vector) and move the object for the remaining time (which is 0.5 in our example) testing again for collisions during this time. This procedure gets repeated until the time step is completed.

When multiple moving objects are present, each moving object is tested with the static geometry for intersections and the nearest intersection is recorded. Then the intersection test is performed for collisions among moving objects, where each object is tested with everyone else. The returned intersection is compared with the intersection returned by the static objects and the closest one is taken. The whole simulation is updated to that point, (i.e. if the closest intersection would be after 0.5 sec. we would move all the objects for 0.5 seconds), the reflection vector is calculated for the colliding object and the loop is run again for the remaining time.

3) 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) also does 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 Writes

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 Account 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.

4) Explaining the Code

Congratulations…

If you are still with me you have survived successfully the theory section ;) Before having fun playing around with the demo, some further explanations about the source code are necessary. The main flow and steps of the simulation are as follows (in pseudo code):

While (Timestep!=0) {

 For each ball {

  compute nearest collision with planes;

  compute nearest collision with cylinders;

  Save and replace if it the nearest intersection;

  in time computed until now;

 }

 Check for collision among moving balls;

 Save and replace if it the nearest intersection;

 in time computed until now;

 If (Collision occurred) {

  Move All Balls for time equal to collision time;

  (We already have computed the point, normal and collision time.)

  Compute Response;

  Timestep-=CollisonTime;

 } else Move All Balls for time equal to Timestep

}

The actual code which implements the above pseudo code is much harder to read but essentially is an exact implementation of the pseudo code above.