![pexelscottonbro5275668v2 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2021/06/pexels-cottonbro-5275668v2-scaled.jpg?resize=1140%2C694&ssl=1)
![pexelscottonbro5275668v2 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2021/06/pexels-cottonbro-5275668v2-scaled.jpg?resize=1140%2C694&ssl=1)
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.
![CBUIDERSTUDIOFINALICONS64 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/11/CBUIDER_STUDIO_FINAL_ICONS_64.png?resize=64%2C64)
![CBUIDERSTUDIOFINALICONS64 | Learn C++](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2020/11/CBUIDER_STUDIO_FINAL_ICONS_64.png?resize=64%2C64)
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.
Table of Contents
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.
![ballphysics | Learn C++ Learn To Simulate Realistic 2D Ball Physics in C++ Builder diagram of ball physics](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2021/06/ballphysics-1024x506.png?resize=750%2C371&ssl=1)
![ballphysics | Learn C++ Learn To Simulate Realistic 2D Ball Physics in C++ Builder diagram of ball physics](https://i0.wp.com/learncplusplus.org/wp-content/uploads/2021/06/ballphysics-1024x506.png?resize=750%2C371&ssl=1)
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Tball { public: TImage *image; float x,y, vx,vy, ax, ay; int W,H; Tball() // Constructor { image = new TImage(Form1); }; ~Tball() //Deconstructor { image->Free(); } }; |
Let’s define balls
1 2 3 4 5 6 7 |
#define NUMBER_OF_BALLS 10 Tball ball[NUMBER_OF_BALLS]; unsigned long int mseconds=0; float g = 9.81; |
We can set all balls in our form creation as below;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { srand(time(0)); for(int i=0; i<NUMBER_OF_BALLS; i++) { ball[i].image->Bitmap->Assign(ball_image1->Bitmap); ball[i].image->Width =ball_image1->Width; ball[i].image->Height =ball_image1->Height; ball[i].image->Visible = true; ball[i].x = rand()%800; ball[i].y = rand()%800; ball[i].vx = (rand()%100)/10.0; ball[i].vy = (rand()%100)/10.0; } } |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
void __fastcall TForm1::Timer1Timer(TObject *Sender) { mseconds++; float t=mseconds/1000; for(int i=0; i<NUMBER_OF_BALLS; i++) { if(ball[i].y>Rectangle1->Height-ball[i].H-ball[i].vy) { ball[i].vy=fabs(ball[i].vy)*(-0.8); ball[i].vx*=0.98; } if(ball[i].x>Rectangle1->Width- ball[i].W-ball[i].vx) { ball[i].vx=fabs(ball[i].vx)*(-0.8); } if(ball[i].x<ball[i].vx) { ball[i].vx=fabs(ball[i].vx)*(0.8); } ball[i].vy=ball[i].vy+g*0.06; ball[i].x += ball[i].vx; ball[i].y += ball[i].vy; ball[i].x=Min((float)Rectangle1->Width-ball[i].W, ball[i].x+ball[i].vx); ball[i].y=Min((float)Rectangle1->Height-ball[i].H, ball[i].y+ball[i].vy); ball[i].image->Position->X=ball[i].x; ball[i].image->Position->Y=ball[i].y; } } |
Simulating ball movement every millisecond in C++
Timer to simulate every millisecond,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
void __fastcall TForm1::Timer1Timer(TObject *Sender) { mseconds++; float t=mseconds/1000; for(int i=0; i<NUMBER_OF_BALLS; i++) { if(ball[i].y>Rectangle1->Height-ball[i].H-ball[i].vy) { ball[i].vy=fabs(ball[i].vy)*(-0.8); ball[i].vx*=0.98; } if(ball[i].x>Rectangle1->Width- ball[i].W-ball[i].vx) { ball[i].vx=fabs(ball[i].vx)*(-0.8); } if(ball[i].x<ball[i].vx) { ball[i].vx=fabs(ball[i].vx)*(0.8); } ball[i].vy=ball[i].vy+g*0.06; ball[i].x += ball[i].vx; ball[i].y += ball[i].vy; ball[i].x=Min((float)Rectangle1->Width-ball[i].W, ball[i].x+ball[i].vx); ball[i].y=Min((float)Rectangle1->Height-ball[i].H, ball[i].y+ball[i].vy); ball[i].image->Position->X=ball[i].x; ball[i].image->Position->Y=ball[i].y; } } |
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?
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition