/* Many spheres bouncing inside a cube with gravity, energy dissipation on impact, and impact between the spheres. TODO: DRY - up with bouncing_balls_one.cpp - balls flicker a lot. Why? */ #include #include #include #define GL_GLEXT_PROTOTYPES #include #include #include "common.hpp" static GLfloat eyex = 2.75; static GLfloat eyey = 0.75; static GLfloat eyez = 0.0; static GLdouble eye_speed = 0.1; static GLfloat centerx = 0.0; static GLfloat centery = 0.0; static GLfloat centerz = 0.0; static GLfloat upx = 0.0; static GLfloat upy = 1.0; static GLfloat upz = 0.0; static GLfloat clear_color_r = 0.0; static GLfloat clear_color_g = 0.0; static GLfloat clear_color_b = 0.0; static GLfloat fast_forward = 1; static GLfloat REST_COEF = 0.95; static Vector3D GRAVITY = Vector3D(0.0, -0.98, 0.0); int spheres_side = 2; int total_spheres = spheres_side * spheres_side * spheres_side; Sphere *spheres; // Which spheres have collided in this round.spheres that collide stop moving // in this round in this round, and get speeds that will separate them. bool *collided; static int old_t = 0; //old time in ms. used to keep real time consistent static GLfloat vs[][3] = { {-1,-1,-1}, {-1,-1,1}, {-1,1,-1}, {-1,1,1}, {1,-1,-1}, {1,-1,1}, {1,1,-1}, {1,1,1} }; void init(int argc, char** argv) { srand ( time(NULL) ); spheres = new Sphere[total_spheres]; collided = new bool[total_spheres]; int sphere_count = 0; for(int i=0; i 1.0f-SPHERE_RADIUS){ spheres[i].speed.x = -REST_COEF*spheres[i].speed.x; collided[i] = true; } else if( fabs(new_center.y) > 1.0f-SPHERE_RADIUS){ spheres[i].speed.y = -REST_COEF*spheres[i].speed.y; collided[i] = true; } else if( fabs(new_center.z) > 1.0f-SPHERE_RADIUS){ spheres[i].speed.z = -REST_COEF*spheres[i].speed.z; collided[i] = true; } else { for (int j=i+1; j