C++Game DevelopmentLanguage FeatureLearn C++

This Is How To Simulate Ball Physics 3D in A C++ App

Do you want to learn how to make your C++ app simulate physics of objects in 3D without using any 3D engine? In this post we will explain how we can simulate ball physics 2D in a simple way. Let’s assume that we look from a X-Y view, and Y is the height that means Y=0 is the ground. How we can simulate a ball physics in a given ball velocity, and gravity in that environment?

C++ Builder is a great compiler and C++ IDE with FireMonkey and VCL frameworks. It has compilers for Win32, Win64, Android and iOS. C++Builder has both CLANG Enhanced C/C++ Compiler and a Borland C/C++ Compiler. It also features a modern, high-productivity RAD Studio IDE, debugger tools, and enterprise connectivity for to accelerate cross-platform UI development. We can develop GUI based applications easily, as it comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition that can be used by students, beginners and startups with limitations.

Let’s assume that we have a ball at bx and by coordinate with a given Vx and Vy velocity. Now we want to see what happens in every milliseconds under the gravitational environment.

What is a TSphere and how can we use it?

The TSphere is is used in Viewport3D component, it is a 3D sphere, a class implements a 3D sphere shape built on a 3D wireframe, that can be placed on a 3D FireMonkey form. TSphere is a visual object that can be added from the Tool Palette. To change the color or add texture to the sphere, use the MaterialSource property. Set SubdivisionsAxes and SubdivisionsHeight to specify how smooth the sphere’s surfaces are.

Here is a short example C++ app showing how to use TSphere

Let’s create a new Multi-Device C++ Builder FireMonkey Application and save all project and unit files to a folder. Now we should create a ball class, g gravity

Let’s create new balls and lets define their radius, position and velocity randomly.

Simulating ball physics in your own C++ app

We can simulate ball physics in time fractions, we will use dt variable to see our simulation in that time frictions. To simulate ball physics on the ground in 3D we should detect ball position in Y direction, if this Y position smaller than ball radius that means ball hits the ground so we should change the velocity vector direction by multiplying -1, if we want to add some energy loss on the ground, we can multiply with between 0.0 to -1 (i.e. 0.9).

We can add a Timer (TTimer) component, drag this component to your form and set its Interval property to between 10 to 35, that means each interval will simulate our dt fraction of time in that interval time. If you are new to C++ Builder and you don’t know how to Timer component, we had a previous post that explains well all about Timer component. Please read this post below.

Double click to Timer component to create OnTimer() event and modify lines inside as below,

What does the C++ app example do?

This Timer event sets position of balls in their velocity in each interval. Also, it checks Y position of each ball and if it hits the ground sets its new velocity with a small velocity loss. We added a line to printout the position of first ball[0] parameters. You can remove this to make it faster and smooth.

Finally, when we close the form, we should free all mesh objects that we allocated with the new command, as below,

Now you can simulate many ball objects, or you can add more detailed physical or engineering formulas to simulate other objects.


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 was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is Deleted Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?