Rotating 3D primitives around its own axis
For a few days now I've been working on a project in XNA. The main idea of
the application is to have a grid of flat colored tiles that individually
can be flipped. The problem that im having is that I can't really figure
out how to do the flip rotation around the tiles individual central axis.
The thing that is happening now is that the tiles rotates around the
worlds axis instead of its own.
Each tile consists of an array of vertices that creates a two-dimensional
square in the 3D-world.
I'm pretty certain that the problem has to do with me messing up with the
matrices.
Here is the code for each individual tile:
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);
}
And here is the drawing method:
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);
}
If it helps I can send you the whole source. Any help would be priceless!
No comments:
Post a Comment