Got it sorted now and working a dream.
Code:
roll[3]={0.0,-0.4,-0.8];
for (n=0; n<3; n++){
if(roll[n]>0.5){
sphere =2;}
if(roll[n]>1){
sphere =3;
}
if(roll[n]>1.5){
sphere = 4;
}
if(roll[n]>2.5){
sphere=5;
}
if(roll[n]>3){
roll[n]=0;
sphere = 1;
}
switch (sphere){
case 0:
roll[n] = roll[n];
break;
case 1:
glPushMatrix();
roll[n] = roll[n]+0.1f;
glTranslatef(roll[n],0,0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
break;
case 2:
glPushMatrix();
roll[n] = roll[n]+0.1f;
glTranslatef(roll[n],0,0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
break;
case 3:
roll[n]=roll[n]+0.1f;
glPushMatrix();
glTranslatef(1,-roll[n]+1,0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
break;
case 4:
roll[n]=roll[n]+0.1f;
glPushMatrix();
glTranslatef(-roll[n]+2.5,-0.5,0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
break;
case 5:
roll[n]=roll[n]+0.1f;
glPushMatrix();
glTranslatef(0,roll[n]-3.0,0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
break;
}
}
Putting the if statements above the switch case changes the direction before the movement of the ball and means the balls move in a smooth roll around any curves rather than turning as block.
However, there are some important things to remember,
The first switch doesn't work too well so put in a non-turning switch on the first straight.
Also make sure the first straight is long enough to handle the entire length of the balls. (i.e 3 balls with a 0.4 radius needs a minimum straight of 0.8).
Most importantly correct the switches for roll[n] values.
roll[n] in the 2nd switch is 1 not 0 so to turn it at xy (1,0) the switch needs to be (1, roll[n]-1) or your balls will turn all over the place and become increasingly disorganised.