Game Development Reference
In-Depth Information
Initialization
At the beginning of the simulation, we have to initialize all four billiard balls. We use
one function, InitializeObjects , for this task. It's a long function, but it's really simple.
The code is shown on this and the following pages. Bodies[0] is the cue ball, and it is
positioned 50 ball diameters along the negative x-axis away from the object balls. There's
no magic to this number; we picked it arbitrarily. Now, we did deliberately set the z-
position of the cue ball (all the balls, for that matter) to one-half the diameter so that
the balls would be just touching the table at the start of the simulation.
To have the cue ball roll from right to left, we gave it an initial velocity of 7 m/s along
the positive x-axis. With this initial velocity, the cue ball will begin sliding across the
table for some short distance as it also starts to roll due to the friction between the ball
and table. You can see in the upcoming code sample that all the other kinematic prop‐
erties are set to 0 for the cue ball. For the object balls, all of their kinematic properties
are set to 0.
We encourage you to experiment with different initial values for the cue ball's kinematic
properties. For example, try setting the angular velocity about any of the coordinate
axes to something other than 0. Doing so will allow you to see how a spinning ball may
move slightly left or right depending on the spin. It's also fun to see how spin affects the
object balls upon collision with the cue ball.
Aside from setting the positions and kinematic properties of the balls, InitializeOb
jects also initializes mass properties. We used the previously defined BALLWEIGHT
divided by the acceleration due to gravity to determine the ball mass. For mass moment
of inertia, we simply used the equations for a solid sphere we showed you way back in
Chapter 1 :
void InitializeObjects(int configuration)
{
float iRoll, iPitch, iYaw;
int i;
float Ixx, Iyy, Izz;
float s;
/////////////////////////////////////////////////////
// Initialize the cue ball:
// Set initial position
Bodies[0].vPosition.x = -BALLDIAMETER*50.0f;
Bodies[0].vPosition.y = 0.0f;
Bodies[0].vPosition.z = BALLDIAMETER/2.0f;
// Set initial velocity
s = 7.0;
Bodies[0].vVelocity.x = s;
Bodies[0].vVelocity.y = 0.0f;
Bodies[0].vVelocity.z = 0.0f;
Bodies[0].fSpeed = s;

Custom Search