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 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++?