ball movement problem

This is a discussion on ball movement problem within the Game Programming forums, part of the General Programming Boards category; I'm trying to move upto 30 balls in 3 groups around a circuit using for commands and switches. This is ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    Slovakia
    Posts
    21

    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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > 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]
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Location
    Slovakia
    Posts
    21
    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.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Location
    Slovakia
    Posts
    21
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D ball problem
    By software tester in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2011, 02:54 PM
  2. Replies: 14
    Last Post: 08-24-2010, 04:13 AM
  3. Replies: 13
    Last Post: 08-22-2010, 12:37 PM
  4. Help with Ball Movement please!!
    By SRM in forum Game Programming
    Replies: 2
    Last Post: 04-24-2003, 07:59 PM
  5. Movement problem...
    By Umoniel in forum C Programming
    Replies: 2
    Last Post: 11-10-2002, 11:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21