C++Game DevelopmentLanguage FeatureLearn C++

How To Simulate Realistic Physical Effects in C++

In this post, you’ll learn how to simulate throwing images, objects, or other components on your form when the user interacts with them via keyboard or mouse; how to simulate the effect of force on objects in the 2D Form window, and how to throw balls in our apps like in sports games. By learning to simulate the realistic physical effects in c++, it will help you easily build C++ applications with the use of C++ IDE.

Mice, touch and interaction

First, in normal hardware like mouse and touch panels etc. there is no sensor to measure forces. there is no way to measure force on mouse button, or force on touchpad. Some new touchpads recognize the force but exactly unable to measure real force. So, how we can emulate forcing as in sport games?

There are three ways to simulate forcing. First method is measuring the distance between the left mouse button pressed and up. The second method is measuring the time pressed between when the mouse down and up. Third one is using both, measuring the distance and time pressed. So we can simulate a force by the difference in each method. For example, if user press long, applies higher force, or if he press and move mouse in long distance he can apply higher force or or vice versa, etc. We can not measure the real force, but we can simulate and let the user feel like he or she apply a force to an object.

Force effect simulation explained

Let’s try to explain the first way to simulate. In this method we will measuring the distance between the mouse button is pressed and mouse button released.

How To Simulate Realistic Physical Effects in C++ diagram

Let’s add some global variables to our project, LX and LY will be used to record last X and Y coordinate of mouse when pressed, and we will use Fx and Fy variables to calculate forces in X and Y directions.

Adding some code to the mouse events

Now select the Form (i.e Form1), go to its events in Object Inspector, double click to OnMouseDown(), OnMouseMove() and OnMOuseUp() events. This will create automatically these FormMouseDown(), FormMouseMove(), FormMouseUp() event methods. Change each method inside as below to store last X and Y mous position and calculate Fx and Fy force by the difference between X and Y mouse directions. Please change codes as below,

Moving an image with force effects

Finally we can move this image with these forces.

You can add friction, gravity and extra accelerations to your objects. Now it is your time, Imagine what you can do!


You can download a trial copy of RAD Studio C++ Builder for free today by clicking here.

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

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?