首页 > 解决方案 > 给定位置 C# Unity 的力,计算 GameObject 的旋转速度

问题描述

我发现这个脚本返回旋转速度(考虑到力的施加位置和与质心的距离)。

public Vector3 ForceToTorque(Vector3 force, Vector3 position, ForceMode forceMode = ForceMode.Force)
{
    Vector3 t = Vector3.Cross(position - body.worldCenterOfMass, force);
    ToDeltaTorque(ref t, forceMode);

    return t;
}

private void ToDeltaTorque(ref Vector3 torque, ForceMode forceMode)
{
    bool continuous = forceMode == ForceMode.VelocityChange || forceMode == ForceMode.Acceleration;
    bool useMass = forceMode == ForceMode.Force || forceMode == ForceMode.Impulse;

    if (continuous) torque *= Time.fixedDeltaTime;
    if (useMass) ApplyInertiaTensor(ref torque);
}

private void ApplyInertiaTensor(ref Vector3 v)
{
    v = body.rotation * Div(Quaternion.Inverse(body.rotation) * v, body.inertiaTensor);
}

private static Vector3 Div(Vector3 v, Vector3 v2)
{
    return new Vector3(v.x / v2.x, v.y / v2.y, v.z / v2.z);
}

使用 2100 牛顿,我得到 0.6 弧度(36 度)的旋转。

var test = rotationScript.ForceToTorque(shipFront.right * 2100, shipFront.position, ForceMode.Force);
Debug.Log(test + " " + test * Mathf.Rad2Deg);
// Above gives (0, 0.6, 0) and (0, 36.1, 0)

但是使用AddForceAtPosition以相同的力旋转船我没有得到相同的结果

if (currTurn > 0) {
    body.AddForceAtPosition(shipFront.right * 2100, shipFront.position, ForceMode.Force);
    body.AddForceAtPosition(-shipBack.right * 2100, shipBack.position, ForceMode.Force);
} else if (currTurn < 0) {
    body.AddForceAtPosition(-shipFront.right * 2100, shipFront.position, ForceMode.Force);
    body.AddForceAtPosition(shipBack.right * 2100, shipBack.position, ForceMode.Force);
}

它不是每秒给我 36 度 - 我通过计算完成一个完整的 360 度旋转需要多长时间来进行测试,据说它应该在 10 秒内完成,但它只需要 10 秒旋转约 90 度。

在第一个脚本中有很多我不明白的地方,就像大多数物理部分一样,但考虑到我的飞船质量(body.worldCenterOfMass?),我看不到它,可能是这样吗?

我需要这个,所以我可以更精确地旋转我的船。

标签: c#unity3dvectorphysicsgame-physics

解决方案


主要的错误是混淆了加速度速度。施加扭矩会导致角加速度(弧度/秒/秒),它由您的test36.1 deg / s^2围绕 Y 轴)给出。这不是角速度,而是变化率,因此您不应期望相同的结果。

(而且传递给的力ForceToTorque只是所需力的一半。)


快速物理笔记 - 扭矩方程:

在此处输入图像描述

I惯性矩张量,一个 3x3 矩阵,由上面的积分给出,在身体的所有质量元素上。它的索引i和显然是对称的j,因此它是可对角化的(任何体面的线性代数书):

在此处输入图像描述

D主体主轴基中的 M-of-I 张量,R是从主基到当前基的旋转矩阵。的对角线元素D是向量 的值body.inertiaTensor,这意味着 Unity 始终将对象的主轴与世界轴对齐,并且我们始终拥有I = D

因此,要获得扭矩产生的角加速度:

在此处输入图像描述

最后一行由Div.


确保精确旋转的更好方法是应用角脉冲,它直接改变角速度。Q和所需的相应线性脉冲P都满足:

在此处输入图像描述

这直接改变了身体的角速度。(*)是输入参数必须满足的条件。您仍然可以使用AddForceAtPositionwith ForceMode.Impulse。代码:

Vector3 AngularvelocityToImpulse(Vector3 vel, Vector3 position)
{
   Vector3 R = position - body.worldCenterOfMass;
   Vector3 Q = MultiplyByInertiaTensor(vel);

   // condition (*)
   if (Math.Abs(Vector3.Dot(Q, R)) > 1e-5) 
      return new Vector3();

   // one solution
   // multiply by 0.5 because you need to apply this to both sides
   // fixes the factor-of-2 issue from before
   return 0.5 * Vector3.Cross(Q, R) / R.sqrMagnitude;
} 

Vector3 MultiplyByInertiaTensor(Vector3 v)
{
   return body.rotation * Mul(Quaternion.Inverse(body.rotation) * v, body.inertiaTensor);
}

Vector3 Mul(Vector3 v, Vector3 a)
{
   return new Vector3(v.x * a.x, v.y * a.y, v.z * a.z);
}

申请:

var test = AngularvelocityToImpulse(...);
body.AddForceAtPosition(test, shipFront.position, ForceMode.Impulse);
body.AddForceAtPosition(-test, shipBack.position, ForceMode.Impulse);

推荐阅读