Problems keeping all polygons in viewport!!

I’m creating a 2d game in which there are 24 things (squares) bouncing around inside window off of the sides. I was able to get 1 out of the 4 to stay in bounds but the other 3 only check the left side and the bottom…what am I doing wrong??
If I can’t get four things to stay in bounds, how can I do 24???

Here’s what I have:

#include <stdio.h>
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <time.h>
#include <stdlib.h>

const int NUM_OF_THINGS = 4;

void RenderScene(void);
void TimerFunction(int);
void myInit(void);
void ChangeSize(GLsizei, GLsizei);
void myKey(unsigned char, int, int);
void createThing();

GLfloat createThings[4];
GLuint Square_DL;

//Array of clolors
GLfloat RED[3] = {1,0,0};
GLfloat YELLOW[3] = {1,1,0};
GLfloat PURPLE[3] = {1,0,1};
GLfloat BLACK[3] = {0,0,0};

// Initial square position and size
GLfloat xpos = 100.0f; //or 100.0f
GLfloat ypos = 150.0f; //or 150.0f
GLsizei rsize = 50;

// size of square
GLfloat x1 = 0.1;
GLfloat y1 = 0.1;

// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 1.0f; //speed or .25
GLfloat ystep = 1.0f;

// Keep track of windows changing width and height
GLfloat windowWidth = 400;
GLfloat windowHeight = 400;

void createThing ()

int xSize = .1;
int ySize = .1;

// Draw a filled rectangle with current color
glRectf(xSize, ySize, xSize+rsize, ySize+rsize);

// Called to draw scene
void RenderScene()
// Clear the window with current clearing color

for (int i = 0; i &lt; NUM_OF_THINGS; i++) 
    glTranslatef(xpos, ypos, 0.0);

// Flush drawing commands


void myKey(unsigned char key, int x, int y)
if ((key == ‘Q’) || (key == ‘q’)) {

// Called by GLUT library when idle (window not being
// resized or moved)
void TimerFunction(int value)
// Reverse direction when you reach left or right edge
if(xpos > windowWidth-rsize || xpos < 0)
xstep = -xstep;

// Reverse direction when you reach top or bottom edge
if(ypos &gt; windowHeight-rsize || ypos &lt; 0)
    ystep = -ystep;

// Check bounds.  This is incase the window is made
// smaller and the rectangle is outside the new
// clipping volume
if(xpos &gt; windowWidth-rsize)
    xpos = windowWidth-rsize-1;

if(ypos &gt; windowHeight-rsize)
    ypos = windowHeight-rsize-1;

// Actually move the square
xpos += xstep;
ypos += ystep;

// Redraw the scene with new coordinates
glutTimerFunc(10,TimerFunction, 0);


// Setup the rendering state
void myInit(void)
// Set clear color to black
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

// Called by GLUT library when the window has chanaged size
void ChangeSize(GLsizei w, GLsizei h)
// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset coordinate system

// Keep the square square, this time, save calculated
// width and height for later use
if (w &lt;= h) {
    windowHeight = 400.0f*h/w;
    windowWidth = 400.0f;
else {
    windowWidth = 400.0f*w/h;
    windowHeight = 400.0f;

// Set the clipping volume
gluOrtho2D(0.0f, windowWidth, 0.0f, windowHeight);



// Main program entry point
int main(int argc, char** argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInit(&argc, argv);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow(“Bounce Beeatch”);
glutTimerFunc(10, TimerFunction, 0);

return 0;


If I can’t get four things to stay in bounds, how can I do 24???

Let me turn that around and ask: if you can handle one object correctly, why can you not handle four? I haven’t read your code (please use [ code][ /code] without space after ‘[’ around code snippets), but the calculations should be the same for all squares, no? So the question is what makes the one that works different from the other three? Is there some piece of code that only happens once? Do you modify some OpenGL state (especially modelview or projection matrices) after processing the first square?

And after taking a quick glance at your code, why are there not four sets of variables to track the position of each of the four objects? I only see xpos, ypos, how do you even know where the other 3 objects need to be drawn if you only have variables for one?

Thanks for help. I’m sorry I’m a newbie and what you said makes sense! I’ll give it another try