GameObject.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. namespace FuelCell
  4. {
  5. public class GameObject
  6. {
  7. public Model Model { get; set; }
  8. public Vector3 Position { get; set; }
  9. public bool IsActive { get; set; }
  10. public BoundingSphere BoundingSphere { get; set; }
  11. public GameObject()
  12. {
  13. Model = null;
  14. Position = Vector3.Zero;
  15. IsActive = false;
  16. BoundingSphere = new BoundingSphere();
  17. }
  18. protected BoundingSphere CalculateBoundingSphere()
  19. {
  20. BoundingSphere mergedSphere = new BoundingSphere();
  21. BoundingSphere[] boundingSpheres;
  22. int index = 0;
  23. int meshCount = Model.Meshes.Count;
  24. boundingSpheres = new BoundingSphere[meshCount];
  25. foreach (ModelMesh mesh in Model.Meshes)
  26. {
  27. boundingSpheres[index++] = mesh.BoundingSphere;
  28. }
  29. mergedSphere = boundingSpheres[0];
  30. if ((Model.Meshes.Count) > 1)
  31. {
  32. index = 1;
  33. do
  34. {
  35. mergedSphere = BoundingSphere.CreateMerged(mergedSphere, boundingSpheres[index]);
  36. index++;
  37. } while (index < Model.Meshes.Count);
  38. }
  39. mergedSphere.Center.Y = 0;
  40. return mergedSphere;
  41. }
  42. /// <summary>
  43. /// Debugging method to draw the bounding sphere of the object.
  44. /// </summary>
  45. /// <param name="view">The view matrix from the camera.</param>
  46. /// <param name="projection">The projection matrix from the camera.</param>
  47. /// <param name="boundingSphereModel">The GameObject to draw the bounding sphere around.</param>
  48. internal void DrawBoundingSphere(Matrix view, Matrix projection, GameObject boundingSphereModel)
  49. {
  50. Matrix scaleMatrix = Matrix.CreateScale(BoundingSphere.Radius);
  51. Matrix translateMatrix = Matrix.CreateTranslation(BoundingSphere.Center);
  52. Matrix worldMatrix = scaleMatrix * translateMatrix;
  53. foreach (ModelMesh mesh in boundingSphereModel.Model.Meshes)
  54. {
  55. foreach (BasicEffect effect in mesh.Effects)
  56. {
  57. effect.World = worldMatrix;
  58. effect.View = view;
  59. effect.Projection = projection;
  60. }
  61. mesh.Draw();
  62. }
  63. }
  64. }
  65. }