我目前正在为卫星游戏构建简化的 react 控制系统,并且需要一种使用该系统将卫星与世界空间坐标中的给定单位方向对齐的方法。因为这是游戏模拟,所以我在伪造系统,只是在对象震中周围施加扭矩。
这很困难,因为在我的情况下,扭矩无法改变强度,无论是打开还是关闭。是全力还是全力。计算需要施加扭矩的方向相对容易,但是我很难使其完全对准而又不会失去控制并陷入逻辑回路。它需要在恰好正确的“时间”施加相反的力,以零角速度降落在目标方向上。
到目前为止,我已经确定的是,需要根据当前的角速度和两个 vector 之间的角度来计算达到零速度所需的“时间”。如果超过我达到零角的时间,则需要施加相反的扭矩。从理论上讲,这也可以防止其围绕轴“弹跳”得太多。我几乎可以使它正常工作,但是在某些情况下,在一个方向上施加力似乎卡住了,所以我希望有人可以检查一下逻辑。我的模拟目前未考虑质量,因此您可以忽略惯量张量(除非它使计算更容易!)
对于一个轴,我目前正以这种方式进行操作,但是我认为有人会拥有一种更为优雅的解决方案,该解决方案实际上可以一次计算偏航和俯仰轴(滚动无效)。
Omega = Angular Velocity in Local-Space (Degrees Per Second)
Force = Strength of the Thrusters
// Calculate Time Variables
float Angle = AcosD(DotProduct(ForwardVector, DirectionVector));
float Time1 = Abs(Angle / Omega.Z); // Time taken to reach angle 0 at current velocity
float Time2 = Abs(DeltaTime * (Omega.Z / Force); // Time it will take to reach Zero velocity based on force strength.
// Calculate Direction we need to apply the force to rotate toward the target direction. Note that if we are at perfect opposites, this will be zero!
float AngleSign = Sign(DotProduct(RightVector, DirectionVector));
float Torque.Z = 0;
if (Time1 < Time2)
{
Torque.Z = AngleSign * Force;
}
else
{
Torque.Z = AngleSign * Force * -1.0f
}
// Torque is applied to object as a change in acceleration (no mass) and modified by DeltaSeconds for frame-rate independent force.
这远非优雅,肯定存在一些标志问题。你们是否知道实现此目标的更好方法?
编辑:
如果有人了解虚幻引擎的蓝图系统,这就是我目前在将其移至C++之前对其进行原型(prototype)制作的方式。
最佳答案
从“计算方向”行开始,您可以直接在3D中计算校正扭矩 vector ,然后如果知道先前的校正将要过冲,则可以修改其符号:
// Calculate Direction we need to apply the force to rotate toward the target direction
Torque = CrossProduct(DirectionVector, ForwardVector)
Torque = Normalize(Torque) * Force
if (Time2 < Time1)
{
Torque = -Torque
}
但是您应该处理有问题的情况:
// Calculate Direction we need to apply the force to rotate toward the target direction
Torque = CrossProduct(DirectionVector, ForwardVector)
if (Angle < 0.1 degrees)
{
// Avoid divide by zero in Normalize
Torque = {0, 0, 0}
}
else
{
// Handle case of exactly opposite direction (where CrossProduct is zero)
if (Angle > 179.9 degrees)
{
Torque = {0, 0, 1}
}
Torque = Normalize(Torque) * Force
if (Time2 < Time1)
{
Torque = -Torque
}
}
关于c++ - 计算以恒定加速度对齐两个3D vector 所需的扭矩?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33648662/