C++ help!

This is a discussion on C++ help! within the C++ Programming forums, part of the General Programming Boards category; I want to add the gravity factor in my program.I have a circle class and I want to move the ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    C++ help!

    I want to add the gravity factor in my program.I have a circle class and I want to move the circle under gravity.I used opengl library to make the circle but dont know how to open it as if it falling!

    Please help me!
    Code:
    #include "glut.h"
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <iostream>
    using namespace std;
    
    #define M_PI 3.14159265358979323846f
    
    int g_Width=400;
    int g_Height=400;
    float gravity=200;
    int Mx=0;
    int My=0;
    char press;
    
    void init();
    void myMouseFunction( int button, int state, int mouseX, int mouseY );
    void myKeyboardFunction( unsigned char key, int mouseX, int mouseY );
    void mySpecialKeysFunction(int key, int x, int y);
    void myReshapeFunction( int width, int height );
    void myTimerFunction( int val );
    void myDisplayFunction();
    void myMouseMoveFunction(int x,int y);
    
    class Circle{
    
        int x;
        int y;
        int radius;
    public:
    
        Circle(){};
        ~Circle(){ printf("destructor called\n");}
        
        static int count;
    
        Circle(int a, int b, int c)
        {
            x=a; y=b; radius=c;
        }
        void set_data(int a, int b, int c)
        {
            x=a; y=b; radius=c;
        }
        
        void draw_circle()
        {
            float angle;
            count++;
       
        glColor3f(0.0f, 1.0f, 0.0f);
        glLineWidth(1.0f);
        glBegin(GL_LINE_LOOP);
        {
        
        for(int i = 0; i < 100; i++) {
            
            angle = i*2*M_PI/100;
            glVertex2f(x + (cos(angle) * radius), y + (sin(angle) * (radius)));
        
    
        }}
        glEnd();
       
        }
    
        friend void display_circle_data(Circle c); //Friend Function
        
    
    };
    int Circle::count=0;
    
    //friend function
    void display_circle_data(Circle c){
        printf("\nRadius is %d\n",c.radius);
        printf("Xvalue is %d\n",c.x);
        printf("Yvalue is %d\n",c.y);
    }
    
    Circle x(0,0,5);
    Circle y(399,399,5);
    Circle z(x);
    
    Circle* c5 = new Circle();
    
    
    int main(int argc, char** argv)
    {
        glutInit( &argc, argv );
    
        glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
        glutInitWindowSize( g_Width, g_Height ); 
        glutInitWindowPosition( 0, 0 );
        glutCreateWindow( "Danish game wala" );
    
        init();
    
        glutMouseFunc( myMouseFunction );
        glutKeyboardFunc( myKeyboardFunction );
        glutSpecialFunc( mySpecialKeysFunction );
        glutMotionFunc(myMouseMoveFunction);
    
        glutReshapeFunc( myReshapeFunction );
        glutDisplayFunc( myDisplayFunction ); 
        glutTimerFunc( 33, myTimerFunction, 0 );     
        //glutFullScreen();
        //glutSetCursor( GLUT_CURSOR_NONE );
    
        
    
        glutMainLoop();
        
    
        return 0;
        
             
        
    
    }
    
    void init(void) 
    {
        glClearColor( 0.0, 0.0, 0.0, 0.0 );
    
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
    
        glOrtho(0.0, g_Width, 0.0, g_Height, -1.0, 1.0);
    }
    
    void myMouseFunction( int button, int state, int mouseX, int mouseY ) 
    {
        //glVertex2f(mouseX,mouseY);
        printf("\nmouseX: %d mouseY: %d, State: %d",mouseX,mouseY,state);
        Mx=mouseX; My=mouseY;
    
        
        cout<<"C is pressed\n";
    
        c5->set_data(mouseX,400-mouseY,5);
        //for (int gravity=0;gravity<100;gravity++){
        c5->draw_circle();
        //glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
        //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //    }
        if (state==1){
        cout<<"No of Circles are "<<(Circle::count)/2;
        }
        
        
            
         if(press =='d'){
            cout<<"delete";
            //delete c5;
            
        }
        else if(press =='s'){
            
            }
        if(press =='t'){
            }
        
        //xCircle s(mouseX,400-mouseY,5);
    }
    
    void myKeyboardFunction( unsigned char key, int mouseX, int mouseY )
    {
        if( key == 'c' ) // i.e. Esc key
        {
            press ='c';
            //display_circle_data(s);
        }
        if (key=='d')
        {press='d';
            }
        else if (key=='s')
        {press='s';
        }
        if (key=='t')
        {press='t';
        }
        if (key=='+')
        {
        }
        if (key=='-')
        {
        }
        else if(key==27)
            exit(0);
    }
    
    void mySpecialKeysFunction(int key, int x, int y)
    {
        switch( key )
        {
        case GLUT_KEY_UP:
            break;
        case GLUT_KEY_DOWN:
            break;
        case GLUT_KEY_LEFT:
            break;
        case GLUT_KEY_RIGHT:
            break;
        case GLUT_KEY_HOME:
            break;
        case GLUT_KEY_END:
            break;
        case GLUT_KEY_PAGE_UP:
            break;
        case GLUT_KEY_PAGE_DOWN:
            break;
        }
    }
    
    void myReshapeFunction( int width, int height )
    {
        glClear(GL_COLOR_BUFFER_BIT);
    
        g_Width  = width; 
        g_Height = height;
    
        glViewport (0, 0, g_Width, g_Height);
    
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
        glOrtho(0.0, g_Width, 0.0, g_Height, -1.0, 1.0);
    }
    
    void myTimerFunction( int val )
    {
        glutTimerFunc( 33, myTimerFunction, 0 );     
        myDisplayFunction();
    }
    int i=0;
    void myDisplayFunction()
    {
    //    glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        
        
        //x.draw_circle();
        //y.draw_circle();
        //s.draw_circle();
        glutSwapBuffers();
        
    }
    
    void myMouseMoveFunction(int x,int y)
    {
        printf("Mouse moved at x:%d y:%d",x,y);
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    Well there are physics libraries that will help you model gravity effects - rolling your own code to emulate the effect is not i think, for you right now. - It seems that you have a bit of a frankenstein mix of coding and pasting and c and c++, and even half of your globals are free variables, - i would hope that gravity is a constant?
    Also sorry but indentation/ style concistency is poor - probably due to pasting, code is littered with commented out lines, no wonder you have not had an answer until now - I dont often rant about poor posts, where code has been offered, but really - tidy it up
    Last edited by rogster001; 10-05-2012 at 03:03 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

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