The second law can also be described with the famous formula:F = ma or a=F m The force equals the mass of an object times its acceleration.. However, more often you are interested in th
Trang 1How is the difference in size between actors and models handled?
JN: Basically, there are no limits there at all We have even had a human actor do the
motions for a T-Rex once You will, of course, have to make adjustments if you record
a very small person and use the recorded motions for a large game character Often small people and large people move completely different Another funny thing is that you can really see from the motion whether the actor is a woman or a man One time
we tried to capture motion for a 3D character, a woman, and I was acting That’s actually somewhat of an inside joke here now An outsider came to look at the final animation and he immediately said that it has got to be a man.
JM: That’s the cool accuracy of this system If you know the actor personally, you can
see that it is him or her just from watching the motion capture data There’s more emotion in movements than you might think, and you can really see the difference when you compare motion capture animation to traditional artist created animation.
FIGURE 5.9
Mocap with actor jumping.
Trang 2In this chapter you learned about some more advanced topics of character animation You learned about animation blending, which is something you’ll definitely need if you ever aim to create a realistic character With it, you can create new animations by blending two others together You can also create a transition between two animations by blending between them Another advanced topic covered in this chapter was animation callbacks, with which you can time events in your game to when a specific animation occurs If you made a fighting game for instance, you might want to time the “punch” sound to a specific time
in the punch animation Lastly, the topic of motion capture and its variations were briefly covered That about wraps up the skeletal animation part of this book, which the last three chapters have focused on Now it’s time to turn to dynamic animation and look at implementing a ragdoll system
CHAPTER 5 EXERCISES
Try modifying Example 5.1 so that you run the same animation in two different tracks, with different weights and slightly different speeds See what happens? Create animation callbacks for the footsteps in the Soldier’s walk animation
FURTHER READING
[1] http://www.organicmotion.com [2] http://www.mova.com
[3] http://www.moven.com [4] http://www.vicon.com
Trang 4Physics Primer
6
As is common practice with most programming books that include only one chapter covering physics, I’ll start this one with a disclaimer This book is not about physics! There are books focusing solely on the topic of game physics However, since I will be covering ragdoll animations in this book, I need cover the “bare bones,” so to speak,
of creating a physics engine The physics engine demonstrated in this chapter builds
on simulating particles connected with springs In this chapter you’ll learn about the following topics:
Trang 5Basics of rigid body physics Quaternions
Oriented bounding boxes Intersection tests
Simulating a particle Simulating a spring
In the beginning of this chapter, many basic physical concepts will be covered If you are already comfortable with Newton’s three laws of motion, gravity, quaternions, etc., feel free to skip ahead to the implementation part at the end of this chapter.
INTRODUCTION TORIGID BODY PHYSICS
If you’ve never come across the topic of rigid bodies before, don’t worry; it is quite easy to understand (even though it can be quite hard to simulate) A rigid body is by definition a solid mass in space that does not change shape A real-life basketball is
an example of a non-rigid body The fact that the basketball is not rigid is what causes it to bounce once it hits the floor Instead, imagine a sphere the size of a basketball in solid steel hitting a steel floor Very likely there wouldn’t be a very large bounce from a collision like this This is because the steel sphere won’t change its shape It is rigid!
In computer graphics there are accurate mathematical systems where rigid bodies are simulated colliding with the environment and with each other In games, however, this simulation has to be able to run in real time This essentially means several shortcuts need to be taken, and some serious optimizations must be done in order to get it fast enough for real-time applications So when making physics engines for games, things like speed, stability, and appearance take prece-dence over accuracy and realism
Before I dive into rigid body physics, I’ll cover the basics of physics in general Over 300 years ago a fellow named Isaac Newton published a three-volume work
called Philosophiae Naturalis Principia Mathematica, or Principia for short In
plain English, the title was “Mathematical Principles of Natural Philosophy” and contained, as you may already know, Newton’s three laws of motion The first of
the three volumes was called De motu corporum (“On the motion of bodies”), and
now, more than 300 years later, these laws are still used when simulating physics
in games Table 6.1 shows a simplified version of Newton’s laws of motion
Trang 6The second law can also be described with the famous formula:
F = ma or a=F
m
The force equals the mass of an object times its acceleration However, more often you are interested in the acceleration resulting from an external force, in which
case the acceleration a is the force F divided by an object’s mass m Later on I’ll
dis-cuss how the acceleration of an object affects its velocity and its position But first it
is time to cover some important concepts needed to create your own physics engine
F ORCES
As you may have seen in Newton’s three laws, there was a lot of talk about forces
A force has both a magnitude and a direction Imagine, for example, two equally strong men pushing a box from opposite sides Since the directions of their efforts are opposing, the box won’t move an inch However, if the two men were pushing from the same side, the two forces would combine and the box would move in the direction they are pushing This little thought-experiment proves the fact that two opposing forces cancel each other out
The most common force you face on a daily basis is gravity (unless you happen
to be an astronaut) In his Principia, Newton also defined the law of gravity:
F G = G m 1 m 2
d 2
F Gis the resulting gravitational force, G is the universal gravitational constant
[Wikipedia], and m 1 and m 2are the masses of the two objects attracting each other
Finally, d is the distance between the objects Simply put: Two objects attract each
other with a force proportional to the product of their masses divided by the square
of the distance between them (quite a mouthful) Take the simple example of the Sun and the Earth, as shown in Figure 6.1
TABLE 6.1 NEWTON’S LAWS OF MOTION
1 st Law: An object’s velocity will not change unless affected by an external force.
2 nd Law: The acceleration of an object is proportional to the magnitude of the force acting
on the object and inversely proportional to its mass.
3 rd Law: Every action has an equal and opposite reaction.
Trang 7It might be hard to see from the numbers representing the mass of the Sun and the Earth But the Sun has 333,000 times more mass than the Earth and it represents 98% of all the mass in our solar system This means that whatever the gravitational force is between the Sun and the Earth, 99.9997% of that force affects the earth, and 0.0003% of it affects the sun This gravitational force is the only thing keeping the Earth in orbit around the Sun
Now turn your attention to the Earth itself, where there’s a similar gravitational force affecting all smaller objects on the Earth’s surface (for instance, an apple) Just like in the example of the Sun and the Earth, each small object actually also attracts the Earth toward it However, this force is so small that it is negligible This leaves
us with one force pulling all objects toward the Earth’s surface
In games, this is almost always represented as a constant force in the negative
Y direction (and the curvature of the Earth is completely dismissed) Just as in real life, games try to simulate the Earth’s gravitational pull (9.8 m/s2), making objects behave as realistically as possible Later on I’ll cover how we apply gravity to game objects, updating their velocity and position over time
That pretty much covers the gravitational force However, there are plenty of other non-constant forces in the world, such as wind, collision impacts, etc.—for example, in an action game when a bullet hits an object, the bullet will affect the object with a force proportional to its speed and mass This brings us to the different ways a force can affect an object
T HE E FFECT OF F ORCES ON A R IGID B ODY
Okay, so you know that there are different forces affecting an object (i.e., a rigid body) The next thing to cover is how these bodies react when affected by an external force First, consider what happens to a rigid body’s position when you push or pull
it with an external force Figure 6.2 shows an example
FIGURE 6.1
Gravitational pull between the Earth and the Sun.
Trang 8As you can see in Figure 6.2A, the body is being affected by two external forces pushing at the object from different directions Just like with the analogy of the two men pushing a box, the rigid body will move over time in the combined direction of the forces as long as the forces are applied In this example you can see the center of the rigid body’s mass represented as a cross The forces in Figure 6.2 are pointing straight at the object’s center of mass, resulting in the energy being used 100% to move the object However, this is of course not always the case; sometimes forces are applied to an object causing it to spin, rather than to move linearly
Try it yourself Find a small object (like a pen) and poke it close to its center of mass No doubt the object will move in the direction you poked it Try it again, but this time poke it far from its center of gravity This time the object will spin instead This phenomenon is shown Figure 6.3
FIGURE 6.2
How a force affects the position of a rigid body.
FIGURE 6.3
How a force affects the orientation of a rigid body.
Trang 9Just like in the previous case, when there are several forces affecting the orien-tation of an object, the forces are all summed up before applying the final roorien-tation
of the object So far I have only been talking about objects in 2D space, but the same fundamental reactions naturally occur with objects in 3D space as well To describe the orientation of an object in a physics simulation, you basically have three different options:
Euler angles Rotation matrix Quaternions
If you have covered the basics of 3D math, you have probably come in contact with Euler angles Euler angles are easy to understand; you have one rotation value for the yaw, pitch, and roll of an object Even though the Euler angles are easy to grasp, they come with some limitations such as Gimbal lock (covered in Chapter 4) Another option is to use a rotation transformation matrix Although this is an often used option, it suffers from the fact that small imperfections creep in (due to rounding of float values) and the matrix becomes skewed over time This pretty much leaves us with quaternions!
Q UATERNIONS
Before continuing with the implementation of the physics engine, and later the ragdoll system, you need to understand the concept of quaternions Quaternions are stored as four values; the first value is a scalar value, and the three following values describe a vector Since a quaternion is a four-dimensional entity, it’s close
to impossible to create a mental image of it Instead, the best approach for any non-mathematician is just to learn how to create an arbitrary orientation using quaternions, and then get more comfortable with them by using them in your
code The word quaternion comes from Latin’s “Quaternio,” which means “set of
four.” A quaternion is defined as the following four values:
q = [w, x, y, z]
Quaternions were invented by an Irish mathematician named Sir William Hamilton some 200 years ago His motivation was to come up with a method to describe the transformation required to transform a vector V1 into a vector V2 In the case of two points A and B, there exists a vector V that transforms point A into point B, as shown in Figure 6.4
Trang 10A quaternion performs the same operation but on two vectors A quaternion can transform one specific vector into another specific vector Usually a vector is described as its x, y, and z components, but a vector can also be described with a direction and a length Here’s the general theory of how you can transform one vector into another when they are described as a direction and a length:
Figure 6.5 shows the two vectors V1 and V2 The first step of changing V1 into V2
is to make sure their length (or magnitude) is the same For this you simply ignore their orientation and calculate the scale factor S you need to apply to V1 so that its length matches that of V2
S = 兩V 2兩
兩V 1兩 The scale factor S, is calculated by simply dividing the length of vector V2 with
the length of vector V1 Next you need a way of transforming a direction into
another, and for this you need to use versors A versor describes the difference in
orientation of two vectors of equal length, as shown in Figure 6.6
FIGURE 6.4
Transforming one point into another.
FIGURE 6.5
Vectors V1 and V2 differ both in magnitude and orientation.
Trang 11All you need to reorient V1 into V2 is the angle between the vectors and the axis around which to rotate The angle is obtained by calculating the inverse cosine value of the vectors’ dot product:
a = a cos(V1•V2)
The axis around which the rotation should take place is simply the cross product
of the vectors:
A = V1 V2
The quaternion that combines the scaling and rotating of V1 into V2 can then
be created using one value for scale, one for the angle, and two values for the plane
on which V1 and V2 lie When I talk about orientations and rotations using quaternions throughout this book, only a small subset of all possible quaternions are of particular interest This subset is all the unit quaternions (i.e., quaternions with a length of 1) All possible unit quaternions form a hyper-sphere, which basically is a four-dimensional sphere (something quite hard to visualize)
Here’s a practical problem—one which you will no doubt run into once you switch to quaternions Have a look at the problem shown in Figure 6.7
FIGURE 6.6
Transforming the direction of a vector to another.
Trang 12In Figure 6.7, a rotation of -radians around Axis A is described This rotation
will transform the vector p1 into p2 Remember that the axis around which you
want to rotate can be any arbitrary axis Here’s how you can create a quaternion to define this rotation Remember that quaternions were defined as:
q = [w, x, y, z]
In the example of rotating -radians around Axis A (x, y, z), the quaternion for this would be:
cox(a/2),
q = x•sin(a/2),
y•sin(a/2), z*sin(a/2)
In DirectX, a quaternion is stored using the D3DXQUATERNIONstructure:
struct D3DXQUATERNION { FLOAT x;
FLOAT y;
FLOAT z;
FLOAT w;
};
FIGURE 6.7
Transforming the unit vector p1 into p2 using quaternions.