The ModelObject Component

The ModelObject component implements DrawableGameComponent and draws a 3D model and provides some basic physics.

Properties

  • ModelName - Gets or Sets the name of the model. This is loaded automatically by the component model when LoadContent is called.
  • Model - Gets the Model associated with the component.
  • Position - Gets or sets the Position of the model in 3D space
  • Rotation - Gets or sets the Rotation of the model on all 3 axes (X = pitch, Y = yaw, Z = roll).
  • Scale - Gets or sets the Scale of the model.
  • Velocity - Gets or sets the Velocity as a 3D vector.
  • IsAlive - Gets or sets an indicator of whether the object is "Alive". If true, the position is updated during the Update method based on the velocity and the object is drawn during the Draw method. If false, the object is not updated or drawn This also sets the Visible property of the base DrawableGameComponent.
  • Camera - Gets or sets the Camera used to render the model.

Constructors

        /// <summary>
        /// Initializes a new instance of the ModelObject class for the specified Game.
        /// </summary>
        /// <param name="game">The Game the component sould be attached to</param>
        public ModelObject(Game game)
            : base(game)
        {
            Visible = false;
        }

        /// <summary>
        /// Initializes a new instance of the ModelObject class for the specified Game and model.
        /// </summary>
        /// <param name="game">The Game the component sould be attached to</param>
        /// <param name="modelName">The name of the model to be loaded</param>
        public ModelObject(Game game, string modelName)
            : base(game)
        {
            ModelName = modelName;
            Visible = false;
        }

        /// <summary>
        /// Initializes a new instance of the ModelObject class for the specified Game and model.
        /// </summary>
        /// <param name="game">The Game the component sould be attached to</param>
        /// <param name="model">Model to be rendered by the component</param>
        public ModelObject(Game game, Model model)
            : base(game)
        {
            _model = model;
            Visible = false;
        }

LoadContent method

        /// <summary>
        /// Loads the model specified by the ModelName property.
        /// </summary>
        protected override void LoadContent()
        {
            _model = Game.Content.Load<Model>(ModelName);
            base.LoadContent();
        }

Draw Method

        /// <summary>
        /// Draws the 3D model using the assigned Camera.
        /// </summary>
        /// <param name="gameTime">Time passed since the last call to Draw</param>
        public override void Draw(GameTime gameTime)
        {
            foreach (ModelMesh mesh in Model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;

                    effect.World = Matrix.CreateFromYawPitchRoll(
                        Rotation.Y,
                        Rotation.X,
                        Rotation.Z) *
                        Matrix.CreateScale(Scale) *
                        Matrix.CreateTranslation(Position);
                    effect.Projection = Camera.ProjectionMatrix;
                    effect.View = Camera.ViewMatrix;
                }
                mesh.Draw();
            }
            base.Draw(gameTime);
        }

Last edited Mar 29, 2009 at 6:30 PM by douglampe, version 2

Comments

No comments yet.