在我的Windows手机应用程序中,我想使用屏幕触控输入旋转3d模型。
问题是;
一开始一切正常,我可以使用触摸移动模型但是当我通过X轴的旋转使对象上下颠倒时,Y轴旋转变为倒置。 那是因为我的世界轴也发生了变化。 我尝试了很多方法。
第一
Matrix world = Matrix.Identity *Matrix.CreateRotationY( somerotation);
world = world * Matrix.CreateRotationX( somerotation );
world *= Matrix.CreateTranslation(0, 0, 0);
第二
Matrix world = Matrix.Identity * Matrix.CreateFromYawPitchRoll(somerotation,somerotation,0);
world *= Matrix.CreateTranslation(0, 0.0f, zoomXY);
第三
Matrix world = Matrix.Identity *Matrix.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0),somerotation));
world *= Matrix.CreateFromQuaternion(Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), somerotation));
world *= Matrix.CreateTranslation(0,0,0);
第四
Matrix world = Matrix.Identity;
world = Matrix.CreateFromAxisAngle(Vector3.Up,somerotation);
world *= Matrix.CreateFromAxisAngle(Vector3.Right,somerotation);
world *= Matrix.CreateTranslation(0, 0,0);
结果是一样的......现在我的思绪在无法控制地旋转。
如何使用旋转后不会改变的静态轴?还是其他任何建议?
感谢。
答案 0 :(得分:1)
问题是你的数学没有错。第二轴运动被反转,因为这是从相反方向观察时的正确运动,这是通过旋转另一轴引起的。
不是每帧都从头开始围绕固定轴创建旋转,而是尝试存储一些当前的方向向量(向上和向右,向前和向左),并将关于这些方向的小增量旋转应用到持久性世界矩阵。当然,您还必须对您的方向应用相同的更改。
这样,无论您的矩阵当前面向哪个方向,您都可以始终相对于它旋转并朝着您想去的方向旋转。
编辑(代码):
class gameclass
{
Vector3 forward = Vector3.UnitZ; //persistent orientation variables
Vector3 left = -1 * Vector3.UnitX;
Vector3 up = Vector3.UnitY
Matrix world = Matrix.Identitiy;
InputClass inputputclass; //something to get your input data
void Update()
{
Vector3 pitch = inputclass.getpitch(); //vertical swipe
forward = Vector3.transform(forward,
Matrix.CreateFromAxisAngle(left, pitch));
up = Vector3.transform(up,
Matrix.CreateFromAxisAngle(left, pitch));
Vector3 yaw = inputclass.getyaw(); //horizontal swipe
forward = Vector3.transform(forward,
Matrix.CreateFromAxisAngle(up, yaw));
left = Vector3.transform(left,
Matrix.CreateFromAxisAngle(up, yaw));
forward.Normalize(); left.Normalize(); top.Normalize(); //avoid rounding errors
world = Matrix.CreateWorld(
postition //this isn't defined in my code
forward,
up);
}
}
拥有自由的球形旋转并不简单。 :)