Tank.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Tank.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Content;
  12. using Microsoft.Xna.Framework.Graphics;
  13. #endregion
  14. namespace SimpleAnimation
  15. {
  16. /// <summary>
  17. /// Helper class for drawing a tank model with animated wheels and turret.
  18. /// </summary>
  19. public class Tank
  20. {
  21. #region Fields
  22. // The XNA framework Model object that we are going to display.
  23. Model tankModel;
  24. // Shortcut references to the bones that we are going to animate.
  25. // We could just look these up inside the Draw method, but it is more
  26. // efficient to do the lookups while loading and cache the results.
  27. ModelBone leftBackWheelBone;
  28. ModelBone rightBackWheelBone;
  29. ModelBone leftFrontWheelBone;
  30. ModelBone rightFrontWheelBone;
  31. ModelBone leftSteerBone;
  32. ModelBone rightSteerBone;
  33. ModelBone turretBone;
  34. ModelBone cannonBone;
  35. ModelBone hatchBone;
  36. // Store the original transform matrix for each animating bone.
  37. Matrix leftBackWheelTransform;
  38. Matrix rightBackWheelTransform;
  39. Matrix leftFrontWheelTransform;
  40. Matrix rightFrontWheelTransform;
  41. Matrix leftSteerTransform;
  42. Matrix rightSteerTransform;
  43. Matrix turretTransform;
  44. Matrix cannonTransform;
  45. Matrix hatchTransform;
  46. // Array holding all the bone transform matrices for the entire model.
  47. // We could just allocate this locally inside the Draw method, but it
  48. // is more efficient to reuse a single array, as this avoids creating
  49. // unnecessary garbage.
  50. Matrix[] boneTransforms;
  51. // Current animation positions.
  52. float wheelRotationValue;
  53. float steerRotationValue;
  54. float turretRotationValue;
  55. float cannonRotationValue;
  56. float hatchRotationValue;
  57. #endregion
  58. #region Properties
  59. /// <summary>
  60. /// Gets or sets the wheel rotation amount.
  61. /// </summary>
  62. public float WheelRotation
  63. {
  64. get { return wheelRotationValue; }
  65. set { wheelRotationValue = value; }
  66. }
  67. /// <summary>
  68. /// Gets or sets the steering rotation amount.
  69. /// </summary>
  70. public float SteerRotation
  71. {
  72. get { return steerRotationValue; }
  73. set { steerRotationValue = value; }
  74. }
  75. /// <summary>
  76. /// Gets or sets the turret rotation amount.
  77. /// </summary>
  78. public float TurretRotation
  79. {
  80. get { return turretRotationValue; }
  81. set { turretRotationValue = value; }
  82. }
  83. /// <summary>
  84. /// Gets or sets the cannon rotation amount.
  85. /// </summary>
  86. public float CannonRotation
  87. {
  88. get { return cannonRotationValue; }
  89. set { cannonRotationValue = value; }
  90. }
  91. /// <summary>
  92. /// Gets or sets the entry hatch rotation amount.
  93. /// </summary>
  94. public float HatchRotation
  95. {
  96. get { return hatchRotationValue; }
  97. set { hatchRotationValue = value; }
  98. }
  99. #endregion
  100. /// <summary>
  101. /// Loads the tank model.
  102. /// </summary>
  103. public void Load(ContentManager content)
  104. {
  105. // Load the tank model from the ContentManager.
  106. tankModel = content.Load<Model>("tank");
  107. // Look up shortcut references to the bones we are going to animate.
  108. leftBackWheelBone = tankModel.Bones["l_back_wheel_geo"];
  109. rightBackWheelBone = tankModel.Bones["r_back_wheel_geo"];
  110. leftFrontWheelBone = tankModel.Bones["l_front_wheel_geo"];
  111. rightFrontWheelBone = tankModel.Bones["r_front_wheel_geo"];
  112. leftSteerBone = tankModel.Bones["l_steer_geo"];
  113. rightSteerBone = tankModel.Bones["r_steer_geo"];
  114. turretBone = tankModel.Bones["turret_geo"];
  115. cannonBone = tankModel.Bones["canon_geo"];
  116. hatchBone = tankModel.Bones["hatch_geo"];
  117. // Store the original transform matrix for each animating bone.
  118. leftBackWheelTransform = leftBackWheelBone.Transform;
  119. rightBackWheelTransform = rightBackWheelBone.Transform;
  120. leftFrontWheelTransform = leftFrontWheelBone.Transform;
  121. rightFrontWheelTransform = rightFrontWheelBone.Transform;
  122. leftSteerTransform = leftSteerBone.Transform;
  123. rightSteerTransform = rightSteerBone.Transform;
  124. turretTransform = turretBone.Transform;
  125. cannonTransform = cannonBone.Transform;
  126. hatchTransform = hatchBone.Transform;
  127. // Allocate the transform matrix array.
  128. boneTransforms = new Matrix[tankModel.Bones.Count];
  129. }
  130. /// <summary>
  131. /// Draws the tank model, using the current animation settings.
  132. /// </summary>
  133. public void Draw(Matrix world, Matrix view, Matrix projection)
  134. {
  135. // Set the world matrix as the root transform of the model.
  136. tankModel.Root.Transform = world;
  137. // Calculate matrices based on the current animation position.
  138. Matrix wheelRotation = Matrix.CreateRotationX(wheelRotationValue);
  139. Matrix steerRotation = Matrix.CreateRotationY(steerRotationValue);
  140. Matrix turretRotation = Matrix.CreateRotationY(turretRotationValue);
  141. Matrix cannonRotation = Matrix.CreateRotationX(cannonRotationValue);
  142. Matrix hatchRotation = Matrix.CreateRotationX(hatchRotationValue);
  143. // Apply matrices to the relevant bones.
  144. leftBackWheelBone.Transform = wheelRotation * leftBackWheelTransform;
  145. rightBackWheelBone.Transform = wheelRotation * rightBackWheelTransform;
  146. leftFrontWheelBone.Transform = wheelRotation * leftFrontWheelTransform;
  147. rightFrontWheelBone.Transform = wheelRotation * rightFrontWheelTransform;
  148. leftSteerBone.Transform = steerRotation * leftSteerTransform;
  149. rightSteerBone.Transform = steerRotation * rightSteerTransform;
  150. turretBone.Transform = turretRotation * turretTransform;
  151. cannonBone.Transform = cannonRotation * cannonTransform;
  152. hatchBone.Transform = hatchRotation * hatchTransform;
  153. // Look up combined bone matrices for the entire model.
  154. tankModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
  155. // Draw the model.
  156. foreach (ModelMesh mesh in tankModel.Meshes)
  157. {
  158. foreach (BasicEffect effect in mesh.Effects)
  159. {
  160. effect.World = boneTransforms[mesh.ParentBone.Index];
  161. effect.View = view;
  162. effect.Projection = projection;
  163. effect.EnableDefaultLighting();
  164. }
  165. mesh.Draw();
  166. }
  167. }
  168. }
  169. }