1. ## 3D ball problem

I'm trying to swing two balls so that when the first ball swings from -20 degrees to zero it stops and sends the second ball swinging out to 20 degrees and returning back to zero again.

However, that problem is that whilst I can get both balls swinging between the ranges at the same time I can't get the one ball to move whilst the second is still and vice versa. I tried a switch case but that failed totally.

any ideas?
then I can get a Newton's Cradle working

Code:
```#include <iostream>
#include <GL/glut.h>
#include <windows.h>
#include <math.h>

using namespace std;
HDC hdc;

int slices = 40;
int stacks = 40;

float angle[2] = {0.0f, 0.0f};

GLfloat mat_ambient[] = {0.5, 0.5, 0.5, 0.01};
GLfloat mat_specular[] = {0.1, 0.1, 0.1, 0.01};
GLfloat mat_shininess[] = {50.0};
GLfloat light_position[] = {10.0, 10.0, 20.0, 0.0};
GLfloat model_ambient[] = {0.5, 0.5, 0.5, 0.0};

void setupMaterials() {
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
}

void changeColour(GLfloat r, GLfloat g, GLfloat b, GLfloat A) {
model_ambient[0] = r;
model_ambient[1] = g;
model_ambient[2] = b;
model_ambient[3] = A;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
}

void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.01);
setupMaterials();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
}

void reshape(int w, int h) {
GLfloat fAspect;
if(h==0) h=1;

glViewport(0,0,w,h);

fAspect = (GLfloat)w / (GLfloat)h;

glMatrixMode(GL_PROJECTION);

gluPerspective(60, fAspect, 0.5, 1000.0);
glTranslatef(0,0,-1);
glMatrixMode(GL_MODELVIEW);
}
void DrawSphere(){

glPushMatrix();

changeColour(0.3, 0.3, 0.35, 0.0);
glTranslatef(0.0f, 0.5f,0.0f);
glBegin(GL_LINES);

glVertex2f(0.0, 0.0);
glVertex2f(0.0, -0.5);
glEnd();

glTranslatef(0.0, -0.5, 0.0);

glPopMatrix();
}
void DrawSphere2(){

glPushMatrix();
changeColour(0.3, 0.3, 0.35, 0.0);
glTranslatef(0.0f,0.5f,0.0f);
glBegin(GL_LINES);

glVertex2f(0.2, 0.0);
glVertex2f(0.2, -0.5);
glEnd();
glTranslatef(0.2, -0.5, 0.0);

glPopMatrix();
}
void display(void) {

bool static sphere= false;
bool static sphere2=true;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (sphere){
angle[0] = angle[0] +1.0f;
}
else {
angle[0] = angle[0] -1.0f;
}
if (sphere2){
angle[1] = angle[1] +1.0f;
}
else {
angle[1] = angle[1] -1.0f;
}

if(angle[0]<=-20.0f){
sphere = true;

}

if(angle[0]>=0.0f){
sphere = false;

}
glPushMatrix();
glTranslatef(0.0, 0.5,0.0);
glRotatef(angle[0],0.0,0.0,1.0);
glTranslatef(-0.0, -0.5, 0.0);
DrawSphere();
glPopMatrix();

if(angle[1]<=0.0f){
sphere2 = true;
}

if(angle[1]>=20.0f){
sphere2 = false;
}
glPushMatrix();
glTranslatef(0.2, 0.5,0.0);
glRotatef(angle[1],0.0,0.0,1.0);
glTranslatef(-0.2,-0.5,0.0);
DrawSphere2();

glPopMatrix();

glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27:
exit(0); // Exit the application if 'Esc' key is pressed
}
}

void animate() {
Sleep(100);

glutPostRedisplay();
}
void processMouse(int button, int state, int x, int y){
if (state==GLUT_DOWN){
if((button==GLUT_LEFT_BUTTON) && x<350 && x>450){
glTranslatef(0.0, 0.5,0.0);
glRotatef(x, 0.0 ,0.0, 1.0);
glTranslatef(0.0, -0.5,0.0);
angle[0] = angle[0]+x;
}
}
if (state==GLUT_UP){
if (button==GLUT_LEFT_BUTTON && angle[0]!=0){
animate();
}
}
}
int main(int argc, char * argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (800, 600);
glutCreateWindow (argv[0]);   init();
glutKeyboardFunc (keyboard);
PaintDesktop(hdc);
glCullFace(GL_BACK);
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutMouseFunc(processMouse);
glutIdleFunc(animate);
glutMainLoop();
return 0;
}```

2. Well, at least you make it rather obvious which parts of the code you copy-pasted, and which ones you attempted to write yourself.

So, welcome to MWAAAHAAA's troubleshooting for beginners. Let's get started with the first step in getting your program on the right track:

Step #1(a) to solving your problem: properly indent the code you wrote.
Step #1(b): please explain (in properly indented pseudo-code) the logic you are attempting to implement for achieving your objective.

We'll get to step #2 once above items have been taken care of.