I am working in open gl using visual c++.I have made a quad a balls.Now i want to bounce the balls on quad repeadetly but i donot know how to do this??i thought of timerfunction to use but i donot know how to use it??which coordinates to set.here is the code

#include <windows.h>
#include <stdio.h>
#include <GL/glut.h>
#include<math.h>
//GLuint texture;
void drawEllipse(float xradius, float yradius,float h,float k,int i);
void TimerFunction(int value);
void ChangeSize(int w, int h); 
void SetUpRC();
float angle=90.0;
GLfloat rsize = 25;
int a=10;
int b=10;
//////////////////////////////////////////////////////
float pi=180;
const float DEG2RAD = 3.14159/180;
float z=0.0f;
float u=1.0f;
// Initial square position and size
GLfloat x[10];
GLfloat y[10];
//GLfloat rsize = 25;
//int a=10;
//int b=10;
// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep[10]; 
GLfloat ystep[10];
// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;
////////////////////////////////////////////////////////////////
double frame;
//void display2 (void);
/*GLuint LoadTexture( const char * filename, int width, int height ){
    GLuint texture;
    unsigned char * data;
    FILE * file;
    //The following code will read in our RAW file
    file = fopen( filename, "rb" );
    if ( file == NULL )
        return 0;
    data = (unsigned char *)malloc( width * height * 3 );    
    fread( data, width * height * 3, 1, file );    
    fclose( file );    
    glGenTextures( 1, &texture ); //generate the texture with the loaded data
    glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture to it’s array
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //set texture environment parameters
    //here we are setting what textures to use and when. The MIN filter is which quality to show   
    //when the texture is near the view, and the MAG filter is which quality to show when the texture
    //is far from the view.
    //The qualities are (in order from worst to best)
    //GL_NEAREST    
    //GL_LINEAR    
    //GL_LINEAR_MIPMAP_NEAREST
    //GL_LINEAR_MIPMAP_LINEAR
    //And if you go and use extensions, you can use Anisotropic filtering textures which are of an
    //even better quality, but this will do for now.
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR );    
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR );
    //Here we are setting the parameter to repeat the texture instead of clamping the texture
    //to the edge of our shape.     
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );    
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );    
    //Generate the texture with mipmaps 
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
    free( data ); //free the texture
    return texture; //return whether it was successfull
}
void FreeTexture( GLuint texture ){  
    glDeleteTextures( 1, &texture ); 
}*/
void cube (void) {
    glBindTexture( GL_TEXTURE_2D, texture );    
    //glColor3f(0.0,0.0,0.0);  
    //  glScalef(2,2,1);
    glBegin (GL_QUADS); 
    //glTexCoord2d(0,0);   
    glVertex3f(-2,2,0);
    // glTexCoord2d(1,0);  
    glVertex3f(2,2,0);
    //glTexCoord2d(1,1);
    glVertex3f(2,-2,0);  
    //glTexCoord2d(0,1);
    glVertex3f(-2,-2,0);  
    glEnd();
}
void display (void) {
    GLfloat aspectRatio;
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glPushMatrix(); //texture calling
    glEnable( GL_TEXTURE_2D );
    glTranslatef(2,0,-10);
    cube();
    glPopMatrix();
    //////////////////////////////////////////////////////////  
    glPushMatrix();//sets the size of bubbles
    glTranslatef(0,0,-2.5);
    aspectRatio = (GLfloat)640 / (GLfloat)480;
    windowWidth = 100;
    windowHeight = 100 / aspectRatio;
    glOrtho (-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
    //draw bubbles
    for(int i=0;i<1;i++)
    {
        //glTranslatef(i+20,0,-5);
        //glRotatef (angle, 0, 0, 1);
        //glRotatef (angle, 0, 0, 5);
        //glRotatef (angle, 0, 0, 5);
        //glRotatef (angle, 0, 0, 5);
        //glRotatef (angle, 0, 0, 0);
        drawEllipse(a+2, b+2,x[i],y[i] - rsize,i);
    }
    //sets the rendering scene
    // glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    SetUpRC();
    glPopMatrix();
    glutSwapBuffers();
}
    //angle=angle+0.02;
//}
void SetUpRC(){
    {
        int a=0,b=0;   
        // Set clear color to blue
        glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
        for(int i=0;i<1;i++){
            //xstep[i]=2.0;
            ystep[i]=1.5;    
        }
        x[0]=60;
        y[0]=50;
        x[1]=20;
        y[1]=30;
        x[2]=-25;
        y[2]=45;
        x[3]=-70;
        y[3]=70;
        x[4]=-75;
        y[4]=20;
        x[5]=-60;
        y[5]=40;
        x[6]=70;
        y[6]=45;
        x[7]=-100;
        y[7]=40;
        x[8]=-140;
        y[8]=60;
        x[9]=-170;
        y[9]=50;
    }
}
void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);  
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
}
void drawEllipse(float xradius, float yradius,float h,float k,int i){  
    glBegin(GL_POLYGON);
    for (int i1=0; i1< 360; i1++)   {
        //glColor3f(0.0f,1.0f, 0.0f);
        glColor3f(z+1.0f, 0.0f, u+1.0f);
        //convert degrees into radians
        float degInRad = i1*DEG2RAD;
        glVertex2f(cos(degInRad)*xradius+h,sin(degInRad)*yradius+k);
    }
    glBegin(GL_TRIANGLE_FAN); 
    for ( i1=0; i1 < 360; i1++)   {  
        //glColor3f(0.0f,1.0f, 0.0f);
        glColor3f(z+1.0f, 0.0f, u+1.0f);
        //convert degrees into radians
        float degInRad = i1*DEG2RAD;
        glColor3f(1.0,z+1.0,u+2.0);
        glVertex2f(cos(degInRad)*xradius+h,sin(degInRad)*yradius+k);
    }
    glEnd();
}
///////////////////////////////////////////////////////////
// Called by GLUT library when the window has chanaged size
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
void init (void) {  
    //texture[0] = LoadTexture( "grey.bmp", 1024,677 );
    //texture = LoadTexture( "Island_2.raw", 1024, 768 ); 
    //texture[2] = LoadTexture( "niagrafall.raw", 800, 600 );
    //  texture[3] = LoadTexture( "waterfall.raw", 2344, 1768 );
    //texture[4] = LoadTexture( "nadia.raw", 720,553);
}
int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE);
    glutInitWindowSize (800, 600);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    init();
    glutDisplayFunc (display);
    glutReshapeFunc (reshape);
    glutIdleFunc (display);
    :((  :((    
    glutMainLoop (); 
    return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Welcome to Daniweb! Although your question is written in c++, you may have better luck with this kind of thing in the Game Development forum:
http://www.daniweb.com/forums/forum71.html

Also, be sure to use /code at the end of your code tags so they are applied!

I'd also try to simplify your question into sub-questions. To make something "bounce", it seems to me that you need to know how to do two things.

1) make an object move
2) detect collisions between two objects

Good luck,

Dave

I'll admit that I tried re-formatting it and re-posting it, got about a quarter of the way through and gave up.

Then just attach the *.cpp files. If you are using the quick editor, Click the "Use Advanced Editor" button, scroll down and click the "Manage Attachments" button.

I'd say attaching a file is a last resort. There aren't many questions that can't be simplified into a very short, compilable version of the problem and posted reasonable with code tags.

This article has been dead for over six months. Start a new discussion instead.