-
ball movement problem
I'm trying to move upto 30 balls in 3 groups around a circuit using for commands and switches. This is to save on using 30 switches 1 for each ball. However, the problem is that the changes in direction aren't acting on each ball but on the lead ball in each group. As each change of direction occurs the problem intensifies and ends up with balls all over the screen going in every direction. Instead of politely following the lead ball and turning the corners as appropriate.
This is the code segment in question.
Code:
for (n=0; n<3; n++){
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:
roll[n]=roll[n]+0.1f;
glPushMatrix();
glTranslatef(1,-roll[n],0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
break;
case 3:
roll[n]=roll[n]+0.1f;
glPushMatrix();
glTranslatef(-roll[n],-1.5,0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
break;
case 4:
roll[n]=roll[n]+0.1f;
glPushMatrix();
glTranslatef(0,roll[n],0);
glutSolidSphere(0.2,40,40);
glPopMatrix();
break;
}
if(roll[n]>1){
sphere =2;
}
if(roll[n]>1.5){
sphere = 3;
}
if(roll[n]>2){
sphere=4;
}
if(roll[n]>3){
sphere = 1;
roll[n]=0;
}
}
Any ideas how I can correct this or should I set up an additional file with all thirty switches?
I don't want to use an additional thirty switches in the main file if possible as it will cause the program complexity to get out of hand and debugging will be a nightmare.
I'm sorry but this segment has been changed so many times that all semblance of indentation went out of the window long ago.
-
> I'm sorry but this segment has been changed so many times that all semblance of indentation went out of the window long ago.
Well while you're waiting, you could fix that and post a better version.
People will be in no hurry to review code which looks a mess.
Though quite why you don't have a data structure which matches your description is beyond me.
I mean, how about
Code:
balls[3][10];
for ( group = 0 ; group < 3 ; group++ ) {
for ( n = 0 ; n < 10 ; n++ ) {
// stuff with balls[group][n]
}
}
-
Thanks, I'll give that a try but the groups will be 1 group of 4 and 1 group of 11 and a group of 12. I'm not sure if this would cause any problem.
I'll look at properly indenting the code while I wait but I also have a fair amount of reading to do as well.
I'm starting a new job as a system tester next month.
-
Well then you could just use
Code:
ball balls[3][MAX_BALLS_IN_GROUP];
int ball_count[3] = {4, 11, 12};
// copying Salem's example:
for ( group = 0 ; group < 3 ; group++ ) {
for ( n = 0 ; n < ball_count[group] ; n++ ) {
// stuff with balls[group][n]
}
}
for example.
-
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.