C++Game DevelopmentLanguage FeatureLearn C++

How To Simulate Realistic Physical Effects in C++

Do you want to simulate throwing images, objects or other components on your form when the user interacts with them using their keyboard or mouse? How can simulate the effect of force on objects on the 2D Form window? Can we throw balls in our apps as in sport games? Let’s answer these questions.

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.

close

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.

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
C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

C++Introduction to C++Language FeatureLearn C++

How To Include Or Exclude Trailing Backslashes On Windows

C++Introduction to C++Language FeatureLearn C++

How To Check Characters of File Names and Paths on Windows

C++Introduction to C++Language FeatureLearn C++

How to use SameFileName method on Windows

en_USEnglish