几天前,我一直在使用XNA开发某个项目。该应用程序的主要思路是创建一个平面着色瓷砖网格,每个瓷砖都可以单独翻转。我遇到的问题是无法确定如何围绕各自中心轴旋转瓷砖。
现在发生的情况是瓷砖围绕世界坐标系旋转,而不是围绕自身旋转。
每个瓷砖由一组顶点组成,在三维世界中创建了一个二维正方形。
我相信问题与我搞错了矩阵有关。
以下是每个单独瓷砖的代码:
如果有帮助的话,我可以将整个源代码发送给你。任何帮助都是无价之宝!
现在发生的情况是瓷砖围绕世界坐标系旋转,而不是围绕自身旋转。
每个瓷砖由一组顶点组成,在三维世界中创建了一个二维正方形。
我相信问题与我搞错了矩阵有关。
以下是每个单独瓷砖的代码:
public Tile(Vector3 position, int size, Matrix world)
{
this.position = position;
this.size = size;
this.world = world;
vertices = new VertexPositionColor[6];
for (int i = 0; i < vertices.Length; i++)
vertices[i].Position = position;
vertices[0].Position += new Vector3(0, 0, 0);
vertices[0].Color = Color.Pink;
vertices[1].Position += new Vector3(0, size, 0);
vertices[1].Color = Color.Yellow;
vertices[2].Position += new Vector3(0, size, size);
vertices[2].Color = Color.Green;
vertices[3].Position += new Vector3(0, size, size);
vertices[3].Color = Color.Green;
vertices[4].Position += new Vector3(0, 0f, size);
vertices[4].Color = Color.Blue;
vertices[5].Position += new Vector3(0, 0, 0);
vertices[5].Color = Color.Blue;
}
public VertexPositionColor[] Vertices
{
get{ return Functions.TransformVertices((VertexPositionColor[])vertices.Clone(), GetMatrix()); }
}
private Matrix GetMatrix()
{
return Matrix.CreateRotationZ(rot);
}
private void TransformVertices(Matrix matrix)
{
for (int i = 0; i < vertices.Length; i++)
vertices[i].Position = Vector3.Transform(vertices[i].Position, matrix);
}
这里是绘制方法:
protected override void Draw( GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
RasterizerState rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.None;
GraphicsDevice.RasterizerState = rasterizerState;
viewMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0, 0, 0), Vector3.Up);
effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];
effect.Parameters["xView"].SetValue(viewMatrix);
effect.Parameters["xProjection"].SetValue(projectionMatrix);
Vector3 rotAxis = new Vector3(0, 0, 1);
rotAxis.Normalize();
worldMatrix = Matrix.Identity;
effect.Parameters["xWorld"].SetValue(worldMatrix);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
foreach(Tile tile in tiles)
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, tile.Vertices, 0, 2, VertexPositionColor.VertexDeclaration);
}
base.Draw(gameTime);
}
如果有帮助的话,我可以将整个源代码发送给你。任何帮助都是无价之宝!