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 an X-Y view, and Y is the height which means Y=0 is the ground. How we can simulate 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 a 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

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

Learn Default Constructors Of Classes In Modern C++

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

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?