将鼠标拖动运动转换为对象 3d 旋转的最佳方法 [英] Best way to translate mouse drag motion into 3d rotation of an object

查看:20
本文介绍了将鼠标拖动运动转换为对象 3d 旋转的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 3d 对象,我希望能够在 3d 中旋转.最简单的方法是直接将 X 和 Y 鼠标运动转换为绕 Y 轴和 X 轴旋转,但如果沿两个轴都有一些旋转,模型的旋转方式就会变得非常违反直觉(即,如果将对象翻转 180 度约一个轴,你沿另一个轴的运动是相反的).

I have a 3d object that I wish to be able to rotate around in 3d. The easiest way is to directly translate X and Y mouse motion to rotation about the Y and X axes, but if there is some rotation along both axes, the way the model rotates becomes highly counterintuitive (i.e. if you flip the object 180 degrees about one axis, your motion along the other axis is reversed).

我可以简单地执行上述方法,但不是存储绕两个轴旋转的量,我可以存储完整的旋转矩阵,并在每次鼠标拖动时沿相同的轴进一步旋转它,但我很担心那很快就会出现精度问题.

I could simply do the above method, but instead of storing the amount to rotate about the two axes, I could store the full rotation matrix and just further rotate it along the same axes for each mouse drag, but I'm concerned that that would quickly have precision issues.

推荐答案

创建一个累加器矩阵并用身份初始化它.

Create an accumulator matrix and initialize it with the identity.

每一帧,在绘制对象之前将其应用到您的模型视图/世界矩阵状态.

Each frame, apply that to your modelview/world matrix state before drawing the object.

根据鼠标移动,构建一个绕 X 轴的旋转矩阵,其中包含一些灵敏度_常数 * delta_x.为另一个组件构建另一个绕 Y 轴的旋转矩阵.乘以一个,然后将另一个乘以累加器.

Upon mouse motion, construct a rotation matrix about the X axis with some sensitivity_constant * delta_x. Construct another rotation matrix about the Y axis for the other component. Multiply one, then the other onto the accumulator.

当您移动鼠标时,累加器会发生变化.绘制时,它会按照您的预期定向对象.

The accumulator will change as you move the mouse. When drawing, it will orient the object as you expect.

此外,谈论四元数的人是对的;这仅适用于小的增量更改.如果您在对角线上快速拖动它,它不会像您预期的那样旋转.

Also, the person talking about quaternions is right; this will look good only for small incremental changes. If you drag it quickly on a diagonal, it won't rotate quite the way you expect.

这篇关于将鼠标拖动运动转换为对象 3d 旋转的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆