Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BaseEntity : IDisposable
- {
- #region Fields
- // Entity's owner, null means root node
- protected BaseEntity parent = null;
- // matrix to represent the entity's orientation, relative to parent
- private Matrix orientation =
- Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Up);
- // bounding box, to check camera visibility
- protected BoundingSphere boundingSphere = new BoundingSphere(Vector3.Zero, 1.0f);
- #endregion
- #region Constructors
- // ------------------------------------------------------------------
- public BaseEntity() : this(null) { }
- // ------------------------------------------------------------------
- public BaseEntity(BaseEntity parent)
- {
- this.parent = parent;
- CalculateBoundingSphere();
- }
- #endregion
- #region Functions
- // ---- Load -----------------------------------------------------
- // ------------------------------------------------------------------
- public virtual void LoadContent(ContentManager content) { }
- // ------------------------------------------------------------------
- public void SetupWorld(Vector3 scale, Matrix rot, Vector3 position)
- {
- Matrix s = Matrix.CreateScale(scale);
- Matrix t = Matrix.CreateTranslation(position);
- orientation = s * rot * t;
- CalculateBoundingSphere();
- }
- protected virtual void CalculateBoundingSphere()
- {
- // create a sphere based on largest scaling factor
- boundingSphere.Radius = Math.Max(World.M11, Math.Max(World.M22, World.M33));
- boundingSphere.Center = World.Translation;
- }
- // ---- Update -----------------------------------------------------
- // ------------------------------------------------------------------
- public virtual void Update(GameTime gameTime)
- {
- CalculateBoundingSphere();
- }
- // ------------------------------------------------------------------
- public virtual void InfluenceModelRotation(Quaternion quat)
- {
- // rotate in model space by rotating normally then
- // replacing the translation component to the original
- Vector3 pos = orientation.Translation;
- orientation *= Matrix.CreateFromQuaternion(quat);
- orientation.Translation = pos;
- }
- // ------------------------------------------------------------------
- public virtual void InfluenceModelTranslation(Vector3 translation)
- {
- Vector3 pos;
- Vector3 scale;
- Quaternion rot;
- orientation.Decompose(out scale, out rot, out pos);
- // transform the translation to model space
- translation = Vector3.Transform(translation, rot);
- // update model based on model-space translation
- orientation *= Matrix.CreateTranslation(translation);
- }
- // ------------------------------------------------------------------
- #endregion
- #region Properties
- // ------------------------------------------------------------------
- public Matrix World
- {
- get
- {
- if (parent != null)
- return orientation * parent.World;
- else
- return orientation;
- }
- }
- // ------------------------------------------------------------------
- public Vector3 Forward { get { return World.Forward; } }
- // ------------------------------------------------------------------
- public Vector3 Up { get { return World.Up; } }
- // ------------------------------------------------------------------
- public Vector3 Right { get { return World.Right; } }
- // ------------------------------------------------------------------
- public Vector3 Position { get { return World.Translation; } }
- // ------------------------------------------------------------------
- public Vector3 Scale
- {
- get
- {
- if (parent != null)
- {
- float x, y, z;
- x = parent.World.M11 * orientation.M11;
- y = parent.World.M22 * orientation.M22;
- z = parent.World.M33 * orientation.M33;
- return new Vector3(x, y, z);
- }
- else
- return new Vector3(orientation.M11, orientation.M22, orientation.M33);
- }
- }
- // ------------------------------------------------------------------
- public Matrix Orientation
- {
- get { return orientation; }
- set { orientation = value; }
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement