本文介绍了使用 System.Numerics.Quaternion 的 3D 旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有人知道如何使用 .net(4.6 及更高版本)System.Numerics.Quaternion 旋转 vector3?

Anybody here know how to rotate a vector3 using .net(4.6 and up) System.Numerics.Quaternion?

虽然我的数学很差,但我的理解只是:四元数是 4d 的结构",可以在 3d 中产生平移、缩放和旋转.

My maths is pretty poor though and my understanding only really goes as far as: quaternions are 4d "structures" that can produce translation, scaling and rotation in 3d.

所以我玩了一场,不能轮换.做看起来很明显的事情:改变四元数的 W 分量.(角度)然后读取向量产生缩放?!?任何人都可以帮助或指出正确的方向寻求帮助?

So I had a play and cannot get any rotation. Doing what seemed obvious : changing the W component of the Quaternion.(angle) then reading the vector produces scaling ?!? Anyone able to help or point me in the right direction for help?

我当前的旋转(非四元数)代码(X轴示例)

My current rotation (non-quaternion) code (X-axis example)

Private Sub Xaxis_rotation(ByVal angle As Double)
        Dim Cangle As Double = Cos(angle)
        Dim Sangle As Double = Sin(angle)
        Parallel.For(1, vertcount, Sub(f As Int32)
                                       Verts(f) -= modelcenter
                                       Verts(f).Y = (Verts(f).Y * Cangle) + (Verts(f).Z * Sangle)
                                       Verts(f).Z = (Verts(f).Z / Cangle) - (Verts(f).Y * Sangle)
                                       Verts(f) += modelcenter
                                   End Sub)
    End Sub
    Dim rotAxis As Vector3 = Vector3.UnitX
    Dim rotangle As Single =  0.785398 '45 degrees as radians
    Dim q As Quaternion = Quaternion.CreateFromAxisAngle(rotAxis, rotangle)
    Dim aVector As Vector3 = *vector to be rotated*

    'rotate
    Dim resultQ As Quaternion = q * Quaternion.CreateFromAxisAngle(aVector, 0) / q

    aVector.X = resultQ.X
    aVector.Y = resultQ.Y
    aVector.Z = resultQ.Z

q*Quaternion.CreateFromAxisAngle(aVector, 0)/q 是我最好的猜测,但它不会产生旋转.

q*Quaternion.CreateFromAxisAngle(aVector, 0) / q is my best guess but it does not produce rotation.

推荐答案

最后更新:

下面使用 Vector3.Transform 的代码比之前定义 resultq 四元数的方法更快并且看起来更准确.

The code below using Vector3.Transform is faster and appears more accurate than the previous method, defining the resultq Quaternion.

Public Shared Sub Rotate_point(ByRef Vect As Vector3, CentPoint As Vector3, angles As Vector3)
    Dim Rotator As Quaternion = Quaternion.CreateFromYawPitchRoll(angles.Y, angles.X, angles.Z) 'rotation in radians
    Vect -= CentPoint '---------The point to rotate around.
    Vect = Vector3.Transform(Vect, Rotator)
    Vect += CentPoint
 End Sub

这篇关于使用 System.Numerics.Quaternion 的 3D 旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 13:47