0

I have an object in my game that has a few meshes and when I try to rotate either of the meshes either way, it only rotates it around world axis, and not its local axis. I have a rotation = Matrix.Identity in a class constructor. Every mesh has this class attached to it. Then this class also contains methods:

...
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);
}
...

And now the Draw() method:

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();
}

EDIT: I am afraid either I screwed somewhere up, or your tehnique does not work, this is what I did. Whenever I move the whole object(Parent), I set its Vector3 Position; to that new value. I also set every MeshProperties Vector3 Position; to that value. And then inside CreateMatrix() of MeshProperties I did like so:

...
Transform = RotationMatrix * Matrix.CreateScale(x, y, z) * RotationMatrix * Matrix.CreateTranslation(Position) * Matrix.CreateTranslation(Parent.Position);
...

Where:

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));
}

And Rotation is Vector3. RotationMatrix and Transform are both set to Matrix.Identity in the constructor.

The problem is if I try to rotate around for example Y axis, he should rotate in a circle while "standing still". But he moves around while rotating.

4

2 回答 2

1

我不完全确定这是你想要的。我在这里假设您有一个对象,其中一些网格和位置偏离主对象位置的位置和方向,并且您希望相对于父对象围绕其局部轴旋转子对象。

Matrix.CreateTranslation(-Parent.Position) *                  //Move mesh back...
Matric.CreateTranslation(-Mesh.PositionOffset) *              //...to object space

Matrix.CreateFromAxisAngle(Mesh.LocalAxis, AngleToRotateBy) * //Now rotate around your axis
Matrix.CreateTranslation(Mesh.PositionOffset) *               //Move the mesh...
Matrix.CreateTranslation(Parent.Position);                    //...back to world space

当然,您通常存储一个变换矩阵,它可以一步将网格从对象空间转换到世界空间,并且您还可以存储逆矩阵。您还将网格始终存储在对象坐标中,并且仅将其移动到世界坐标中进行渲染。这会简化一些事情:

Matrix.CreateFromAxisAngle(Mesh.LocalAxis, AngleToRotateBy) * //We're already in object space, so just rotate
ObjectToWorldTransform *
Matrix.CreateTranslation(Parent.Position);

我认为您可以简单地将示例中的 Mesh.Transform 设置为此并全部设置。

我希望这就是你要找的!

于 2012-11-18T20:45:08.410 回答
1

问题是,当我将模型导出为 .FBX 时,轴心点不在模型中心。从而使模型在旋转时移动。

于 2012-11-21T19:11:11.720 回答