# 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.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.