# Microsoft XNA Game Studio Creator’s Guide- P5

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

0
53
lượt xem
11

## Microsoft XNA Game Studio Creator’s Guide- P5

Mô tả tài liệu

Microsoft XNA Game Studio Creator’s Guide- P5:The release of the XNA platform and specifically the ability for anyone to write Xbox 360 console games was truly a major progression in the game-programming world. Before XNA, it was simply too complicated and costly for a student, software hobbyist, or independent game developer to gain access to a decent development kit for a major console platform.

Chủ đề:

Bình luận(0)

Lưu

## Nội dung Text: Microsoft XNA Game Studio Creator’s Guide- P5

1. 98 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE FIGURE 7-2 Earth and moon example The vertices for the earth and moon are set when the program begins, so InitializeTriangle() is called from the Initialize() method: InitializeTriangle(); When animating the triangle, a time-scaled increment is made to the triangle’s ro- tation during each Update(). This rotation value is modded by 2π to clamp the value between 0 and 2π. earthRotation += gameTime.ElapsedGameTime.Milliseconds/1000.0f; earthRotation = earthRotation%(2.0f * MathHelper.Pi); moonRotation += (float)TargetElapsedTime.Milliseconds/750.0f; moonRotation = moonRotation %(2.0f * MathHelper.Pi); Here are the five recommended steps for drawing the revolving Earth object: 1. Declare the matrices. 2. Initialize the matrices. The identity matrix is initialized as a default matrix in the event of no transformations. (Try leaving it out of the transformation,
2. C H A P T E R 7 99 Animation Introduction and notice you still get the same result.) A matrix that generates the earth’s revolution on the Y axis is computed based on a constantly changing angle (in radians). Every frame, the angle is incremented with a value based on the time lapse between frames. This time-scaled increment to the rotation angle ensures that the animation appears smoothly while maintaining a constant rate of change. Scaling the increment based on time is necessary because durations between frames can vary depending on other tasks being performed by the operating system. Finally, a translation is created to move the earth 0.5 units upward on the Y axis and 8.0 units inward on the Z axis. 3. The World matrix is built by multiplying each of the matrices in the transformation using the I.S.R.O.T. sequence. 4. The World matrix used to transform the earth is passed to the shader as part of the World*View*Projection matrix. 5. The triangle is rendered by drawing vertices with a triangle strip. Adding DrawEarth() to the game class provides the code needed for transform- ing and drawing the Earth: private void DrawEarth(){ // 1: declare matrices Matrix world, translation, rotationY; // 2: initialize matrices rotationY = Matrix.CreateRotationY(earthRotation); translation = Matrix.CreateTranslation(0.0f, 0.5f, -8.0f); // 3: build cumulative World matrix using I.S.R.O.T. sequence // identity, scale, rotate, orbit(translate & rotate), translate world = rotationY * translation; // 4: set shader parameters positionColorEffectWVP.SetValue(world * cam.viewMatrix * cam.projectionMatrix); // 5: draw object - select primitive type, vertices, # primitives PositionColorShader(PrimitiveType.TriangleStrip, triangleVertex, 1); }
3. 100 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE Next, the DrawMoon() method implements the same five-step drawing routine to transform and render the same vertices as a Moon object. The moon has its own revo- lution about the Y axis, and it also orbits around the earth. In addition, the moon is scaled to one-fifth the size of the earth. The DrawMoon() method performs all of the same transformations as the DrawEarth() method. Plus, DrawMoon() implements scaling and an orbit. All of the matrices declared in the DrawEarth() method are declared in DrawMoon() to perform the same transformations. Also, additional matrices are declared and set in this method to handle the scaling and orbit. The scale is set to draw the object at one-fifth the size of the earth by assigning the scale matrix the following value: Matrix.CreateScale(0.2f, 0.2f, 0.2f); Remember that the orbit is a two-step process that involves a translation followed by a rotation. When the World matrix is built, the crucial I.S.R.O.T. sequence is used to ensure that the matrices are multiplied in the proper order: world = scale * rotationY * orbitTranslation * orbitRotationY * translation; Since the same vertices are used for drawing the Moon and the Earth, steps 4 and 5 of DrawMoon() are identical to those in DrawEarth(). private void DrawMoon(){ // 1: declare matrices Matrix world, scale, rotationY, translation, orbitTranslation, orbitRotationY; // 2: initialize matrices scale = Matrix.CreateScale(0.2f, 0.2f, 0.2f); rotationY = Matrix.CreateRotationY(moonRotation); translation = Matrix.CreateTranslation(0.0f, 0.8f,-8.0f); orbitTranslation = Matrix.CreateTranslation(0.0f, 0.0f,-1.0f); orbitRotationY = Matrix.CreateRotationY(moonRotation); // 3: build cumulative World matrix using I.S.R.O.T. sequence // identity, scale, rotate, orbit(translate & rotate), translate world = scale * rotationY * orbitTranslation * orbitRotationY * translation;
5. 102 MICROSOFT XNA GAME STUDIO CREATOR’S GUIDE FIGURE 7-3 Primitive airplane with rotating propeller 3. When you finish Exercise 2, transform your propeller so it serves as a rotor for a helicopter. Using the same set of vertices, write another procedure to transform and render the same rectangle used for the main rotor as a back rotor, as shown here in Figure 7-4. FIGURE 7-4 Helicopter with top and back rotors using the same vertices
6. CHAPTER 8 Character Movement
7. reading and applying the material covered in Chapter 7, AFTER you should be comfortable performing simple animations with translations and rotations. For most gamers, it is not enough just to make a bird flap its wings or make the propeller of an airplane spin; anybody with half an ounce of curiosity wants to see these objects actually fly. This chapter introduces a simple animation method that allows moving objects to travel independently within your 3D world. Additional methods for enabling the movement of objects are covered in Chapter 21. Regardless of the method used to move objects and characters, basic movement is generated by updating the X, Y, and Z position coordinates, as well as the rotation angles of the moving object rendered at every frame. D IRECTION When you animate vehicles that fly, drive, sail, or glide, you most likely expect them to point in the direction they are traveling. Calculating the angle of direction can be done using several methods. Without this calculation, your vehicles could look as if they are flying backward or even sideways. Trigonometry offers a simple intuitive approach to calculate the angle of direction—this method will be used often through- out this book. However, computing direction can also be done with vectors. Using vectors to calculate direction is actually a more powerful method for implementing rotations of direction because they offer a simpler means to implement complex transformations for directions in all planes. Calculating Direction Using Trigonometry The trigonometry applied in this chapter is actually quite simple and only involves using the arctangent function. The arctangent function enables calculations of direc- tion about the Y axis when the X and Z coordinates of the object are known. When the Right Hand Rule is used, all positive rotations are counterclockwise. To calculate an object’s angle about the Y axis, draw a line from the object’s position to the preceding axis in the rotation to create a right-angle triangle. The tangent of the angle between the hypotenuse and the axis can be calculated with the following equa- tion: tan φ side length / adjacent side length (where φ is the angle) 104