In this post, you’ll learn what kinematics is, how to use it in programming, draw kinematic drawings in C++, and how to simulate kinematic animations in a C++ app.
Table of Contents
What does Kinematics mean?
Kinematics is a subfield of physics; it describes the motion of points, rigid bodies, and systems of these rigid body groups without considering the forces that cause them to move. Kinematics developed in classical mechanics, which is often referred to as the “geometry of motion” and is occasionally seen as a branch of mathematics. A kinematics problem begins by describing the geometry of the system and declaring the initial conditions of any known values of position, velocity, and/or acceleration of points within the system. Then, using arguments from geometry, the position, velocity, and acceleration of any unknown parts of the system can be determined. The study of how forces act on bodies falls within kinetics, not kinematics. This is also the area of analytical dynamics.
Kinematics is one of the important parts of engineering to analyze current systems, generally used in mechanical engineering applications, electric and electronic engineering applications, mechatronic applications, aerospace mechanics; rarely, architects need to analyze these kinds of systems in their designs. The most needed areas are; robotics, industrial machines, electro-mechanic parts, motion capture and analysis in games, many different fields of sports, art, movie, and tv series sector.
Before the advent of widely available and affordable computers, engineers were calculating these details on big A3, A2, A1 papers. In the last 30 years, it has become very easy to simulate and analyze these kinds of data by programming and doing simulations. Once understood, it’s easy to analyze and focus on each part and easy to see the changes in modifications. Sometimes we analyze this by using equations and by the results of the equations; sometimes we capture data from a real kinetic object, for example a dancer, working robot, or through analysis of real-world objects such as an old mechanical dam gate,
Why is C++ Builder a perfect choice for Kinematics?
C++ is one of the faster and native programming languages and that means you can maximize the use of CPU, GPU, and RAM at their most optimum level. We can analyze calculations in real-time faster, we can analyze a single object and we can simulate many other kinematic variations together. For example, we can capture data from a dancer’s movement, and then we can analyze the position, velocity, and acceleration of each point, we can use some equations and parameters and we can simulate hundreds of dancers in an application.
C++ Builder is a modern way to develop these kinds of analytical applications. You can do this on both VCL and FMX applications. In this post, let’s develop a Windows VCL application.
How can I analyze Kinematics in my own apps?
Kinematics is a subfield of physics, meanwhile a field in engineering and needs mathematics to analyze these kinds of problems. For example, here are some of the equations for a 4-joint mechanism.
If we know well which equations are needed for a system then we can write a program to simulate these kinds of problems in applications. In C++ we can use arrays, structs, vectors, and classes to hold data obtained from the equations. Simply, In C++ we can use float or double types to define the position of the joint, length of arms, velocity, angular velocity, speed, the angular speed of the joint, of a system, or parts of the system.
Generally to display a simulation of the movement we just need the positions of the joints in 2D (X, Y) or 3D (X, Y, Z) and the length of arms (here we used R). Maybe we need which 2 joints are connected. We can obtain some data from the components (i.e. arm lengths from the Edit1, Edit2, Edit3, etc.)
We can define 2D points and R lengths of arms as below,
1 2 3 4 5 6 7 8 9 |
double X[10],Y[10],R[10]; calculate_kinematics() { // equations here // calculate and define the exact position of each joint } |
How can I visualize Kinematic motions data in C++ Builder?
In C++ Builder, we can visualize kinematic motions in many different ways. Generally, in VCL applications, we have the PaintBox to draw these kinds of lines. For example, we can draw arms of a 4-joint kinematic system as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void draw_kinematics() { // Set the Brush and Pen Colors PaintBox1->Canvas->Brush->Color=clBlack; PaintBox1->Canvas->Pen->Color = clWhite; // Clear the PaintBox1 PaintBox1->Canvas->Rectangle(0,0,PaintBox1->Width, PaintBox1->Height); // Move pixel Cursor to Start Position PaintBox1->Canvas->MoveTo(X[0],Y[0]); // Draw Arm Parts in Order for(int i=0; i<=3; i++) { PaintBox1->Canvas->LineTo(X[i],Y[i]); } } |
Another method can be drawing into bitmap and displaying this bitmap. Personally I like to draw into bitmaps, it’s easy to manipulate pixels and can be stretched, the user can zoom in out, they can be used in other forms, and can be saved. If you need 3D simulations you can use OpenGL or easily you can use Viewport3D.
How can I simulate Kinematic motions in C++ Builder?
If we run the above drawing procedure in a timer, with the TTimer component, we can draw and simulate this with given intervals, for example every 30 ms, as a parameter of the TTimer component. The OnTimer event in that example would look like the following:
1 2 3 4 5 6 7 |
void __fastcall TForm1::Timer1Timer(TObject *Sender) { calculate_kinematics(); draw_kinematics(); } |
This is a very simple example that simulates kinematic motion in C++,
We can add a TTrackbar to control the speed of the animation, or we can add some check boxes to see reruns of previous drawings. As you can see, using C++ Builder makes it very easy to visualize kinematic data. We can simply obtain data from sensors or from a database and then do calculations if we need to define positions and then draw them.
Here is a screenshot of the final kinematic result on runtime when the cleaning line trails is unchecked.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version 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 for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.