【发布时间】:2012-11-22 03:11:11
【问题描述】:
我的游戏中有一个对象,它有几个网格,当我尝试以任一方式旋转任一网格时,它只会围绕世界轴旋转,而不是围绕其局部轴旋转。我在类构造函数中有一个rotation = Matrix.Identity。每个网格都附加了这个类。那么这个类也包含方法:
...
public Matrix Transform{ get; set; }
public void Rotate(Vector3 newRot)
{
rotation = Matrix.Identity;
rotation *= Matrix.CreateFromAxisAngle(rotation.Up, MathHelper.ToRadians(newRot.X));
rotation *= Matrix.CreateFromAxisAngle(rotation.Right, MathHelper.ToRadians(newRot.Y));
rotation *= Matrix.CreateFromAxisAngle(rotation.Forward, MathHelper.ToRadians(newRot.Z));
CreateMatrix();
}
private void CreateMatrix()
{
Transform = Matrix.CreateScale(scale) * rotation * Matrix.CreateTranslation(Position);
}
...
现在是 Draw() 方法:
foreach (MeshProperties mesh in model.meshes)
{
foreach (BasicEffect effect in mesh.Mesh.Effects)//Where Mesh is a ModelMesh that this class contains information about
{
effect.View = cam.view;
effect.Projection = cam.projection;
effect.World = mesh.Transform;
effect.EnableDefaultLighting();
}
mesh.Mesh.Draw();
}
编辑:
恐怕我在某个地方搞砸了,或者你的技术不起作用,这就是我所做的。每当我移动整个对象(父对象)时,我都会将其 Vector3 Position; 设置为该新值。我还将每个 MeshProperties Vector3 Position; 设置为该值。然后在 CreateMatrix() 的 MeshProperties 中我这样做了:
...
Transform = RotationMatrix * Matrix.CreateScale(x, y, z) * RotationMatrix * Matrix.CreateTranslation(Position) * Matrix.CreateTranslation(Parent.Position);
...
地点:
public void Rotate(Vector3 newRot)
{
Rotation = newRot;
RotationMatrix = Matrix.CreateFromAxisAngle(Transform.Up, MathHelper.ToRadians(Rotation.X)) *
Matrix.CreateFromAxisAngle(Transform.Forward, MathHelper.ToRadians(Rotation.Z)) *
Matrix.CreateFromAxisAngle(Transform.Right, MathHelper.ToRadians(Rotation.Y));
}
而Rotation 是Vector3。
RotationMatrix 和 Transform 在构造函数中都设置为 Matrix.Identity。
问题是,如果我尝试围绕 Y 轴旋转,他应该在“静止不动”时旋转一圈。但他在旋转时四处移动。
【问题讨论】: