Right, so far I have managed to create a Quaternion for rotation. Only problem is now, how do I apply it to only certain cubes? As when I press the right key on the keyboard at the moment, every cube is being rotated continiously around the origin.
For reference, I have 8 cubes positioned in a similar set-up to that of a Rubik's Cube (2x2x2). So when I press the right/left arrow, the right/left face of the 'Cube' (big cube made up of the 8 smaller cubes), rotates 90 degrees clockwise/anti-clockwise.
An example of one of the Cubes (out of eight cubes in total) declaration:
view sourceprint?1 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;
}
19
20 // if key is pressed, change value of rotation
21 if (keys.IsKeyDown(Keys.Up))
22 {
23 updownRotation = 0.1f;
24 }
// rotation around axis
29 Quaternion addRot = Quaternion.CreateFromAxisAngle(new Vector3(1.0f, 0.0f, 0.0f), leftrightRot);
30
31 //rotation of cubes
32 cubeRotation = cubeRotation * addRot;
My draw function:
void DrawGameObject(GameObject gameobject)
02 {
//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();
}
}
}
What I think is the problem is that matrix.createtranslation(gameobject.position) is obviously affecting all my cubes. I've tried creating new vector3 i.e: c_component1 = vector3.transform(cube1pos, cubeRotation) ; but even then, I am unsure where to put that and actually use it.
Any ideas anyone? Any help will be much appreciated.
-DaveT