| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace FuelCell
- {
- public class GameObject
- {
- public Model Model { get; set; }
- public Vector3 Position { get; set; }
- public bool IsActive { get; set; }
- public BoundingSphere BoundingSphere { get; set; }
- public GameObject()
- {
- Model = null;
- Position = Vector3.Zero;
- IsActive = false;
- BoundingSphere = new BoundingSphere();
- }
- protected BoundingSphere CalculateBoundingSphere()
- {
- BoundingSphere mergedSphere = new BoundingSphere();
- BoundingSphere[] boundingSpheres;
- int index = 0;
- int meshCount = Model.Meshes.Count;
- boundingSpheres = new BoundingSphere[meshCount];
- foreach (ModelMesh mesh in Model.Meshes)
- {
- boundingSpheres[index++] = mesh.BoundingSphere;
- }
- mergedSphere = boundingSpheres[0];
- if ((Model.Meshes.Count) > 1)
- {
- index = 1;
- do
- {
- mergedSphere = BoundingSphere.CreateMerged(mergedSphere, boundingSpheres[index]);
- index++;
- } while (index < Model.Meshes.Count);
- }
- mergedSphere.Center.Y = 0;
- return mergedSphere;
- }
- /// <summary>
- /// Debugging method to draw the bounding sphere of the object.
- /// </summary>
- /// <param name="view">The view matrix from the camera.</param>
- /// <param name="projection">The projection matrix from the camera.</param>
- /// <param name="boundingSphereModel">The GameObject to draw the bounding sphere around.</param>
- internal void DrawBoundingSphere(Matrix view, Matrix projection, GameObject boundingSphereModel)
- {
- Matrix scaleMatrix = Matrix.CreateScale(BoundingSphere.Radius);
- Matrix translateMatrix = Matrix.CreateTranslation(BoundingSphere.Center);
- Matrix worldMatrix = scaleMatrix * translateMatrix;
- foreach (ModelMesh mesh in boundingSphereModel.Model.Meshes)
- {
- foreach (BasicEffect effect in mesh.Effects)
- {
- effect.World = worldMatrix;
- effect.View = view;
- effect.Projection = projection;
- }
- mesh.Draw();
- }
- }
- }
- }
|