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

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?