XNA--Object Transformation

         A transformation is stored in a matrix (which is a 4 × 4 matrix of floats). The transformation that stores the position and orientation of an object in your game is called the World 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 the world 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 multiplying the 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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值