到目前为止,我已经设法创建了一个Quaternion
for 旋转。现在唯一的问题是,我如何将它仅应用于某些立方体?就像我此刻按下键盘上的右键一样,每个立方体都围绕原点不断旋转。
作为参考,我将 8 个立方体放置在与魔方 (2x2x2) 类似的设置中。因此,当我按下右/左箭头时,“立方体”(由 8 个较小立方体组成的大立方体)的右/左面顺时针/逆时针旋转 90 度。
一个多维数据集(总共八个多维数据集)声明的示例:
GameObject subCube3 = new GameObject();
Vector3 subCube3Pos = new Vector3(-0.55f, -0.55f, 0.55f);
在我的更新方法中:
// 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;
我的绘图功能:
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
ie:c_component1 = Vector3.Transform(cube1pos, cubeRotation);
但即便如此,我也不确定该放在哪里并实际使用它。
有什么想法吗?任何帮助都感激不尽。