Clint
Clint

Reputation: 175

z_axis movement of individual 3d cubes

I have created a function to move several cubes along the z-axis. I can get all the cubes to move at once and repeat but I am trying to get each individual cube to move independently of the other. My z-axis function is:

void moveCubes()
{
    cubeZ += 0.050f;
        if(cubeZ > 120)
    cubeZ -= 110.0f;
    glutPostRedisplay();
}

and the display function is:

void myDisplay()
{

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
for(int i = 0; i < maxCubes; i++)
{
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -110);
    glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeZ);
    glRotatef(rotateAxis, cubeOrigins[i].rotateX, cubeOrigins[i].rotateY, 0.0f);
    drawCubes();
}

moveCubes();
glutSwapBuffers();

}

cubeZ is defined at the top of my code and is:

GLfloat cubeZ = 0.0;

cubeOrigins[i].x and .y are generated rands(). I can add more code if needed but I believe this is the important part. I am hoping someone can show me what I need to do. Frankly it is getting pretty frustrating. I appreciate any help.

Upvotes: 0

Views: 43

Answers (1)

paddy
paddy

Reputation: 63481

You've hard-coded a constant Z-position for all cubes. You probably want to use cubeOrigins[i].z instead. If you don't have such a value in your struct, perhaps you should (and randomize that the same as your x- and y-values).

When you update:

void moveCubes() {
    for( int i = 0; i < maxCubes; i++ ) {
        cubeOrigins[i].z += 0.05f;
        if( cubeOrigins[i].z > 120 )
            cubeOrigins[i].z -= 110.0f;
    }
    glutPostRedisplay();
}

And of course:

glTranslatef(cubeOrigins[i].x, cubeOrigins[i].y, cubeOrigins[i].z);

If you want to get techy, you may also want to randomize the speed of the cubes. So you can create an array of floats to represent each cube's speed and use that instead of the constant 0.05f speed.

Upvotes: 2

Related Questions