Tank.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. using XnaGraphicsDemo;
  14. using System;
  15. #endregion
  16. namespace SimpleAnimation
  17. {
  18. /// <summary>
  19. /// Helper class for drawing a tank model with animated wheels and turret.
  20. /// </summary>
  21. public class Tank
  22. {
  23. #region Fields
  24. // The XNA framework Model object that we are going to display.
  25. Model tankModel;
  26. // Shortcut references to the bones that we are going to animate.
  27. // We could just look these up inside the Draw method, but it is more
  28. // efficient to do the lookups while loading and cache the results.
  29. ModelBone leftBackWheelBone;
  30. ModelBone rightBackWheelBone;
  31. ModelBone leftFrontWheelBone;
  32. ModelBone rightFrontWheelBone;
  33. ModelBone leftSteerBone;
  34. ModelBone rightSteerBone;
  35. ModelBone turretBone;
  36. ModelBone cannonBone;
  37. ModelBone hatchBone;
  38. // Store the original transform matrix for each animating bone.
  39. Matrix leftBackWheelTransform;
  40. Matrix rightBackWheelTransform;
  41. Matrix leftFrontWheelTransform;
  42. Matrix rightFrontWheelTransform;
  43. Matrix leftSteerTransform;
  44. Matrix rightSteerTransform;
  45. Matrix turretTransform;
  46. Matrix cannonTransform;
  47. Matrix hatchTransform;
  48. // Array holding all the bone transform matrices for the entire model.
  49. // We could just allocate this locally inside the Draw method, but it
  50. // is more efficient to reuse a single array, as this avoids creating
  51. // unnecessary garbage.
  52. Matrix[] boneTransforms;
  53. // Current animation positions.
  54. float wheelRotationValue;
  55. float steerRotationValue;
  56. float turretRotationValue;
  57. float cannonRotationValue;
  58. float hatchRotationValue;
  59. #endregion
  60. #region Properties
  61. /// <summary>
  62. /// Gets or sets the wheel rotation amount.
  63. /// </summary>
  64. public float WheelRotation
  65. {
  66. get { return wheelRotationValue; }
  67. set { wheelRotationValue = value; }
  68. }
  69. /// <summary>
  70. /// Gets or sets the steering rotation amount.
  71. /// </summary>
  72. public float SteerRotation
  73. {
  74. get { return steerRotationValue; }
  75. set { steerRotationValue = value; }
  76. }
  77. /// <summary>
  78. /// Gets or sets the turret rotation amount.
  79. /// </summary>
  80. public float TurretRotation
  81. {
  82. get { return turretRotationValue; }
  83. set { turretRotationValue = value; }
  84. }
  85. /// <summary>
  86. /// Gets or sets the cannon rotation amount.
  87. /// </summary>
  88. public float CannonRotation
  89. {
  90. get { return cannonRotationValue; }
  91. set { cannonRotationValue = value; }
  92. }
  93. /// <summary>
  94. /// Gets or sets the entry hatch rotation amount.
  95. /// </summary>
  96. public float HatchRotation
  97. {
  98. get { return hatchRotationValue; }
  99. set { hatchRotationValue = value; }
  100. }
  101. #endregion
  102. /// <summary>
  103. /// Loads the tank model.
  104. /// </summary>
  105. public void Load(ContentManager content)
  106. {
  107. // Load the tank model from the ContentManager.
  108. tankModel = content.Load<Model>("tank");
  109. // Look up shortcut references to the bones we are going to animate.
  110. leftBackWheelBone = tankModel.Bones["l_back_wheel_geo"];
  111. rightBackWheelBone = tankModel.Bones["r_back_wheel_geo"];
  112. leftFrontWheelBone = tankModel.Bones["l_front_wheel_geo"];
  113. rightFrontWheelBone = tankModel.Bones["r_front_wheel_geo"];
  114. leftSteerBone = tankModel.Bones["l_steer_geo"];
  115. rightSteerBone = tankModel.Bones["r_steer_geo"];
  116. turretBone = tankModel.Bones["turret_geo"];
  117. cannonBone = tankModel.Bones["canon_geo"];
  118. hatchBone = tankModel.Bones["hatch_geo"];
  119. // Store the original transform matrix for each animating bone.
  120. leftBackWheelTransform = leftBackWheelBone.Transform;
  121. rightBackWheelTransform = rightBackWheelBone.Transform;
  122. leftFrontWheelTransform = leftFrontWheelBone.Transform;
  123. rightFrontWheelTransform = rightFrontWheelBone.Transform;
  124. leftSteerTransform = leftSteerBone.Transform;
  125. rightSteerTransform = rightSteerBone.Transform;
  126. turretTransform = turretBone.Transform;
  127. cannonTransform = cannonBone.Transform;
  128. hatchTransform = hatchBone.Transform;
  129. // Allocate the transform matrix array.
  130. boneTransforms = new Matrix[tankModel.Bones.Count];
  131. }
  132. /// <summary>
  133. /// Animates the tank model.
  134. /// </summary>
  135. /// <param name="gameTime"></param>
  136. public void Animate(GameTime gameTime)
  137. {
  138. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  139. SteerRotation = (float)Math.Sin(time * 0.75f) * 0.5f;
  140. TurretRotation = (float)Math.Sin(time * 0.333f) * 1.25f;
  141. CannonRotation = (float)Math.Sin(time * 0.25f) * 0.333f - 0.333f;
  142. HatchRotation = MathHelper.Clamp((float)Math.Sin(time * 2) * 2, -1, 0);
  143. }
  144. /// <summary>
  145. /// Draws the tank model, using the current animation settings.
  146. /// </summary>
  147. public void Draw(Matrix world, Matrix view, Matrix projection, LightingMode lightMode, bool textureEnable)
  148. {
  149. // Set the world matrix as the root transform of the model.
  150. tankModel.Root.Transform = world;
  151. // Calculate matrices based on the current animation position.
  152. Matrix wheelRotation = Matrix.CreateRotationX(wheelRotationValue);
  153. Matrix steerRotation = Matrix.CreateRotationY(steerRotationValue);
  154. Matrix turretRotation = Matrix.CreateRotationY(turretRotationValue);
  155. Matrix cannonRotation = Matrix.CreateRotationX(cannonRotationValue);
  156. Matrix hatchRotation = Matrix.CreateRotationX(hatchRotationValue);
  157. // Apply matrices to the relevant bones.
  158. leftBackWheelBone.Transform = wheelRotation * leftBackWheelTransform;
  159. rightBackWheelBone.Transform = wheelRotation * rightBackWheelTransform;
  160. leftFrontWheelBone.Transform = wheelRotation * leftFrontWheelTransform;
  161. rightFrontWheelBone.Transform = wheelRotation * rightFrontWheelTransform;
  162. leftSteerBone.Transform = steerRotation * leftSteerTransform;
  163. rightSteerBone.Transform = steerRotation * rightSteerTransform;
  164. turretBone.Transform = turretRotation * turretTransform;
  165. cannonBone.Transform = cannonRotation * cannonTransform;
  166. hatchBone.Transform = hatchRotation * hatchTransform;
  167. // Look up combined bone matrices for the entire model.
  168. tankModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
  169. // Draw the model.
  170. foreach (ModelMesh mesh in tankModel.Meshes)
  171. {
  172. foreach (BasicEffect effect in mesh.Effects)
  173. {
  174. effect.World = boneTransforms[mesh.ParentBone.Index];
  175. effect.View = view;
  176. effect.Projection = projection;
  177. switch (lightMode)
  178. {
  179. case LightingMode.NoLighting:
  180. effect.LightingEnabled = false;
  181. break;
  182. case LightingMode.OneVertexLight:
  183. effect.EnableDefaultLighting();
  184. effect.PreferPerPixelLighting = false;
  185. effect.DirectionalLight1.Enabled = false;
  186. effect.DirectionalLight2.Enabled = false;
  187. break;
  188. case LightingMode.ThreeVertexLights:
  189. effect.EnableDefaultLighting();
  190. effect.PreferPerPixelLighting = false;
  191. break;
  192. case LightingMode.ThreePixelLights:
  193. effect.EnableDefaultLighting();
  194. effect.PreferPerPixelLighting = true;
  195. break;
  196. }
  197. effect.SpecularColor = new Vector3(0.8f, 0.8f, 0.6f);
  198. effect.SpecularPower = 16;
  199. effect.TextureEnabled = textureEnable;
  200. }
  201. mesh.Draw();
  202. }
  203. }
  204. }
  205. }