XNA--Object Transformation

来源:互联网 发布:网络视频 编辑:程序博客网 时间:2024/05/17 18:28

         A transformation is stored in a matrix (which is a 4 × 4 matrix of floats). The transformation thatstores the position and orientation of an object in your game is called theWorld matrix, as it defines where and how the object is positioned in your world. Other than World matrices, you also need the camera’s View and Projection matrices to transform 3D positions to 2D screen coordinates.

// Translate        Vector3 translate;        // Rotate around the (X, Y, Z) world axes        Vector3 rotate;        // Scale the X, Y, Z axes        Vector3 scale;        bool needUpdate;        // Store the combination of the transformations        Matrix matrix;        public Vector3 Translate        {            get { return translate; }            set { translate = value; needUpdate = true; }        }        public Vector3 Rotate        {            get { return rotate; }            set { rotate = value; needUpdate = true; }        }        public Vector3 Scale        {            get { return scale; }            set { scale = value; needUpdate = true; }        }        public Matrix Matrix        {            get            {                if (needUpdate)                {                    // Compute the final matrix (Scale * Rotate * Translate)                    matrix = Matrix.CreateScale(scale) *                             Matrix.CreateRotationY(MathHelper.ToRadians(rotate.Y)) *                             Matrix.CreateRotationX(MathHelper.ToRadians(rotate.X)) *                             Matrix.CreateRotationZ(MathHelper.ToRadians(rotate.Z)) *                             Matrix.CreateTranslation(translate);                    needUpdate = false;                }                return matrix;            }        }
         The combination of these three transformations is called theworld transformation, as it uniquely defines where and how an object is positioned in the 3D world. You can set and retrieve this matrix attribute through the Matrix property, and it is recalculated whenever the translate, rotate, or scale transformation is updated.

       Notice that the object’s world transformation matrix is calculated by multiplyingthe scale, rotation, and translation transformations of the object, in this order. Because the matrix product is not commutative,the order in which you combine the transformations is very important.