C++Game DevelopmentLanguage FeatureLearn C++

How To Simulate 3D Ball Physics In C++ Using OpenGL

How To Simulate 3D Ball Physics In C++ Using OpenGL example image

Simulating 3D ball physics is one of those tasks that sounds difficult but is actually fairly simple in C++ IDE. In this post, you’ll learn how to simulate 3D balls in a cube, using OpenGL in C++ Builder; adding physics to a 3D object to make it more realistic, and creating 3D animations with OpenGL.

Using C++ Builder for animations, simulations and games is not difficult

C++ Builder makes it relatively easy to build these kind of simple simulations and games. C++ is a fast programming language that you can use to develop fully-native simulations and games which operate with maximum performance. With C++ Builder, you can directly create your own 3D objects and you can animate them at runtime in real-time. The Viewport3D (TViewportd3D) component in C++ Builder FireMonkey projects is very effective at displaying many basic 3D Objects like geometric planes, cubes, spheres, cones, 3D ellipses and much more. Please see this post about Working With 3D In Modern Windows C++ Development for creating these 3D objects. You also might find it useful to visit this post: How To Simulate Realistic Physical Effects in C++ which goes into a related topic for 2d objects.

We can also easily load your 3D objects into Viewport3D by using Model3D (TModel3D).

We can also use OpenGL or Direct3D libraries or some other 3rd party 3D Engines. In this post we will see how we can simulate ball physics in 3D.

What is OpenGL?

OpenGL (Open Graphics Library) is a 3D library for development of graphical programs. It is a cross-languagecross-platform Application Programming Interface (API) for rendering 2D and 3D vector graphics. The OpenGL API is typically used to interact with CPU and mostly with GPU to achieve hardware accelerated rendering.

Creating an example OpenGL application in C++

C++ Builder supports to develop very low level graphics (here low term means very fast and native codes for 3D graphics) with specific C++ codes or with OpenGL. If you want to simulate something with OpenGL or if you want to develop a game with OpenGL, we should setup openGL we need a loop that loops on Application Idle. In this loop we should apply physics and and we should draw or update our 3D objects.

In this example we will have balls in a cube and this balls will attract with the boundaries of this cube and with each other. Let’s start with creating a new VCL application in C++ Builder.

How to set up an OpenGL example header file in C++ Builder

First switch to Unit1.h from the tabs below the IDE to modify header. We should include two main OpenGL headers (gl.h and glu.h) as below,

As we described above, we need to declare IdleLoop(), Setup_OpenGL(), Create_Objects3D(), Draw_Objects3D(), Apply_Physics3D() methods as below.

We need to define number of balls, we need to add some private and public declarations for OpenGL settings and for our objects, here is the Unit1.h Header of full example,

How to create an OpenGL example CPP File in C++ Builder

Now let’s back to Unit1.cpp by using tabs below,

Let’s summarize how our OpenGL app will work, this is the whole main part of our app in summary. We need IdleLoop() that runs Apply_Physics3D() and Draw_Objects3D(). When form is created we should setup OpenGL(). If this OpenGL set it up successful, then we should run Create_Objects3D() and we should also set to our IdleLoop() method to application ( Application->OnIdle = IdleLoop; ). Thus, our application should be as below.

Here, if our app sets OpenGL it creates object and defines IdleLoop method as Application->IOnIdle(). This loop will calculate physical environment and it will draw objects every loop.

OpenGL pixel formats, lightings and textures

Our Setup_OpenGL() method will setup Pixel Format then it will create a Context with this Pixel Format and then it will set some OpenGL settings, Lightings, Textures etc. Here is our example below,

Creating and drawing OpenGL objects in C++ Builder

We can create cube by creating a Cylinder – gluCylinder() – with 4 slices and 1 section. In our Generate_Cube3D() method we have object number, cube size and R,G,B values. Here is our Generate_Cube3D() method that draws OpenGL cube 3D.

We can create our balls by creating a Sphare – gluSphere(). In our Generate_Ball3D() method we have object number, ball radius and R,G,B values. Here is our Generate_Ball3D() method that draws OpenGL ball 3D.

Making a Create_Objects3D() C++ method

Now lets define our Create_Objects3D() method with these two methods above. In this method, first we should define position, velocity and radius parameters of our balls randomly and then we should generate our 3D Cube and our Balls in OpenGL Matrix with two methods above. Here is our method below,

Now we can draw objects to OpenGL matrix as in our Draw_Objects3D() method below,

How to apply simulated physics to 3D objects in C++

Remember that we do calculations to apply physics in our IdleLoop(). Thus we should define this Apply_Physics3D() method as below,

In this method we check cube boundaries and if there is collusion we reverse the velocity vector by multiplying with -1. Here we also check ball collusions with each other then if there is collusion we change velocity vectors of balls.

Setting some methods of the form


We need these OnPaint(), OnResize() and OnDestroy() procedures to apply changes in our OpenGL.

How to add mouse and keyboard interactions to an OpenGL display in C++

Finally if we can simply rotate our OpenGL environment by mouse or by keyboard as given example below.

Here is our example C+ OpenGL Application running

Now you can run example and you will see that balls are moving and pinging at the borders and with each other. When this app is running our IdleLoop() will keep calculating physical conditions and formulas and it will draw new positions of all OpenGL objects.

Here is the example output with 3 balls,

If you have errors about variables or procedures please check full Unit1.h header above.

Here is the full Unit1.cpp example.

As you see C++ Builder is very capable to build your own OpenGL apps from the scratch with modern methods.


C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, we have Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?