我有一个关于单个网格(建筑物)的问题,其中包含一组动画对象(这些对象组成整个建筑物,在构造时从地面上升,以不同的速度和旋转).
网格中的动画对象都有自己的位置和旋转动画轨迹.我将它从3DS Max导出为ASE,将其转换为适当的格式并在我的游戏引擎中使用它.
所以,目前,我可以成功地显示网格中所有动画对象的动画位置.执行以下操作:
比例,旋转和平移变量是建筑物的转换信息.这对所有子对象都有效.pos变量表示相对于建筑物中心的子对象位置.
foreach (GeometricObject geoObject in mesh.GeoObjects)
{
Vector3 pos = geoObject.AnimationData.PositionTrack[(int)m_keyFrameTime];
Vector3 translatedPos = new Vector3(translation.X + pos.X,
translation.Y + pos.Y,
translation.Z + pos.Z)
_eRenderer.ActiveCamera.World = Matrix.CreateScale(scale) *
Matrix.CreateFromQuaternion(rotation) *
Matrix.CreateTranslation(translatedPos);
}
Run Code Online (Sandbox Code Playgroud)
这将完美地显示动画建筑(从地面升起),并且所有子对象都处于正确的位置.
现在,当我尝试旋转子对象时,问题出现了.我尝试了很多不同的方法,但在旋转子对象后,子对象的转换似乎总是搞砸了.这是我现在拥有的,但它仍然搞砸了:
foreach (GeometricObject geoObject in mesh.GeoObjects)
{
Vector3 pos = geoObject.AnimationData.PositionTrack[(int)m_keyFrameTime];
Vector3 translatedPos = new Vector3(translation.X, translation.Y, translation.Z)
Matrix rotateSubObjects;
rotateSubObjects = Matrix.CreateTranslation(new Vector3(pos.X, pos.Y, pos.Z));
rotateSubObjects *= Matrix.CreateFromQuaternion(geoObject.AnimationData.RotationTrack[(int)m_keyFrameTime]);
_eRenderer.ActiveCamera.World = Matrix.CreateScale(scale) *
Matrix.CreateFromQuaternion(rotation) *
Matrix.CreateTranslation(translatedPos) *
rotateSubObjects;
}
Run Code Online (Sandbox Code Playgroud)
我已经读过,我可能需要将子对象转换回原点,使其围绕原点旋转,然后将其转换回世界位置,但我不知道在这种情况下如何做到这一点.
我没有想法,任何帮助将不胜感激!此致,Riaan.
你正在寻找的转变是这样的:
Matrix positionRotationMatrix = Matrix.CreateTranslation(-parentPosition)
* Matrix.CreateFromQuaternion(parentRotation)
* Matrix.CreateTranslation(parentPosition);
Vector3 translation = Vector3.Transform(parentPosition + relativePosition,
positionRotationMatrix);
Run Code Online (Sandbox Code Playgroud)
使用新的子对象位置(translation变量)定义世界矩阵:
Matrix worldMatrix = Matrix.CreateScale(scale)
* Matrix.CreateFromQuaternion(rotation)
* Matrix.CreateFromQuaternion(parentRotation)
* Matrix.CreateTranslation(translation);
Run Code Online (Sandbox Code Playgroud)
为了演示,这里是一个围绕它的位置旋转的立方体(父级),带有一个箭头,其位置和旋转相对于其父项位置和旋转进行定义.箭头跟随立方体旋转,并独立地绕其Z轴旋转.

用于演示的值:
parentPosition = Vector3(-1.1f, 0f, -2); //cube
relativePosition = Vector3(0, 0, -3); //arrow
parentRotation = Quaternion.CreateFromRotationMatrix(
Matrix.CreateRotationZ(angle)
* Matrix.CreateRotationY(angle)
* Matrix.CreateRotationX(0.5f));
rotation = Quaternion.CreateFromYawPitchRoll(0f, 0f, angle);
angle += 0.05f;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
551 次 |
| 最近记录: |