Rotating Cubes in XNA

So far I have managed to create Quaternionfor rotation. Now the only problem is how to apply it only to certain cubes? When I press the right key on the keyboard at the moment, each cube constantly rotates around the origin.

For reference, I have 8 cubes located in a similar configuration with a Rubik cube (2x2x2). Therefore, when I press the right / left arrow, the right / left side of the “cube” (a large cube consisting of 8 smaller cubes) rotates 90 degrees clockwise / counterclockwise.

An example of one of the Cubes expressions (out of eight cubes in total):

GameObject subCube3 = new GameObject();  
Vector3 subCube3Pos = new Vector3(-0.55f, -0.55f, 0.55f); 

In my update method:

// declare rotation floats     
float updownRotation = 0.0f;     
float leftrightRot = 0.0f;     

// get state of keyboard     
KeyboardState keys = Keyboard.GetState();     

// if key is pressed, change value of rotation     
if (keys.IsKeyDown(Keys.Right))     
{     
    leftrightRot = -0.10f;     
}

// if key is pressed, change value of rotation     
if (keys.IsKeyDown(Keys.Left))     
{     
    leftrightRot = 0.1f;     
}     

// if key is pressed, change value of rotation     
if (keys.IsKeyDown(Keys.Up))     
{
    updownRotation = 0.1f;     
}

// rotation around axis     
Quaternion addRot = Quaternion.CreateFromAxisAngle(new Vector3(1.0f, 0.0f, 0.0f), leftrightRot);

//rotation of cubes     
cubeRotation = cubeRotation * addRot; 

My drawing function:

void DrawGameObject(GameObject gameobject)
{     
    //graphics.GraphicsDevice.RenderState.CullMode = CullMode.None;     
    foreach (ModelMesh mesh in gameobject.model.Meshes)     
    {     
        foreach (BasicEffect effect in mesh.Effects)     
        {     
            effect.EnableDefaultLighting();     
            effect.PreferPerPixelLighting = true;     
            effect.World =     
                Matrix.CreateScale(gameobject.scale) *     
                Matrix.CreateFromQuaternion(cubeRotation) *     
                Matrix.CreateTranslation(gameobject.position);     

            effect.Projection = cameraProjectionMatrix;     
            effect.View = cameraViewMatrix;     
        }
        mesh.Draw();     
    }
}

, , Matrix.CreateTranslation(gameobject.position), , . Vector3 i.e: c_component1 = Vector3.Transform(cube1pos, cubeRotation);, , .

- ? .

+3
2

rubix, quat, , . quat (, ). , . (, ), , .

quat , . .

vector3s, .

, , SmallCube 8 . quat Vector3 ( ).

, , .

Foreach(smallCube sc in smallCubes)
{
   if(sc.Position.Y > 0.1f)
   {
     Quaternion addRot = Quaternion.CreateFromAxisAngle(Vector3.Up, MathHelper.ToRadians(90));
     sc.quat *= addRot
     sc.Position = Vector3.Transform(sc.Position, addRot);
   }
}

, , . Quaternion.Dot() . , . , 1.0f. .

+1

- , , , ( XNA ) , GameObject , .

, ...

0

Source: https://habr.com/ru/post/1759113/


All Articles