c# - 给定位置 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
?),我看不到它,可能是这样吗?
我需要这个,所以我可以更精确地旋转我的船。
解决方案
主要的错误是混淆了加速度和速度。施加扭矩会导致角加速度(弧度/秒/秒),它由您的test
(36.1 deg / s^2
围绕 Y 轴)给出。这不是角速度,而是变化率,因此您不应期望相同的结果。
(而且传递给的力ForceToTorque
只是所需力的一半。)
快速物理笔记 - 扭矩方程:
I
是惯性矩张量,一个 3x3 矩阵,由上面的积分给出,在身体的所有质量元素上。它的索引i
和显然是对称的j
,因此它是可对角化的(任何体面的线性代数书):
D
是主体主轴基中的 M-of-I 张量,R
是从主基到当前基的旋转矩阵。的对角线元素D
是向量 的值body.inertiaTensor
,这意味着 Unity 始终将对象的主轴与世界轴对齐,并且我们始终拥有I = D
。
因此,要获得扭矩产生的角加速度:
最后一行由Div
.
确保精确旋转的更好方法是应用角脉冲,它直接改变角速度。Q
和所需的相应线性脉冲P
都满足:
这直接改变了身体的角速度。(*)
是输入参数必须满足的条件。您仍然可以使用AddForceAtPosition
with 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);
推荐阅读
- perl - 如何打印第一个不同值的标量值
- c# - 将 LINQ 返回值转换为二维数组
- mysql - ReactJS,如何在显示数据库中的文本区域时编辑文本区域并更新值?
- r - 重新编辑:塑造不平衡面板数据以运行混合多项式回归的问题
- matlab - 如何绘制在 matlab 中选择的可变步长 ODE 求解器?
- python - 逐行格式化熊猫数据框
- javascript - 类型错误:CSL.Attributes[attrname] 未定义
- ssl - PHPMailer 验证对等 SSL
- apache-nifi - NIFI:将 SQL 查询中的每月转储作为 CSV 文件提取到 SFTP 服务器中
- mongodb - 使用 socket.io/react.js/mongodb 为两个用户之间的每次聊天寻找最佳/干净方式的伪代码来创建和检查唯一的房间“名称”