Tank.cs 11 KB

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