问题描述
我有关于一个网格(建筑),其中包含动画对象的集合(这些对象构成了完整的建筑,当它被建造,以不同的速度和旋转从地面升起)的问题。
I have a question concerning a single mesh (building) that contains a collection of animated objects (these objects make up the complete building, rising up from the ground when it is constructed, at different speeds and rotations).
在网格上的动画对象都有自己的位置和旋转动画轨迹。我从3ds Max软件导出为ASE,将其转换成properietary格式ANDD在我的游戏引擎使用它。
The animated objects in the mesh all have their own position and rotation animation tracks. I export it from 3DS Max as an ASE, convert it into a properietary format andd use it in my gaming engine.
所以,此刻,我可以成功显示所有网格中动画对象的动画位置。执行以下操作:
So, at the moment, I can successfully display the animated positions of all of the animated objects in the mesh. Doing the following:
规模,旋转和平移变量是用于建筑的改造信息。这适用于所有的子对象。该POS变量指示子对象相对于建筑物中心位置。
The scale, rotation and translation variables are the transformation information for the building. This is valid for all the sub-objects. The pos variable indicates the sub-objects position relative to the building centre.
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);
}
这显示动画的建筑(从地面上升)完美之一切分对象是在正确的位置上。
This displays the animated building (rising from the ground) perfectly and alll sub-objects are in their correct positions.
现在,问题就来了时,我尝试旋转子对象。我已经尝试了很多不同的方法,但子对象的翻译似乎总是搞的一团糟我已经旋转子对象之后。这是我目前有,但它仍然是搞砸了:
Now, the problem comes in when I try to rotate the sub-objects. I have tried a quite a few different approaches, but the translation of the sub-objects always seems to get messed up after I have rotated the sub-object. This is what I currently have, but it is still messed up:
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;
}
我已阅读,我可能需要到子对象回转换为其原产地为它绕它的起源,然后将其转换回它的世界上的地位,但我不知道怎么做,在这种情况下。
I have read that I might need to translate the sub-objects back to its origin for it to rotate around its origin, and then translate it back to its world position, but I'm not sure how to do that in this case.
我可选M的想法,任何帮助,将不胜感激!
的问候,
里安
I'm out of ideas, any help would be appreciated!Regards,Riaan.
推荐答案
您正在寻找的转变是这样的:
The transformation you're looking for is this:
Matrix positionRotationMatrix = Matrix.CreateTranslation(-parentPosition)
* Matrix.CreateFromQuaternion(parentRotation)
* Matrix.CreateTranslation(parentPosition);
Vector3 translation = Vector3.Transform(parentPosition + relativePosition,
positionRotationMatrix);
您用新的子对象位置(翻译变量):
You define your world matrix with the new sub-objects position (translation
variable):
Matrix worldMatrix = Matrix.CreateScale(scale)
* Matrix.CreateFromQuaternion(rotation)
* Matrix.CreateFromQuaternion(parentRotation)
* Matrix.CreateTranslation(translation);
为了演示,这里是一个立方体(父母)的正在旋转围绕它的位置,有一个箭头的位置和旋转就其父母的位置和旋转定义的。箭头所示的立方体旋转和绕它的Z轴独立地
To demonstrate, here's a cube (parent) that is being rotated around it's position, with an arrow whose position and rotation are defined in relation to its parents position and rotation. The arrow follows the cubes rotation, and rotates around it's Z axis independently.
用于演示
值:
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;
这篇关于旋转物体附着到其它物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!