C++Builder How To Simulate Realistic Physical Effects in C++

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Simulate Realistic Physical Effects in C++
By Yilmaz Yoru August 5, 2021

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.
1628232534631.png
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.
C++:
//---------------------------------------------------------------------------
 
#include <fmx.h>
#pragma hdrstop
 
#include "Forcing_2D_Objects_Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
 
float LX, LY;
float Fx, Fy;
 
bool forcing = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
 
}

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,
C++:
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, float X, float Y)
{
 // let's record last X and Y coordinate when mouse is pressed
 LX = X;
 LY = Y;
 
 forcing = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, float X, float Y)
{
 //
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, float X, float Y)
{
 float k = 0.1;
 
 Fx = k*(X-LX);
 Fy = k*(Y-LY);
 
 forcing= false;
}

Moving an image with force effects​

Finally we can move this image with these forces.
C++:
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
 Image1->Position->Point = { Image1->Position->X+Fx, Image1->Position->Y+Fy };
}
You can add friction, gravity and extra accelerations to your objects. Now it is your time, Imagine what you can do!