What's wrong here? (code included)

This is the code for a physics project I’m wrking on right now where there are ten balls that bounce differently. But there are 2 problems:

  1. The balls’ y goes up in the hundred thousands, and
  2. The screen seemingly only refreshes when I minimize, restore, or reshape the window.

#define pio180 3.14159/180.0
#include <gl/glu.h>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

using namespace std;

struct bounce{
float speed;
float x;
float y;
float mu;

bounce ball[11];

void reshape( int width, int height ){
GLfloat w[] = { -10.0, 10.0, -10.0, 10.0 };
GLfloat aspect;

glMatrixMode( GL_PROJECTION );

aspect = (GLfloat) width / (GLfloat) height;

if ( aspect <= 1.0 ){
glOrtho( w[0], w[1], w[2] / aspect, w[3] / aspect, -1.0, 1.0 );
glOrtho( w[0] * aspect, w[1] * aspect, w[2], w[3], -1.0, 1.0 );

glMatrixMode( GL_MODELVIEW );

glViewport( 0, 0, width, height );

void display(){
int i;
float pos;
for(i = 0; i < 10; i++){
glColor3f(0, 0, .1);
glTranslatef(ball[i].x, ball[i].y, 0);
glutSolidSphere(1, 6, 6);
glVertex2f(-9, -9.5);
glVertex2f(9, -9.5);

void showwhy(){

void idle(){

int i;
for(i = 0; i < 10; i++){
ball[i].y += ball[i].speed;
ball[i].speed -= 1;
if(ball[i].y < -9){
ball[i].speed = -ball[i].speed;
ball[i].speed -= ball[i].mu;
int main(int argc, char argv){
int i;
float temp = -9;
for(i = 0; i < 10; i++){
ball[i].mu = temp;
ball[i].y = 9;
ball[i].speed = 0;
ball[i].x = temp;
temp += 2;

void glutInit(int *argcp, char **argv);
glutInitWindowSize(800, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow(“BOUNCY BALL by Andrew MacRobert”);

1: That’s because, for each bounce, the ball is leaving with larger speed than with what it arrived with. When the ball reaches the bottom, you negate the speed, which is fine, but you also add an extra value, mu, to this speed. If mu is negative, the resulting speed is greater that the original speed, baking the ball jump higher and higher.

You’re also having problems with some balls continuously falling down, despite the fact that you try to make them bounce once they go below -9. This is a result of you continuously subtracting a constant factor off the speed, making the speed negative at the wrong time, so to speak. It’s pretty much the same problem that causes the balls to bounce higher and higher, but the other way around.

2: You don’t tell GLUT to redisplay the window when the physics is updated. Stick a glutPostRedisplay in the idle callback.

And your code in general contains some errors.
[ul][]Your main signature is illegal (make argv a char **).[]You’re redeclaring the signature for glutInit.[]You’re not calling glutInit. It may seem to work anyway, but you can never be sure GLUT is functioning correct without being properly initialized (unless you look in the source).[]It’s glBegin/End(GL_LINES), not glEnable/Disable(GL_LINES).[*]You’re not clearing the frame buffer. This would be OK if you want to continuously draw to a front buffer without removing old stuff, but you’re using double buffering, drawing to the back buffer, and swapping between displays.[/ul]