Corner effects

Good afternoon, dear reader! This is the second translated article in a series of articles about creating the physics engine by Chris Hecker. If you have not yet familiarized yourself with the first , then I recommend doing this, because everything will immediately become clear. Thank you very much for supporting the first translation: it is very encouraging to work further and more! Enjoy reading!

Physics, Part 2, Angular Effects

I just wanted to back the door with something heavy so that an intruder would not come in. Am I asking a lot? I want his car to roll over and explode in a specific place. I want the huge gears to jam before they flatten me. And I want to hastily build a thing that looks like a swing, in order to catapult a cute glowing gift through the castle's fortress wall. Who can stop me from translating all this into reality? You assume that my opponent is in the game world, but in reality - a programmer of the physical engine, because all of the above is based on the angular effect. You can count on the fingers those games where corner effects are implemented, not to mention finding at least one in which this is done correctly.

The main reason why angular (or otherwise rotational) effects are not implemented in games today is because programmers believe that the physics that describes the rotational motion is too complicated to understand and translate into reality. In physics classes in high school (where we all learned Newton’s second law) they usually don’t talk about rotational effects, and it’s not entirely obvious how to move from the force applied to the object to the rotation of this object. Of course, the dynamics of rotational motion is a little more difficult to understand than the dynamics of linear motion, but it is simpler than it seems. Anyone who can create a physical engine in accordance with the material that is presented in the first article of the cycle will also manage to include the angular effects described in this article. There is hope

Brief repetition

Despite the fact that each of my articles on some unique topic, I always re-read what I wrote earlier, in order to understand where I ended up. I just watched my first article on physics, and I am delighted: we managed to learn so much, and moreover, we never wrote a program code or read additional literature! Before we begin, let's refresh the material from the last article.

Table 1 contains the most important conclusions for the dynamics of solids. It follows from Equation 1 that the coordinate vector (r), the velocity vector (v), and the acceleration vector (a) are related by derivatives (and by integrals, if we read in the reverse order). As a reminder, we mark time differentiation by a prime (r '). r 'is the same as dr / dt, and r' 'is the same as the second time derivative. From Equation 2 it follows that the force is associated with a linear momentum (the product of mass and velocity), mass, and acceleration. The definition of the center of mass can be obtained from Equation 3 (this is the point where all the masses and distances balance each other). Equation 4 states that the total linear momentum of a solid is the sum of all its momenta, which, to our luck, are simply equal to the momentum of the center of mass (CM). Equation 5 is a real gem.

To summarize everything described in the first article: we learned that the total force acting on our center of mass is equal to the sum of all the forces applied to the body (including the force of gravity, the villain’s wagon, a nearby explosion, the thrust of our engine, etc. .). After that, we divided this sum vector by body mass in order to obtain the CM acceleration, and then integrated the time acceleration to obtain the speed and coordinate of the body.

Equation 5 is just a masterpiece! You will see that in it there is no concept of points of application of forces to the body, and this is a key point in determining how the body will rotate under their action. Equation 5 is correct. In fact, it is excellent for finding linear acceleration. We are missing half the battle. But first things first ...

In the first article, rotation was ignored, so we only needed a radius vector and its derivative to describe the configuration of our body in 2D. Now let's add another kinematics value, orientation (denoted by the capital letter omega - Ω), in order to work with corner effects. In order to set Ω, we need to choose the coordinate system relative to the solid and the coordinate system of the game world, and the value of Ω will be equal to the difference in angles between them in radians, as shown in Figure 1.

Figure 1. Definition of Ω

In the figure, the xw, yw axes are the coordinate axes of the game world, and xb, yb are the coordinate axes of the solid. Ω is greater than 0 if counted counterclockwise. It is important here to clarify why we study the dynamics of the two-dimensional world before moving to the three-dimensional: orientation in 2D is a scalar quantity (the angle between coordinate systems in radians), while determining the orientation in the three-dimensional world is much more difficult.

As the body rotates, the value of Ω changes. This change leads us to another kinematics - the angular velocity (denoted by the lowercase letter omega - ω). In contrast to the coordinate and linear velocity of the body, we do not denote the angular velocity as follows - Ω '. However, sometimes we denote the derivative of speed with respect to time, or angular acceleration, as ω '(this is another kinematic value) or as α (lowercase alpha). Do not blame me: I did not come up with all these designations; and in every book that I read there are slight discrepancies. Our angular counterpart for Equation 1 is:

$display \left\{d ^ 2 \Omega \ over \left\{dt ^ 2\right\}\right\} = \left\{d\omega \ over \left\{dt\right\}\right\} = \omega \text{'}= \alpha display$

Equation 6

As in Equation 1, we differentiate ω by time to obtain α; and if we integrate α over time, we obtain ω, etc. All by analogy with the previous article: knowing the angular acceleration of α, we can integrate it twice to obtain a new orientation. But the key point here is to know the value of α.

As you can imagine, our goal for this article is to derive an angular analogue for each of the linear equations in Table 1, and then, taking into account linear and angular equations and the force applied to the object, we can calculate its linear acceleration a and angular acceleration α. Finally, we can numerically integrate these accelerations to find new positions and orientations of our bodies.

To begin with, we will connect linear and angular quantities together. And this is a rather unobvious trick in which angular velocity is used. When calculating in dynamics, we often need to find the speed of an arbitrary point on an object. For example, when we calculate collisions of solids, we need to know the speed of the colliding points in order to understand how much they hit each other. If our bodies do not rotate, the speed of each point of the body is the same. We can simply monitor the speed of the center of mass of the body, and that will be enough. Whereas if our bodies rotate, each point of these bodies can have a different speed. Obviously, we cannot calculate the speed of an infinite number of points of our rigid body, therefore we need a different, better solution.

One of the simple methods that are used to find the linear velocity of any point inside an object uses the angular velocity of the object. Consider the case when the body rotates only around one fixed point O, without changing the coordinate of the body. That is, the body rotates, but does not move. From Equation 7 it follows how to calculate the speed of point B of a rotating body:

$display v ^ B = \omega r ^ \left\{OB\right\} _⟂ display$

Equation 7

We need to clarify a couple of points in Equation 7, let's spend some time on this. First, I use top indexing to show which parameter belongs to these points, so v ^ B is the velocity vector of point B of our body. Similarly, r ^ OB denotes a vector drawn from the center of rotation of our body O to point B. The funny inverted letter T is the “perpendicular operator” that acts on the vector (like the vector r in Equation 7) and rotates it 90 degrees counterclockwise arrows. In other words, a new vector is constructed that is perpendicular to the old. In a two-dimensional world, a vector perpendicular to (x, y) is just (-y, x). You can easily check my words on a graph paper sheet. Let's talk more about this. The magnitude of the perpendicular vector is determined by the angular velocity ω and sets the linear velocity v ^ B. In Russian, Equation 7 shows that the speed of a point of a rotating body is calculated by multiplying the perpendicular vector drawn from the center of rotation by the angular velocity. How do I understand that? Well, I read about it in the book, but it is obvious that such an explanation is not enough, so we prove that this is true.

Let us prove the truth of the conclusions of Equation 7 in two stages. First, we prove that the value of the resulting velocity vector is correct; then - that his direction is correct. For the first part of the proof, we will consider Figure 2.

Figure 2. C = Ωr

Figure 2 shows the rotation of point B by an angle equal to Ω radian along the rotation of a rigid body with a radius vector of length r directed from the center of rotation of the body O to point B. B passed the length of the arc C, where C = Ωr from the definition of radians. (A radian measure of an angle is a measure of an arc bounded by a radius of a circle. The circumference of a circle is C = 2πr because the radian measure of an arc of a circle is 2π [or 360 degrees]).

The speed of a point is a change in its coordinate in time. Therefore, we can find the speed of point B — in other words, the magnitude of the velocity vector — by differentiating the equations of motion with respect to time. C = Ωr is the equation of motion.



The radius is taken out of the differentiation sign, because it is a constant (Point B simply rotates, without linear displacement), and the time derivative Ω is ω from Equation 6. Therefore, the magnitude of the vector B is the velocity vector ωr.

By considering Equation 7, we notice that the magnitude of the velocity vector is correct, since the perpendicular does not affect the length of the vector and r ^ OB is a radius vector directed from O to B. Fuh, we are halfway.

In order to verify that the direction of the velocity vector in Equation 7 is correct, let us begin by making sure that the velocity vector must be perpendicular to the radius vector. This assumption is understandable intuitively, because a point rotating around some other given point can only move perpendicular to the vector between these points. It cannot approach the center of rotation or move away from it, or this movement will simply cease to be rotation. We can support our assumption with calculations for vectors, but I am clamped in a certain framework for the volume of the article, so we will assume that our assumption is correct. (If you are eager to prove this on your own, differentiate the scalar product of a fixed-length vector by itself.)

Finally, we must make sure that the vector is marked correctly, because the figure shows two vectors of equal length perpendicular to the radius: v and -v. Since the value of Ω is measured counterclockwise, ω> 0 when the point rotates clockwise. The perpendicular points in a clockwise direction, as does the radius vector. Figure 3 shows the conclusions from Equation 7:

Figure 3. Relationship between linear velocity and angular

Supplement Equation 7 to describe the rotation of moving bodies. We consider the motion of a solid body as a simple movement of the center of rotation of the body and a simple rotation of the rest of the body around this point. For those who are interested, this is the Schal theorem on the classification of movements.

Chall's theorem divides our motion into two components - linear and angular. Let the center of rotation of the body O be the only moving point, then we use ω to calculate the rotation around the point O, and this gives us the general form of Equation 7:

$display v ^ B = v ^ O + \omega r ^ \left\{OB\right\} _⟂ display$

Equation 9

Equation 9 says that we can calculate the speed of any point of a moving body using the linear speed of the center of rotation of the body and, in addition, the speed acquired by rotating the body.

Reason for our impulse

Now we can write an angular analogue of Equation 2, the equation of force. We start by determining the angular momentum, L ^ AB of a given point B, around another point A:



Equation 10

The moment of momentum of a point differs from the linear momentum of a point in that the angular version of the equation takes into account the position of the body in space. From this it follows that the angular momentum of a point must be measured for another place in the game world, in contrast to the linear impulse, which is determined for a given point (the product of its mass and speed). This is shown through the upper indexation in Equation 10. The notation L ^ AB says that the angular momentum is measured for point B from point A (center of rotation of the body). Imagine an arrow pointing from point A to point B. This arrow is a radius vector between two points, it means r ^ AB. So, the angular momentum of a point is a scalar product of a linear momentum vector and a radius vector perpendicular to it. This operation is called a “perpendicular scalar product”,

If you graphically depict what follows from equation 10, on paper - as I did in Figure 4 - you will see that it determines the measure of how the linear moment of point B “rotates around” A.

Figure 4. Moment of momentum

Everything is correct if the momentum of point B points directly to point A, Equation 10 = 0 (since the perpendicular to r forms a right angle with p, and the scalar product will be 0). The larger the momentum B directed perpendicular to A, the greater the momentum. As can be seen in Figure 4, the scalar product in Equation 10 has as its parameter the cosine of the angle θ enclosed between the perpendicular r ^ AB to p ^ B. If you look at it on the other hand, the magnitude of the scalar product is given by the sine of the angle φ between the original ones that are not perpendicular to r ^ AB and p ^ B (the sine is another key to unraveling the connection between the vector and scalar product). Equation 10 gives a measure of how much of the momentum of point B "looks" in the "rotational direction" relative to point A.

Also, as we used the derivative of the linear momentum to determine the force, we will use the derivative of the angular momentum to determine the angular twin of the force - the moment of force (indicated by the lowercase letter tau - τ).



Equation 11

To save space, I cheated a little in Equation 11, having skipped a couple of difficult steps, which include finding derivatives. From the foregoing, it follows that the moment of force is associated with the force at a certain point through a scalar product.

Finally, we got a dynamic equation that uses the point of application of force, which was previously ignored in the linear momentum equations. Equation 11 uses a scalar product with a perpendicular as a measure of how much of the force applied to point B rotates around point A; this “rotational force” is called the moment of force. Equation 11 allows you to find the numerical value of the moment of force - and hence the angular momentum, if we integrate the moment of force, knowing the applied force and the point of application.

However, we still have not received the equation of the relationship between the moment of force and the angular magnitudes of the kinematics that we need to rotate the object around its axis - such as angular acceleration, angular velocity or orientation. Therefore, we cannot move forward before we derive a few more equations.

The moment we've all been waiting for

Before we make a connection between the dynamics and kinematics, we need to determine the total angular momentum, by analogy with how we determined the total linear momentum in Equation 4. I have not forgotten about the angular equivalent for the center of mass in Equation 3; we will see it in the equation of total angular momentum.

The total angular momentum near point A is denoted by L ^ AT and is determined by Equation 12:



Equation 12

Equation 12 is the sum of all the angular momenta of all points of the body, measured relative to point A. On the right side, I used the definition of linear momentum in order to represent p ^ i as the product of mass and velocity (mv). This will be useful to me in the future in order to make from Equation 12 something with a more pronounced applied character. The equation says that to find the total angular momentum for our object, it is necessary to sum the angular momentum of all its points. For a solid body consisting of faces (and not from separate points), it is necessary to calculate the integral, for a non-discrete sum.

Fortunately, we can simplify our calculations by introducing a new quantity called the “moment of inertia”, similar to how we introduced the center of mass to simplify the total momentum equation. Recall that, thanks to Equation 7, we can find the speed of a point through the angular velocity. Let point A in Equation 12 be the center of rotation from Equation 7, and the summation index i in Equation 12 be point B from Equation 7, then it is possible to convert Equation 7 to Equation 12. We get:

$display L ^ \left\{AT\right\} = \ sum _ \left\{i\right\} \left\{\ vec r ^ \left\{Ai\right\} \ times m ^ i \omega \ vec r ^ \left\{Ai\right\}\right\} = \omega \left\{\ sum _ \left\{i\right\} \left\{m ^ i \ vec r ^ \left\{Ai\right\} \ times \ vec r ^ \left\{Ai\right\}\right\}\right\} = \omega \ sum_ \left\{i\right\} \left\{m ^ i \left(\ vec r ^ \left\{Ai\right\}\right) ^ 2\right\} = \omega I ^ A display$

Equation 13 I will

write down Equation 13 in more detail, step by step. First, we replace Equation 7 with 12 in order to get the sum in Equation 13. This replacement allows us to describe the angular momentum using angular velocity. Next, we take ω for the sum, because it is equal for all points of the solid (the angular velocity is determined for the whole body, and not for each point separately), and we write the mass with index i on the left, in order to more clearly see the scalar product radius -vectors for themselves. This scalar product is equal to the square of the length of the radius vector (the scalar product of any vector by itself = the square of its length. Remember that the perpendicular operator does not change the length of the vector.). Finally, we write I ^ A in order to denote the moment of inertia around point A.

The moment of inertia for a two-dimensional solid is an extremely good number, because the points that make up the body cannot change their mass or move away from the center of rotation. As a consequence of these two properties, the sum in Equation 13 is a constant for each body, so we can calculate it. Speaking in Russian, I ^ A is the sum of the distances squared from point A to all other points of the body, and each such distance is scaled in accordance with the mass of each point. Just like for the center of mass - if the body is more solid than composed of discrete points, then the sums will turn into integrals. And the moment of inertia will remain the same in magnitude and will have the same physical meaning.

Determining the moment of inertia near a point is verbose, but think of I ^ A as the measure of how hard it is to turn the body around point A. For example, imagine a pencil (flat pencil). If we measure the moment of inertia near the center of the pencil, we get a certain value equal to the sum of the squares of the distances scaled in accordance with the mass of each point. Whereas if we measure the inertia at the tip of the same pencil, we get a larger value, because the further the points with mass are removed, the greater the value of the squares of their distances. Here we will formulate mathematically what is intuitive to us: rotating a pencil around its center is much easier (read it takes less effort) than rotating it around one of the ends.

Finally, we are ready to draw a connection between the angular equations of dynamics and the angular equations of kinematics. If we differentiate Equation 13, we get the total moment of force on the left, and the product of the moment of inertia and angular acceleration on the right (I ^ A is a constant, we put it outside the sign of differentiation):

$display \tau ^ \left\{AT\right\} = \left\{dL ^ \left\{AT\right\} \ over dt\right\} = \left\{d \left(I ^ A \omega \right) \ over dt\right\} = I ^ A \omega = I ^ A \alpha display$

Equation 14

This equation is the angular equivalent of Equation 5; in fact, it is F = ma for angular dynamics. This is the equation of the connection of the total moment of force and the angular acceleration of the body through the scalar moment of inertia. If we know the moment of force exerted on our body, we can find its angular acceleration, and then the angular velocity and orientation in space through integration - dividing the moment of force by the moment of inertia.

Dynamics algorithm

He hardly sees us through this whirlwind of equations, but all of them are its integral part. We have derived enough equations to obtain the magnificent dynamics of a two-dimensional world with arbitrarily given forces and moments of forces moving and rotating our objects. How to use these equations? Below is the basic algorithm:

1. Find the value of the center of mass and the moment of inertia in the center of mass.
2. Set the initial coordinates of the body, its orientation in space, its linear and angular velocities.
3. Take into account all the forces acting on the body and the points of their application.
4. Find the resultant of all forces and divide it by the mass of the body in order to find the linear acceleration of the center of mass (Equation 5).
5. For each force, construct a scalar product with a perpendicular between the vector directed from the center of mass to the point of force application and the vector of applied force, add this quantity to the total moment of force in the center of mass equation (Equation 11).
6. Find the quotient for the total moment of force and moment of inertia in the center of mass to find the angular acceleration (Equation 14).
7. Numerically integrate linear acceleration and angular acceleration to update the coordinate, linear velocity, spatial orientation and angular velocity (see the last article).
8. Draw the object in the resulting coordinate, and go to Step 3.

In the algorithm above, there are only two steps that I have not explained. First, how to calculate the moment of inertia in Step 1 for a solid object? Secondly, how to solve the problem with the forces from Step 3? The answer to the first question can be found in a simple code example, which I will leave in the application at the end of this article (you will integrate the object by its area). Many books on dynamics contain the calculated moment of inertia for frequently encountered forms of objects in the application at the very end, so you do not have to display them yourself each time.

The answer to the question of how to calculate the forces from Step 3 depends on the application, but I will give some general recommendations. Firstly, forces such as gravity, always directed in one direction (down, in the case of gravity), do not create a moment of force, because they pull all the points at the same time in the same direction, although we we apply these forces directly to the center of mass. Forces similar to the elastic force are applied to a certain point of the object, they will create a moment of force, therefore we consider them in the general case. As we saw in the first article, the force of friction is the same force directed in the direction opposite to the speed of the body.

You can make a simple physical model demonstrating the force of friction, and just apply force to the center of mass, or you can choose which parts of the object will apply friction forces, and do this, which can create a moment of force acting on the object. The forces that bodies experience in collisions are a little more difficult, and we will get to know them in the next article. Forces like a thrust of a rocket engine should be considered as forces with an application point (in this case, if one of the engines fails, you will begin to spin around its axis until you adjust the steering wheel to balance the moment of force!). If you want something similar to gravitational rays from a UFO, then this force should be calculated as the force of gravity and not create a moment of force, or it should be applied to a specific point on the object, and will it revolve around this point while rising up? The choice is yours. The key point is not to be afraid to experiment with different forces calculated in different ways, because now you have a real simulator of two-dimensional graphics, try different types of forces!

I left all the code and links you need on my website because there is no more space left. In my simple application, I implemented the algorithm of the dynamics of a two-dimensional world, and also added objects fastened by a spring; they rotate around their axis, and sometimes even collide with walls, spin. But I’ll talk about this another time. Follow the link for more literature and a simple application for Windows 32 and Macintosh.

Very rarely, Chris Hecker experiences the effect of moment of inertia, but usually it passes quite quickly. Forces can be applied to checker@bix.com.

Translator's notes:  a pun is presented here, the topic of the article and its content are played out.

PS Feedback is welcome. Your comments can improve the quality of work. Thanks!

PPS The author of the translation expresses special thanks to the users of berez and Vasily Tereshkov for editing the translation. Thanks!