C++Game DevelopmentLanguage FeatureLearn C++

Learn To Simulate Realistic 2D Ball Physics in C++ Builder

In this post we will explain how we can simulate realistic 2D ball physics in an easy and accessible 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 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.

Understanding how 2D physics works

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.

Learn To Simulate Realistic 2D Ball Physics in C++ Builder diagram of ball physics

You can download Free the C++ Builder Community Edition (CE) here: https://www.embarcadero.com/products/cbuilder/starter.
Professional developers can use the Professional, Architect or Enterprise versions of C++ Builder. You can download and use the trial version for one month with the same capabilities as the full version . Please visit https://www.embarcadero.com/products/cbuilder.

Let’s create a C++ class to simulate a ball

To simulate ball, we can create

Let’s define balls

We can set all balls in our form creation as below;

Applying the physics

Now we should apply physics in a Timer, we should check if our ball is touching to borders, touching the ground. We should change its velocity direction by multiplying with -1. We can also add some friction (velocity loss) so we can multiply with -0.98. Finally we should change its x y position with well known physics (V = V+g*t or X =Vt+gt*t etc.). We should apply all these in a timer. We can check many balls in a for loop.

For example, we can check all balls physics in a Timer as below,

Simulating ball movement every millisecond in C++

Timer to simulate every millisecond,

Note that you can add rotation to image by changing its rotation. You can also touch it so user can throw this ball. You can develop many creative new games with C++ Builder as given example above.

C++ Builder is an incredibly powerful development environment which can help you rapidly build applications which can operate at the full speed of the device. Why not download a free trial today and see C++ Builder for yourself?


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.

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

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
Artificial Intelligence TechC++C++11C++14C++17Learn C++

Why You Should Know ELU Artificial Neural Net Functions

Artificial Intelligence TechC++C++11C++14C++17Learn C++

What You Need To Know About C++ Gaussian Error Linear Units

C++C++11C++14C++17Learn C++

What Is The SoftPlus Activation Function in C++ Neural Nets?

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Make AI Binary/Heaviside Step Functions In C++