Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.01 KB | None | 0 0
  1. public class BaseEntity : IDisposable
  2.     {
  3.         #region Fields
  4.         // Entity's owner, null means root node
  5.         protected BaseEntity parent = null;
  6.  
  7.         // matrix to represent the entity's orientation, relative to parent
  8.         private Matrix orientation =
  9.             Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Up);
  10.  
  11.         // bounding box, to check camera visibility
  12.         protected BoundingSphere boundingSphere = new BoundingSphere(Vector3.Zero, 1.0f);
  13.  
  14.         #endregion
  15.  
  16.         #region Constructors
  17.         // ------------------------------------------------------------------
  18.         public BaseEntity() : this(null) { }
  19.  
  20.         // ------------------------------------------------------------------
  21.         public BaseEntity(BaseEntity parent)
  22.         {
  23.             this.parent = parent;
  24.             CalculateBoundingSphere();
  25.         }
  26.  
  27.        
  28.  
  29.         #endregion
  30.  
  31.         #region Functions
  32.         // ---- Load    -----------------------------------------------------
  33.         // ------------------------------------------------------------------
  34.         public virtual void LoadContent(ContentManager content) { }
  35.  
  36.         // ------------------------------------------------------------------
  37.         public void SetupWorld(Vector3 scale, Matrix rot, Vector3 position)
  38.         {
  39.             Matrix s = Matrix.CreateScale(scale);
  40.             Matrix t = Matrix.CreateTranslation(position);
  41.  
  42.             orientation = s * rot * t;
  43.             CalculateBoundingSphere();
  44.         }
  45.  
  46.         protected virtual void CalculateBoundingSphere()
  47.         {
  48.             // create a sphere based on largest scaling factor
  49.             boundingSphere.Radius = Math.Max(World.M11, Math.Max(World.M22, World.M33));
  50.             boundingSphere.Center = World.Translation;
  51.         }
  52.  
  53.         // ---- Update  -----------------------------------------------------
  54.         // ------------------------------------------------------------------
  55.         public virtual void Update(GameTime gameTime)
  56.         {
  57.             CalculateBoundingSphere();
  58.         }
  59.  
  60.         // ------------------------------------------------------------------
  61.         public virtual void InfluenceModelRotation(Quaternion quat)
  62.         {
  63.             // rotate in model space by rotating normally then
  64.             // replacing the translation component to the original
  65.             Vector3 pos = orientation.Translation;
  66.             orientation *= Matrix.CreateFromQuaternion(quat);
  67.             orientation.Translation = pos;
  68.         }
  69.        
  70.         // ------------------------------------------------------------------
  71.         public virtual void InfluenceModelTranslation(Vector3 translation)
  72.         {
  73.             Vector3 pos;
  74.             Vector3 scale;
  75.             Quaternion rot;
  76.  
  77.             orientation.Decompose(out scale, out rot, out pos);
  78.  
  79.             // transform the translation to model space
  80.             translation = Vector3.Transform(translation, rot);
  81.            
  82.             // update model based on model-space translation
  83.             orientation *= Matrix.CreateTranslation(translation);
  84.         }
  85.  
  86.         // ------------------------------------------------------------------
  87.  
  88.         #endregion
  89.  
  90.         #region Properties
  91.         // ------------------------------------------------------------------
  92.         public Matrix World
  93.         {
  94.             get
  95.             {
  96.                 if (parent != null)
  97.                     return orientation * parent.World;
  98.                 else
  99.                     return orientation;
  100.             }
  101.         }
  102.  
  103.         // ------------------------------------------------------------------
  104.         public Vector3 Forward { get { return World.Forward; } }
  105.  
  106.         // ------------------------------------------------------------------
  107.         public Vector3 Up { get { return World.Up; } }
  108.  
  109.         // ------------------------------------------------------------------
  110.         public Vector3 Right { get { return World.Right; } }
  111.  
  112.         // ------------------------------------------------------------------
  113.         public Vector3 Position { get { return World.Translation; } }
  114.  
  115.         // ------------------------------------------------------------------
  116.         public Vector3 Scale
  117.         {
  118.             get
  119.             {
  120.                 if (parent != null)
  121.                 {
  122.                     float x, y, z;
  123.                     x = parent.World.M11 * orientation.M11;
  124.                     y = parent.World.M22 * orientation.M22;
  125.                     z = parent.World.M33 * orientation.M33;
  126.                     return new Vector3(x, y, z);
  127.                 }
  128.                 else
  129.                     return new Vector3(orientation.M11, orientation.M22, orientation.M33);
  130.             }
  131.         }
  132.  
  133.         // ------------------------------------------------------------------
  134.         public Matrix Orientation
  135.         {
  136.             get { return orientation; }
  137.             set { orientation = value; }
  138.         }
  139.  
  140.         #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement