thm*_*msn 6 c# math xna quaternions
我一直在关注The Hazy Mind的XNA教程我有一个基础对象,它有一个Position(Vector3)和一个Rotation(Quaternion).对象模型看起来像这样

从教程中的相机实现,我已经制作了旋转和旋转方法的副本,并在Object上实现了它们
public virtual void Rotate(Vector3 axis, float angle)
{
axis = Vector3.Transform(axis, Matrix.CreateFromQuaternion(Rotation));
Rotation = Quaternion.Normalize(Quaternion.CreateFromAxisAngle(axis, angle) * Rotation);
}
public virtual void Revolve(Vector3 target, Vector3 axis, float angle)
{
Vector3 revolveAxis = Vector3.Transform(axis, Matrix.CreateFromQuaternion(Rotation));
Quaternion rotate = Quaternion.Normalize(Quaternion.CreateFromAxisAngle(revolveAxis, angle));
Position = Vector3.Transform(Position - target, Matrix.CreateFromQuaternion(rotate)) + target;
Rotate(axis, angle);
}
Run Code Online (Sandbox Code Playgroud)
My Block对象创建了6个Quad实例,并在每个Quad对象上调用render方法,我在每个Quad上分配了偏移和旋转,形成一个纹理块,如下所示:

现在我们遇到了实际问题,在我的Block对象上调用Rotate会导致对象本身改变旋转,而我的Quad对象不会旋转,因此我在Block对象上实现了一个新的Rotate函数.
public override void Rotate(Vector3 axis, float angle)
{
for (int i = 0; i < mySides.Length; i++)
{
Quad side = mySides[i];
side.Revolve(this.Position, axis, angle);
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这并没有给我预期的结果,如下所示:

错误的解决方案
以下解决方案将起作用但不会非常通用并且将受到限制:一堆if语句检查我想要旋转的轴,然后以正确的方式旋转每一侧(Quad对象).
适当的解决方案
或者,我可以使Quad对象的渲染依赖于Block对象的旋转(它们的父对象),但我不确定如何执行此操作.
第三个也是最后一个选项是让Rotate方法更新每个Quad对象的旋转和位置到正确的值,这个我不确定如何做.
问题
所以我要求的是对此的一些想法或想法,可能是一些链接和/或教程解释如何在XNA中使用Quaternions,最好是以可视方式.
干杯,期待您的投入:)
您应该做什么(您提到过这一点),使四边形对象的渲染依赖于父对象的旋转。
对于像您这样的分层系统来说,这很容易做到。当渲染器去渲染块时,它应该将块的变换存储在矩阵中,我们称之为BlockWorld。当您渲染每个 Quad 时,将 Quad 的局部变换乘以BlockWorld。
| 归档时间: |
|
| 查看次数: |
268 次 |
| 最近记录: |