Best way to translate mouse drag motion into 3d rotation of an object

前端 未结 4 2047
孤街浪徒
孤街浪徒 2020-12-28 08:41

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 t

4条回答
  •  -上瘾入骨i
    2020-12-28 09:05

    Create an accumulator matrix and initialize it with the identity.

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

    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.

提交回复
热议问题