loop problem in code

This is a discussion on loop problem in code within the C++ Programming forums, part of the General Programming Boards category; Basically ive made a class that stores functions for a ball and then a main class that stores my world ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    34

    loop problem in code

    Basically ive made a class that stores functions for a ball and then a main class that stores my world objects. From the main i create an instance of the ball using its constructor and an array (for multiple instances) and for a set period the ball bounces around using a call to the ball classes evolve function (time steps its path). Problem is that i want to fire many balls at the same time however the program pauses while it carries out the while loop to evolve the current ball.

    The code is below.

    Code:
    #include "cannon.h"
    #include <windows.h>
    #include <iostream.h>
    #include <math.h>
    #include <stdlib.h>
    #include <glut.h>
    
    
    float angle1=45.0f;
    float angle2= -90.0f;
    double mag=5;
    double move=0;
    
    float Camera_angle = 0.0,ratio, Camera_posx = 0.0,Camera_posy = 0.0,Camera_posz = 0.0;
    float Los_posx = 0.0,Los_posy = 0.0,Los_posz = -0.5;
    
    GLfloat ballColor[] = {0.3,0.3,0.9,1.0};
    GLfloat crossHColor[] = {0.8,0.3,0.3,1.0};
    
    float ballY;
    float ballX;
    float ballZ = -1;
    float crossX;
    float crossY;
    float crossZ;
    
    bool ballMov=false;
    
    
    void init(void)
    {	
    	glEnable(GL_DEPTH_TEST);
    	glClearColor(0.0f,0.0f,0.0f,0.5f);
    
    	glShadeModel (GL_SMOOTH);
       	glEnable(GL_LIGHTING);
    
    	//Light 0 Properties/Position
    	GLfloat ambientLight[]={0.1,0.1,0.1,1.0};
    	GLfloat position[] = {-3,3,3,1};
    	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    	glLightfv(GL_LIGHT0, GL_POSITION, position);
    
    	glEnable(GL_LIGHT0);
    	//Perspective
    /*	glMatrixMode(GL_PROJECTION) ;
    	glLoadIdentity() ;
    	gluPerspective(45.0, 1.0, 1.0, 40000.0);
    	glMatrixMode(GL_MODELVIEW) ;*/
    
    }
    
    void crosshair(float x,float  y,float z)
    {
    	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, crossHColor);  
    	glutSolidSphere(0.02,20,20);
    }
    
    void cannonBall(double x,double y,double z)
    {
    	glTranslatef(x,y,z);
    	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, ballColor);  
    	glutSolidSphere(0.05,20,20);
    }
    
    void createGround()
    {
    GLfloat grassColor[] = {0.0,1.0,0.0,1.0};
    GLfloat seaColor[] = {0.3,0.6,0.3,1.0};
    
    float floorHeight=-0.05f;//-0.1745
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, seaColor); 
      glBegin(GL_QUADS);
    	glVertex3f(-30.0f, floorHeight, -1.0f); //far left
    	glVertex3f(30.0f, floorHeight, -1.0f); //far right
    	glVertex3f(30.0f, floorHeight, -30.0f); //rear right
    	glVertex3f(-30.0f, floorHeight, -30.0f); //rear left
      glEnd( );
    
    
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grassColor); 
      glBegin(GL_QUADS);
    	glVertex3f(-30.0f, floorHeight, -1.0f); //far left
    	glVertex3f(30.0f, floorHeight, -1.0f); //far right
    	glVertex3f(30.0f, floorHeight, 4.0f); //rear right
    	glVertex3f(-30.0f, floorHeight, 4.0f); //rear left
      glEnd( );
    }
    
    void display(void)
    {
      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
    
    	
    	//glRotatef(rotateangle,0,1,0);
    	//glRotatef(rotateangle1,1,0,0);
    	//glRotatef(rotateangle2,0,0,1);
    
    	//Draw Ball
    	glPushMatrix();
    	cannonBall(ballX,ballY,ballZ);
    	glPopMatrix();
    
    	glPushMatrix();
    	//crosshair(crossX, crossY, crossZ);
    	createGround();
    	glPopMatrix();
    	//glLoadIdentity();
    	//glEnd();
    		//gluLookAt(0,0,0, 0,0,-3,0,1,0);
    
    	glFlush();
    	glutSwapBuffers();
    }
    
    int ballNo=0;
    cannon *cballs[90];
    
    void fire()
    {   
    	cballs[ballNo] = new cannon(5*ballNo,90,5);
    
    	float delta=0.006;
    	int iter = 100;
    
    	while(iter>2)
    	{		
    		cballs[ballNo]->evolve(delta);
    
    		ballY = cballs[ballNo]->gety();
    		ballX = cballs[ballNo]->getx();			
    		ballZ = cballs[ballNo]->getz();
    
    		iter-=1;
    		display();
    	}
    	ballNo+=1;
    }
    
    
    void anim()
    {
    	display();
    }
    
    void orientate(float ang) 
    {
    
    	Los_posx = sin(ang);
    	Los_posz = -cos(ang);
    	glLoadIdentity();   
    	gluLookAt(Camera_posx, Camera_posy, Camera_posz, 
    		      Camera_posx + Los_posx,Camera_posy + Los_posy,Camera_posz + Los_posz,
    			  0.0,1.0,0.0);
    }
    
    void moveFlat(int direction) 
    {
    	Camera_posx = Camera_posx + direction*(Los_posx)*0.1;
    	Camera_posz = Camera_posz + direction*(Los_posz)*0.1;
    	glLoadIdentity();
    	gluLookAt(Camera_posx, Camera_posy, Camera_posz, 
    		      Camera_posx + Los_posx,Camera_posy + Los_posy,Camera_posz + Los_posz,
    			  0.0,1.0,0.0);
    }
    
    void moveUp(int direction) 
    {
    	Camera_posy = Camera_posy + direction*(Los_posx)*0.1;
    	Camera_posy = Camera_posy + direction*(Los_posz)*0.1;
    	glLoadIdentity();
    	gluLookAt(Camera_posx, Camera_posy, Camera_posz, 
    		      Camera_posx + Los_posx,Camera_posy + Los_posy,Camera_posz + Los_posz,
    			  0.0,1.0,0.0);
    }
    
    void keyboard(unsigned char key, int x, int y)
    {
      char c='#';
      
      switch(key)
        {
    		case('w'):
    			moveFlat(1);
    			break;
    
    		case('s'):
    			moveFlat(-1);
    			break;
    
    		case('q'):
    			moveUp(1);
    			break;
    
    		case('e'):
    			moveUp(-1);
    			break;
    
    		case('a'):	  
    			Camera_angle -= 0.05;
    			orientate(Camera_angle);
    			break;
    
    		case('d'):	  
    			Camera_angle += 0.05;
    			orientate(Camera_angle);
    			break;
    
    		case('m'):
    			mag+=1;
    			break;
    
    		case('n'):
    			angle1=angle1+1;
    			break;
    
    		case('l'):
    			ballMov=false;
    			cout<<"nien";
    			break;
    
    		case('f'):
    			fire();
    			break;
        }
    
    
      glutPostRedisplay();
    }
    
    void reshape(int w, int h)//Function To Create Prospective
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(55, 1.0, 0.1, 400);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
    }	
    
    int main( int argc, char * argv[])
    {
       glutInit(&argc, argv);
       glutInitDisplayMode (GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
       glutInitWindowSize (800,800);  
       glutCreateWindow ("Game");   
       init();
       glutReshapeFunc(reshape);
       glutDisplayFunc(display);
       glutKeyboardFunc(keyboard); 
       glutIdleFunc(anim);
       glutMainLoop();
       return 0;
    }
    As you can see when the f key is hit an instance is created and the while loop in the fire(); function is initiated however it hangs on that loop and does not allow me to fire a second ball until this while loop has finished. Any ideas how to get round this problem?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    You're showing the complete arc of the cannon ball in the fire() function? fire() should create another cannon ball in your array (why not std::vector?), and some kind of update() method should increment the position of all cannon balls. Your game loop should be something like this:

    - get input
    - update all objects
    - draw all objects

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weired problem in while loop
    By avisik in forum C Programming
    Replies: 14
    Last Post: 12-01-2008, 01:41 PM
  2. Replies: 12
    Last Post: 06-08-2005, 12:23 PM
  3. Odd problem in main(), code won't proceed
    By aciarlillo in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2005, 12:00 AM
  4. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 03:02 AM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 10:16 AM

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