Archive Pages Design$type=blogging

Simple Opengl Program - Bouncing Ball

Learn how to draw simple opengl program the Bouncing Ball in opengl in C computer graphics.

We have shared so many OpenGL Programs, today we are going to share a simple opengl program on Bouncing Ball. This program is simple, easy to code and understand as well, you can modify the program easily and made it into a superb project.

Simple Opengl Program - Bouncing Ball


Code Bouncing Ball C Program

Initialization

First we create a flag which will check the ball's uppermost position and lower most position. we will also define the coordinate of ball that varies as it bounce. The coordinate will be determine by a certain variable which changes the coordinate position by an amount which we fix. 

static int flag=0;
float tx=0.0,ty=0.0,tz=0.0;
float ball_x=0.5,ball_y=0.0,ball_z=0.0;

Draw and Update Ball

In this Simple Opengl Program we are going to create a procedure that will update the position of ball according to it's motion upward and downward. The function will use the variable flag as mentioned above. The update ball function will update the position of the ball of up and down. 

void updateBall()
{
    if(!flag)
    {
        ball_y+=0.05;
        if(ball_y>1.0)
            flag=1;
    }
    if(flag)
    {
        ball_y-=0.05;
        if(ball_y<-1)
            flag=0;
    }
}

In the draw function of this Simple Opengl Program we are going to draw the ball and make it bounce. Set current matrix on the stack and draw the Ball with the help of  glutSolidSphere. we call the Update function here so that the ball an change according to it's position going up or down. 

void display()
{
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
     
        glClearColor(0,1,0,0);
     
       glPushMatrix();
     
        glBegin(GL_POLYGON);
            glColor3f(1,0,1);
         
            glVertex3f(-0.25,0,-1);

            glVertex3f(0.25,0,-1);

            glVertex3f(0,0.25,-1);
        glEnd();
        glPopMatrix();
 

        glPushMatrix();
            glColor3f(1,1,0);
            glTranslatef(ball_x,ball_y,ball_z);
            glutSolidSphere(0.3,60,23);
        glPopMatrix();

        updateBall();


        glutSwapBuffers();
}

Future Enchantments of Program

Drawing Ball

You can change the size of the ball by modifying the glutSolidSphere, color of the ball can also be modified similarly. Background of the color can also be added and changed accordingly. You an draw any new object in the background as the ball bounces.

What you can Modify

You can modify this Simple Opengl Program by adding some user interaction that will let ball start and stop. You can add sound to the program a well. The 3D version of the program can also be made as a future enchantments.   


Download the Simple OpenGL Program on Bouncing Ball.

COMMENTS

Name

3D Bi-Cycle 3D Game 3D Laptop 3D Objects 3D Projects 3D Zoo Algorithm Android Aquarium Bellman-Ford Algorithm Block Breaker Blockshooting Bucket Sort C# Chess Clock Code Blocks Colors Complex Project Complex Projects Crab Pong Cross Zero DSAV Escapa First Come First Serve Fluids Font Demo Games Gear Motion GlutDino Glutplane Graphics Editor iOS iPhone Java Java OpenGL Graphics Programming Light Torus Linked List Ludo Board Game Mancala board games Memory blocks game Memory Game Menu Mickey Mouse Minesweeper Miniature Steam Engine moth Motion Blur MoveLight moving car Moving Ship Multiplex Networking Based Project Nuclear Power Plant Olympic opengl c++ examples OpenGL ES OpenGL Tutorial Origami OS Based Projects Paint Paper Folding Particles Drop Path Finding ping pong Pong Game Project Report Projects Report Puzzles qix like Random Flowing lines Rigid Body Rings Robot sample c++ opengl code Screen Saver Screen Saver Ship Shadow Cube Shadowfun Ship Iceberg Simple Drawing Simple Haloed Lined Wireframe Simple Move Light Simple Project Simulation SNAKE XENZIA GAME Solar system Sorting Sphere Spot Light Swing Stars Taj Mahal The Edge Game Tic Tac Toe Tower of Hanoi Traffic Signal Transformation Triangular Animation Tutorial water ripple effects Whirlpool
false
ltr
item
OpenGL Projects: Simple Opengl Program - Bouncing Ball
Simple Opengl Program - Bouncing Ball
Learn how to draw simple opengl program the Bouncing Ball in opengl in C computer graphics.
https://4.bp.blogspot.com/-QYh8-e4Mr1g/VtLf1wJebdI/AAAAAAAAHkU/g8LfleYaxTY/s320/Simple%2BOpengl%2BProgram%2B-%2BBouncing%2BBall.PNG
https://4.bp.blogspot.com/-QYh8-e4Mr1g/VtLf1wJebdI/AAAAAAAAHkU/g8LfleYaxTY/s72-c/Simple%2BOpengl%2BProgram%2B-%2BBouncing%2BBall.PNG
OpenGL Projects
http://www.openglprojects.in/2016/02/simple-opengl-program-bouncing-ball.html
http://www.openglprojects.in/
http://www.openglprojects.in/
http://www.openglprojects.in/2016/02/simple-opengl-program-bouncing-ball.html
true
2020194800836807725
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago