C# XNA 摄像机位置遵循混凝土网格/骨骼



如何使相机位置取决于模型中特定元素(网格/骨骼)的旋转和位置。

 #region CameraModeEye
 if (mode == CameraMode.Eye) // do poprawienia
 {
     Matrix[] transforms = new Matrix[objectModel.Model.Bones.Count];
     objectModel.Model.CopyBoneTransformsTo(transforms);
     // test
     Matrix World = (transforms[objectModel.Model.Meshes["Eye"].ParentBone.Index] * objectModel.Transform) * transforms[3];
     new_position = transforms[3].Forward;
     new_lookAt = new_position + Vector3.Forward;
     camVector = Vector3.Up;
 }
 #endregion
 ...
 view = Matrix.CreateLookAt(new_position, new_lookAt, camVector);

我有一个机器人模型(蝎子的形状),它具有以下元素:身体,头部,眼睛。

"

头部"随轴X旋转,并取决于"主体"并且"眼睛"随Y轴旋转,并且依赖于"头部"。

我希望相机位置稍微远离"眼睛"。我已经尝试了很多方法,但每次我都得到这样的东西:

World.Forward {X: -2,301742E-24 Y: 3,456487E-14 Z -1}

其中实际上应该是这样的:

              {X: -10.0         Y: 500.0        Z: 0}

我无法理解。我厌倦了两天,找不到解决方案。请帮忙

if (mode == eye)//pseudoed
{
  Matrix[] transforms = new Matrix[objectModel.Model.Bones.Count];
     objectModel.Model.CopyAbsoluteBoneTransformsTo(transforms);
  Vector3 cameraPosition = (transforms["eye"] * objectModel.Transform).Translation;//I'm assuming objectModel.Transform is the world matrix for the model 
  Vector3 cameraLookAt = cameraPosition + transforms[eye].Forward;//camera looking in the same direction as the eye
  view = Matrix.CreateLookAt(cameraPosition, cameraLookAt, Vector3.Up);
}

这段代码应该传达执行我认为您正在尝试执行的操作的方法。 请注意,我使用CopyAbsoluteBoneTransformsTo而不仅仅是CopyBoneTransformsTotransforms[eye]的绝对版本实际上是将眼睛乘以头部到身体的结果,它给出了眼睛的世界空间位置和方向。而不使用绝对版本的transforms[eye]只是眼睛和头部之间的旋转和平移差异(这是您的代码使用的)。

你的 World.Forward 不应该给出 -10,500,0,但可能(World.Translation + World.Forward)可能会。

OOO,正是我的意思。

后来我明白我应该使用"CopyAbsoluteBoneTransformsTo"而不是"CopyBoneTransformsTo"。最后,我写了这段代码,但他是我的11行。: D

现在,在 Matrix 上的操作只需要 3 行:

 Matrix[] transforms = new Matrix[objectModel.Model.Bones.Count];
 objectModel.Model.CopyAbsoluteBoneTransformsTo(transforms);
 Matrix offset = Matrix.CreateTranslation(Vector3.Down * 70 + Vector3.Forward * 10);
 new_position = (offset * transforms[3] * objectModel.Transform).Translation;
 new_lookAt = new_position + transforms[3].Down; 

我不得不添加一个额外的矩阵(它全局存储在教室中),它负责移动"眼睛"元素模型的位置。否则,相机位置在"眼睛"内,视图隐藏在墙模型后面。


这只是另一个小问题。也就是说,没有旋转轴 Z。我不知道他会如何解释。

假设:

  • 人类的脖子是我的骨头"头",你只能向前和向后旋转它。

  • 人头是我的骨头"眼睛",你只能左右旋转它。

当旋转到颈部向后时,头部向左或向右旋转,我们以一定角度看到图像。

我的机器人一直在前面看,好像他们只是错过了Z轴旋转

我应该在最后使用这样的东西吗?

 view * = Matrix.CreateRotationZ (MathHelper.ToRadians (Rotz));

最新更新