How to rotate a 3D cube in the center of XNA?

I am trying to rotate a 3D cube towards itself from its center and not from the edge. Here is my code.

public rotatemyCube()
{
    ...
    Matrix newTransform = Matrix.CreateScale(scale) * Matrix.CreateRotationY(rotationLoot) * Matrix.CreateTranslation(translation);
    my3Dcube.Transform = newTransform;
    ....


public void updateRotateCube()
{
    rotationLoot += 0.01f;
}

      

My cube rotates perfectly, but not from the center. Here is a diagram that explains my problem. enter image description here

And I need this: enter image description here

my complete code

private void updateMatriceCubeToRotate()
    {
        foreach (List<instancedModel> ListInstance in listStructureInstance)
        {
            foreach (instancedModel instanceLoot in ListInstance)
            {
                if (my3Dcube.IsAloot)
                {

                    Vector3 scale;
                    Quaternion rotation;
                    Vector3 translation;
                    //I get the position, rotation, scale of my cube
                    my3Dcube.Transform.Decompose(out scale,out rotation,out translation);


                    var rotationCenter = new Vector3(0.1f, 0.1f, 0.1f);

                    //Create new transformation with new rotation
                    Matrix transformation =
                        Matrix.CreateTranslation(- rotationCenter)
                        * Matrix.CreateScale(scale)
                        * Matrix.CreateRotationY(rotationLoot)
                        * Matrix.CreateTranslation( translation);

                    my3Dcube.Transform = transformation;


                }
            }
        }
        //Incremente rotation 
        rotationLoot += 0.05f;
    }

      

+3


source to share


1 answer


A rotational matrix rotates vertices around the origin of the coordinate system. To rotate around a specific point, you must make it the source. This can be done by simply subtracting the pivot point from each vertex in the shape.

three drawings showing a roataed cube from the top



var rotationCenter = new Vector3(0.5f, 0.5f, 0.5f);

Matrix transformation = Matrix.CreateTranslation(-rotationCenter)
    * Matrix.CreateScale(scaling) 
    * Matrix.CreateRotationY(rotation) 
    * Matrix.CreateTranslation(position);

      

+6


source







All Articles