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)
{
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);, , .
- ? .