gpt4 book ai didi

c++ - 确定基于四元数调整方向所需的角速度

转载 作者:行者123 更新时间:2023-12-02 10:25:36 24 4
gpt4 key购买 nike

问题:

我在3D空间中有一个以给定方向存在的对象。我需要将对象重新定向为新方向。我目前将方向表示为四元数,尽管这并非绝对必要。

我本质上需要确定将 body 定向到所需方向所需的角速度。

我目前正在使用的内容如下所示:

伪代码:

// 4x4 Matrix containing rotation and translation
Matrix4 currentTransform = GetTransform();

// Grab the 3x3 matrix containing orientation only
Matrix3 currentOrientMtx = currentTransform.Get3x3();

// Build a quat based on the rotation matrix
Quaternion currentOrientation(currentOrientMtx);
currentOrientation.Normalize();

// Build a new matrix describing our desired orientation
Vector3f zAxis = desiredForward;
Vector3f yAxis = desiredUp;
Vector3f xAxis = yAxis.Cross(zAxis);
Matrix3 desiredOrientMtx(xAxis, yAxis, zAxis);

// Build a quat from our desired roation matrix
Quaternion desiredOrientation(desiredOrientMtx);
desiredOrientation.Normalize();

// Slerp from our current orientation to the new orientation based on our turn rate and time delta
Quaternion slerpedQuat = currentOrientation.Slerp(desiredOrientation, turnRate * deltaTime);

// Determine the axis and angle of rotation
Vector3f rotationAxis = slerpedQuat.GetAxis();
float rotationAngle = slerpedQuat.GetAngle();

// Determine angular displacement and angular velocity
Vector3f angularDisplacement = rotationAxis * rotationAngle;
Vector3f angularVelocity = angularDisplacement / deltaTime;

SetAngularVelocity(angularVelocity);

本质上,这只是使我的对象旋转而被遗忘。我已经验证了通过轴构造的期望OrientMtx确实是正确的最终旋转变换。我觉得我在这里想念一些愚蠢的东西。

有什么想法吗?

最佳答案

为了计算角速度,您的turnRate已经提供了幅度(弧度/秒),因此,您真正需要的只是旋转轴。这只是GetAxis( B * Inverse(A) )给出的。相同数量的GetAngle将给出两者之间移动的总角度。有关更多说明,请参见'Difference' between two quaternions

SetAngularVelocity( Normalize( GetAxis( B * Inverse(A)) ) * turnRate )

您需要在某个点(到达目标方向时)将角速度设置为0。一种方法是使用 quaternion distance。另一个更简单的方法是检查所花费的时间。最后,您可以检查两个夸脱之间的角度(如上所述),并检查该角度是否接近0。
float totalAngle = GetAngle( Normalize( endingPose * Inverse( startingPose ) ) );
if( fabs( totalAngle ) > 0.0001 ) // some epsilon
{
// your setting angular velocity code here
SetAngularVelocity(angularVelocity);
}
else
{
SetAngularVelocity( Vector3f(0) );
// Maybe, if you want high accuracy, call SetTransform here too
}

但是,实际上,我不明白为什么您不完全使用 Slerp。不必依靠物理积分器(可能不精确)并依靠知道何时到达目的地(这有点尴尬),因为知道运动就可以逐帧移动对象。
Quaternion startingPose;
Quaternion endingPose;
// As discussed earlier...
Quaternion totalAngle = Quaternion.AngleBetween( startingPose, endingPose );

// t is set to 0 whenever you start a new motion
t += deltaT;

float howFarIn = (turnRate * t) / totalAngle;
SetCurrentTransform( startingPose.Slerp( endingPose, howFarIn ) );

有关此方面的讨论,请参见 Smooth rotation with quaternions

关于c++ - 确定基于四元数调整方向所需的角速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29499074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com