Click here to Skip to main content
15,886,519 members
Articles / Multimedia / DirectX

DirectX Board Game Engine

Rate me:
Please Sign up or sign in to vote.
4.64/5 (19 votes)
29 Nov 2007CPOL11 min read 91.2K   1.7K   50  
An article on how to create a generic engine for board games such as Checkers or Chess
using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Bornander.Games.Direct3D
{
    /// <summary>
    /// This class represents a mesh with a material, and the model can also be highlighted or selected, 
    /// in which case it is rendered with an alternative material.
    /// This class is used to represent a 3D object in 3D space.
    /// </summary>
    public class Model
    {
        #region Private members

        private Mesh mesh;
        
        /// <summary>
        /// Array containing to materials, at 0 the normal material and at 1 the highlighted or selected material.
        /// </summary>
        private Material[] material;
        private bool selected;
        protected Vector3 position;

        /// <summary>
        /// This is a fixed offset used when translating the <code>Model</code>, it is used to create a new "origin" for the mesh. 
        /// </summary>
        private Vector3 positionOffset;
        protected Quaternion orientation;

        #endregion 

        public Model(Mesh mesh, Material[] material)
        {
            this.mesh = mesh;
            this.material = material;
            this.selected = false;
            this.position = Vector3.Empty;
            this.orientation = Quaternion.Identity;
            this.positionOffset = Vector3.Empty;
        }

        public Model(Mesh mesh, Material[] material, Vector3 position)
        {
            this.mesh = mesh;
            this.material = material;
            this.selected = false;
            this.position = position;
            this.orientation = Quaternion.Identity;
            this.positionOffset = Vector3.Empty;
        }

        public void Render(Device device)
        {
            device.Transform.World = this.World;
            device.Material = material[selected ? 1 : 0];
            mesh.DrawSubset(0);
        }

        #region Public properties

        public Boolean Selected
        {
            get { return selected; }
            set { selected = value; } 
        }

        public Matrix World
        {
            // Retrn the world matrix, used when setting the world matrix for this model on the Direct3D device
            get { return Matrix.RotationQuaternion(orientation) * Matrix.Translation(position + positionOffset); }
        }
        
        public Mesh Mesh
        {
            get { return mesh; }
        }

        public Vector3 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Quaternion Orientation
        {
            get { return orientation; }
            set { orientation = value; }
        }

        public Vector3 PositionOffset
        {
            get { return positionOffset; }
            set { positionOffset = value; }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
Article videos
Oakmead Apps Android Games

21 Feb 2014: Best VB.NET Article of January 2014 - Second Prize
18 Oct 2013: Best VB.NET article of September 2013
23 Jun 2012: Best C++ article of May 2012
20 Apr 2012: Best VB.NET article of March 2012
22 Feb 2010: Best overall article of January 2010
22 Feb 2010: Best C# article of January 2010

Comments and Discussions