#include #include "timer.h" #define NUMBALLS 18 #define WHITEBALL 0 #define BALLRADIUS 5 #define TABLEWIDTH 100.0f #define TABLELENGTH 200.0f #define TABLEBOUNCE 0.8f #define TABLEDRAG 0.995f #define MINSPEED 0.05f typedef struct {float x, y, z, w;} Vector_t; short NumberOfBalls = 0; short NumberOfMovingBalls = 0; Vector_t BallPositions [NUMBALLS]; Vector_t BallVelocities [NUMBALLS]; short MoveballsReps = 16; int framecounter = 0; void SetupBalls (void) { int i, x, y, w; BallPositions[WHITEBALL].x = 0; BallPositions[WHITEBALL].y = 0; BallPositions[WHITEBALL].z = -BALLRADIUS*10; x = 0; y = 0; w = 1; for (i=1; i= w) { x = 0; w++; y++; } } for (i=0; i= TABLEWIDTH/2-BALLRADIUS) { Velocity.x = -Velocity.x * TABLEBOUNCE; Position.x = TABLEWIDTH/2-BALLRADIUS; } if (Position.x <= -TABLEWIDTH/2+BALLRADIUS) { Velocity.x = -Velocity.x * TABLEBOUNCE; Position.x = -TABLEWIDTH/2+BALLRADIUS; } if (Position.z >= TABLELENGTH/2-BALLRADIUS) { Velocity.z = -Velocity.z * TABLEBOUNCE; Position.z = TABLELENGTH/2-BALLRADIUS; } if (Position.z <= -TABLELENGTH/2+BALLRADIUS) { Velocity.z = -Velocity.z * TABLEBOUNCE; Position.z = -TABLELENGTH/2+BALLRADIUS; } for (i=0; i MINSPEED || Velocity.y < -MINSPEED || Velocity.y > MINSPEED || Velocity.z < -MINSPEED || Velocity.z > MINSPEED) { NumberOfMovingBalls++; } else { /* stopped */ Velocity.x = 0; Velocity.y = 0; Velocity.z = 0; } } #undef Position #undef Velocity #undef NumberOfBalls } void MoveBalls ( ) { framecounter++; NumberOfBalls = 0; NumberOfMovingBalls = 0; MoveBall ( ); if ( NumberOfMovingBalls == 0 ) { /* If no balls are still moving, then hit the cue-ball again */ BallVelocities[WHITEBALL].x = 10.0f * sin (framecounter); BallVelocities[WHITEBALL].z = 10.0f * cos (framecounter); } } int main () { unsigned a; SetupBalls ( ); START_TIMER(); for ( a = 0; a < 2000; a++ ) MoveBalls ( ); STOP_TIMER ( ); return 0; }