# Character Animation with Direct3D- P8

Chia sẻ: Cong Thanh | Ngày: | Loại File: PDF | Số trang:20

0
62
lượt xem
4

## Character Animation with Direct3D- P8

Mô tả tài liệu

Character Animation with Direct3D- P8:This book is primarily aimed at teaching indie and hobby game developers how to create character animation with Direct3D. Also, the seasoned professional game developer may find some interesting things in this book. You will need a solid understanding of the C++ programming language as well as general object-oriented programming skills.

Chủ đề:

Bình luận(0)

Lưu

## Nội dung Text: Character Animation with Direct3D- P8

1. 126 Character Animation with Direct3D That’s pretty much all you need for a lightweight physics simulation. Note that there are light-years between a physics engine like this and a proper engine. For example, this “engine” doesn’t handle things like object–object collision, etc. Next, you’ll learn how the position of an object is updated, and after that the first physics object (the particle) will be implemented. POSITION, VELOCITY, AND ACCELERATION Next I’ll cover the three most basic physical properties of a rigid body: its position p, its velocity v, and its acceleration a. In 3D space, the position of an object is described with x, y, and z coordinates. The velocity of an object is simply how the position is changing over time. In just the same way, the acceleration of an object is how the velocity changes over time. The formulas for these behaviors are listed below. pn = p + v • t where pn is the new position. vn = v + a • t where vn is the new velocity. f a= m In the case of a 3D object, these are all described as a 3D vector with an x, y, and z component. The following code shows an example class that has the m_position, m_velocity, and the m_acceleration vector. This class implements the formulas above using a constant acceleration: class OBJECT{ public: OBJECT() { m_position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); m_velocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f); m_acceleration = D3DXVECTOR3(0.0f, 1.0f, 0.0f); } void Update(float deltaTime) { Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
2. Chapter 6 Physics Primer 127 m_velocity += m_acceleration * deltaTime; m_position += m_velocity * deltaTime; } private: D3DXVECTOR3 m_position; D3DXVECTOR3 m_velocity; D3DXVECTOR3 m_acceleration; }; As you can see, the m_position and the m_velocity vectors are initialized to zero, while the m_acceleration vector is pointing in the Y-direction with a constant magnitude of one. Figure 6.11 shows a graph of how the acceleration, velocity, and position of an object like this change over time. FIGURE 6.11 Acceleration, velocity, and position over time. In this section you have looked at how an object’s position is affected by velocity, which in turn is affected by its acceleration. In the same manner, an object’s orientation is affected by its angular velocity, which in turn is affected by its torque. However, since you won’t need the concepts of angular velocity and torque to create a simple ragdoll animation, I won’t dive into the detailed math of these. If you want to look into the specifics of angular velocity and torque, I suggest reading the article “Integrating the Equations of Rigid Body Motion” by Miguel Gomez [Gomez00]. THE PARTICLE These days, physicists use billions of volts to try and accelerate tiny electrically charged particles and collide them with each other. What you’re about to engage in is, thankfully, much simpler than that (and also a lot less expensive). You’ll be looking at the smallest (and simplest) entity you can simulate in a physics engine: the particle! ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
3. 128 Character Animation with Direct3D A particle can have a mass, but it does not have a volume and therefore it doesn’t have an orientation either. This makes it a perfect physical entity for us beginners to start with. The particle system I am about to cover here is based on the article “Advanced Character Physics” by Thomas Jakobsen [Jakobsen03]. Instead of storing the velocity of a particle as a vector, you can also store it using an object’s current position and its previous position. This is called Verlet integration and works like this: pn = 2pc - po + a t po = pc pn is the new position, pc is the current position, and po is the previous position of an object. After you calculate the new position of an object, you assign the previous position to the current. As you can see, there’s no velocity in this formula since this is always implied using the difference between the current and the old position. In code, this can be implemented like this: D3DXVECTOR3 temp = m_pos; m_pos += (m_pos - m_oldPos) + m_acceleration * deltaTime * deltaTime; m_oldPos = temp; In this code snippet, m_pos is the current position of the particle, m_oldPos is the old position, and m_acceleration is the acceleration of the particle. This Verlet inte- gration also requires that the deltaTime is fixed (i.e., not changing between updates). Although this method of updating an object may seem more complicated than the one covered previously, it does have some clear advantages. Most important of these advantages is that it makes it easier to build a stable physics simulation. This is due to the fact that if a particle suddenly were to collide with a wall and stop, its velocity would also become updated (i.e., set to zero), and the particle’s velocity would no longer point in the direction of the wall. The following code shows the PARTICLE class. As you can see, it extends the PHYSICS_OBJECT base class and can therefore be simulated by the PHYSICS_ENGINE class. class PARTICLE : public PHYSICS_OBJECT { public: PARTICLE(); PARTICLE(D3DXVECTOR3 pos); void Update(float deltaTime); void Render(); Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
4. Chapter 6 Physics Primer 129 void AddForces(); void SatisfyConstraints(vector &obstacles); private: D3DXVECTOR3 m_pos; D3DXVECTOR3 m_oldPos; D3DXVECTOR3 m_forces; float m_bounce; }; The m_bounce member is simply a float between [0, 1] that defines how much of the energy is lost when the particle bounces against a surface. This value is also known as the “coefficient of restitution,” or in other words, “bounciness.” With a high bounciness value, the particle will act like a rubber ball, whereas with a low value it will bounce as well as a rock. The next thing you need to figure out is how a particle behaves when it collides with a plane (remember the world is described with OBBs, which in turn can be described with six planes). “To describe collision response, we need to partition velocity and force vectors into two orthogonal components, one normal to the collision surface, and the other parallel to it.” [Witkin01] This same thing is shown more graphically in Figure 6.12. FIGURE 6.12 Particle and forces before collision (left). Particle and forces after collision (right). ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
5. 130 Character Animation with Direct3D VN = (N•V)N VN is the current velocity projected onto the normal N of the plane. VT = V – VN VT is the velocity parallel to the plane. V' = VT – VN Finally, V' is the resulting velocity after the collision. The following code snippet shows you one way to implement this particle-plane collision response using Verlet integration. In this code I assume that a collision has occurred and that I know the normal of the plane with which the particle has collided. //Calculate Velocity D3DXVECTOR3 V = m_pos - m_oldPos; //Normal Velocity D3DXVECTOR3 VN = D3DXVec3Dot(&planeNormal, &V) * planeNormal; //Tangent Velocity D3DXVECTOR3 VT = V - VN; //Change the old position (i.e. update the particle velocity) m_oldPos = m_pos - (VT - VN * m_bounce); First, the velocity of the particle was calculated by subtracting the position of the particle with its previous position. Next, the normal and tangent velocities were calculated using the formulas above. Since Verlet integration is used, you need to change the old position of the particle to make the particle go in a different direc- tion the next update. You can also see that I have added the m_bounce variable to the calculation, and in this way you can simulate particles with different “bounciness.” Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
6. Chapter 6 Physics Primer 131 EXAMPLE 6.2 This example covers the PARTICLE class as well as the particle-OBB inter- section test and response. Play around with the different physics parameters to make the particles behave differently. Also try changing the environment and build something more advanced out of the Oriented Bounding Boxes. THE SPRING Next I’ll show you how to simulate a spring in a physics simulation. In real life you find coiled springs in many different places such as car suspensions, wrist watches, pogo-sticks, etc. A spring has a resting length—i.e., the length when it doesn’t try to expand or contract. When you stretch a spring away from this equilibrium length it will “pull back” with a force equivalent to the difference from its resting length. This is also known as Hooke’s Law. F = –kx ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
7. 132 Character Animation with Direct3D F is the resulting force, k is the spring constant (how strong/stiff the spring is), and x is the spring’s current distance away from its resting length. Note that if the spring is already in its resting state, the distance will be zero and so will the resulting force. If an object is hanging from one end of a spring that has the other end attached to the roof, it will have an oscillating behavior whenever the object is moved away from the resting length, as shown in Figure 6.13. FIGURE 6.13 The oscillating behavior of a spring. As you can see in Figure 6.13, the spring makes a nice sinus shaped motion over time. Eventually, however, friction will bring this oscillation to a stop (this is, incidentally, what forces us to manually wind up old mechanical clocks). In this book a specific subset of springs are of particular interest: springs with an infinite strength. If you connect two particles with a spring like this, the spring will auto- matically pull or push these particles together or apart until they are exactly at the spring’s resting length from each other. Using springs with infinite strength can be used to model rigid volumes like tetrahedrons, boxes, and more. The following code snippet shows the implementation of the SPRING class. As you can see, you don’t need anything other than pointers to two particles and a resting length for the spring. The SPRING class also extends the PHYSICS_OBJECT class and can therefore also be simulated by the PHYSICS_ENGINE class. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
8. Chapter 6 Physics Primer 133 class SPRING : public PHYSICS_OBJECT { public: SPRING(PARTICLE *p1, PARTICLE *p2, float restLength); void Update(float deltaTime){} void Render(); void AddForces(){} void SatisfyConstraints(vector &obstacles); private: PARTICLE *m_pParticle1; PARTICLE *m_pParticle2; float m_restLength; }; void SPRING::SatisfyConstraints(vector &obstacles) { D3DXVECTOR3 delta = m_pParticle1->m_pos - m_pParticle2->m_pos; float dist = D3DXVec3Length(&delta); float diff = (dist-m_restLength)/dist; m_pParticle1->m_pos -= delta * 0.5f * diff; m_pParticle2->m_pos += delta * 0.5f * diff; } This code shows the SPRING class and its most important function, the Satisfy- Constraints() function. In it, the two particles are forcibly moved to a distance equal to the resting length of the spring. ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
9. 134 Character Animation with Direct3D EXAMPLE 6.3 This example is a small variation of the previous example. This time, how- ever, the particles are connected with springs forcing the particles to stay at a specific distance from each other. C ONCLUSIONS As promised, this chapter contained only the “bare bones” of the knowledge needed to create a physics simulation. It is important to note that all the code in this chapter has been written with clarity in mind, not optimization. The simplest and most straightforward way to optimize a physics engine like this is to remove all square root calculations. Write your own implementation of D3DXVec3Length(), D3DXVec3Normalize(), etc. using an approximate square root calculation. For more advanced physics simulations, you’ll also need some form of space partitioning speeding up nearest neighbor queries, etc. In this chapter the game world was described using Oriented Bounding Boxes. A basic particle system was simulated as well as particles connected with springs. Although it may seem like a lot more knowledge is needed to create a ragdoll Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
10. Chapter 6 Physics Primer 135 system, most of it has already been covered. All you need to do is start creating a skeleton built of particles connected with springs. In Chapter 7, a ragdoll will be created from an existing character using an open source physics engine. C HAPTER 6 E XERCISES Implement particles with different mass and bounciness values and see how that affects the particles and springs in Example 6.3. Implement springs that don’t have infinite spring strength (use Hooke’s Law). Try to connect particles with springs to form more complex shapes and objects, such as boxes, etc. F URTHER R EADING [Eberly99] Eberly, David, “Dynamic Collision Detection Using Oriented Bounding Boxes.” Available online at http://www.geometrictools.com/Documentation/ DynamicCollisionDetection.pdf, 1999. [Gomez99] Gomez, Miguel, “Simple Intersection Tests for Games.” Available online at http://www.gamasutra.com/features/19991018/Gomez_1.htm, 1999. [Gomez00] Gomez, Miguel, “Integrating the Equations of Rigid Body Motion.” Game Programming Gems, Charles River Media, 2000. [Ibanez01] Ibanez, Luis, “Tutorial on Quaternions.” Available online at http://www. itk.org/CourseWare/Training/QuaternionsI.pdf, 2001. [Jakobsen03] Jakobsen, Thomas, “Advanced Character Physics.” Available online at http://www.gamasutra.com/resource_guide/20030121/jacobson_01.shtml, 2003. [Svarovsky00] Svarovsky, Jan, “Quaternions for Game Programming.” Game Programming Gems, Charles River Media, 2000. [Wikipedia] “Gravitational Constant.” Available online at http://en.wikipedia.org/ wiki/Gravitational_constant. [Witkin01] Witkin, Andrew, “Physically Based Modeling, Particle System Dynamics.” Available online at http://www.pixar.com/companyinfo/research/pbm2001/ pdf/notesc.pdf, 2001. ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark.