В течение нескольких дней я работаю над проектом в XNA. Основная идея приложения - иметь сетку из плоских цветных плит, которые индивидуально можно перевернуть. Проблема, с которой я сталкиваюсь, заключается в том, что я не могу понять, как сделать поворот флип вокруг плиток на отдельной центральной оси.Вращающиеся 3D примитивы вокруг собственной оси
Вещь, которая происходит сейчас, состоит в том, что плитки вращаются вокруг оси мира, а не ее собственной.
Каждая плитка состоит из массива вершин, который создает двумерный квадрат в 3D-мире.
Я вполне уверен, что проблема связана с тем, что я перепутал с матрицами.
Вот код для каждой отдельной плитки:
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);
}
Если это поможет, я могу отправить вам весь источник. Любая помощь будет бесценной!
Спасибо! Может ли кто-нибудь показать, как это сделать практически? – Pijm0
Matrix.CreateTranslation (-tilePosition) * Matrix.CreateTranslation (0, -size/2, -size/2) * Matrix.CreateRotationX (angle) * Matrix.CreateTranslation (tilePosition) – Blau
Помогло ли downvoter объяснить, почему они голосовали отрицательно ? – Paddyd