使用扭矩稳定气垫船刚体直立 [英] Stabilize hovercraft rigidbody upright using torque

查看:23
本文介绍了使用扭矩稳定气垫船刚体直立的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个涉及悬停自行车的游戏.当自行车与某物发生碰撞时,它的角度自然会发生变化.我希望创建某种方式使其趋向于 0.这是我尝试过的:

I'm currently creating a game involving a hover-bike. when the bike collides with something , it's angles change naturally. I wish to create some sort of way for it to tend back to 0. Here's what I've tried:

if (hoverbike.rotation.x != 0 || hoverbike.rotation.z != 0)
        {
            hoverbike.AddTorque(x: Mathf.MoveTowardsAngle(hoverbike.rotation.x, 0, 0.01f), y: hoverbike.rotation.y, z: Mathf.MoveTowardsAngle(hoverbike.rotation.z, 0, 0.01f));
        }

        transform.Rotate(0.0f, -Input.GetAxis("Mouse X") * 0.5f, 0.0f);

很难解释它在做什么,因为我不明白它在做什么,它似乎正在旋转.如果有兴趣,这是我的其余代码:https://pastebin.com/kzMDQMVF,这是一团糟,但我仍在学习如何使用 Unity.

It's hard to explain what it's doing because I don't understand what it's doing, it just seems to spin out. Here's the rest of my code if interested: https://pastebin.com/kzMDQMVF, it's a mess but I'm still learning how to use Unity.

哦,角度 y 不应该趋于 0,因为那是水平角度.

Oh and angle y shouldn't tend to 0 because that's the horizontal angle.

推荐答案

您可以使用 Quaternion.FromToRotation:

You can determine the quaternion that would rotate the hovercraft from its current up to world up using Quaternion.FromToRotation:

Rigidbody hoverRB; // hovercraft's rigidbody

Quaternion deltaQuat = Quaternion.FromToRotation(hoverRB.transform.up, Vector3.up);

然后使用 Quaternion.ToAngleAxis将其转换为角度 &轴:

Then use Quaternion.ToAngleAxis to convert that to an angle & axis:

Vector3 axis;
float angle
deltaQuat.ToAngleAxis(out angle, out axis);

然后,取消一些现有的轮换,以便您最终达到目标:

Then, cancel out some of any existing rotation so that you'll eventually reach the goal:

float dampenFactor = 0.8f; // this value requires tuning
hoverRB.AddTorque(-hoverRB.angularVelocity * dampenFactor, ForceMode.Acceleration);

然后沿我们之前找到的轴施加一些扭矩,按剩余的角度进行缩放:

And then apply some torque along the axis we found before, scaled by how much angle remains:

float adjustFactor = 0.5f; // this value requires tuning
hoverRB.AddTorque(axis.normalized * angle * adjustFactor, ForceMode.Acceleration);

Rigidbody 使用的弧度和 ToAngleAxis 的度数之间的任何转换对于 float 常量都是多余的,所以不要太担心.

Any conversion between radians Rigidbody uses and degrees of ToAngleAxis is redundant with the float constants, so don't worry too much about it.

确保这一切都在 FixedUpdate(或在 FixedUpdate 时间调用/运行的函数)中完成,因为扭矩的方向可能需要从一个改变物理步入另一个阶段.所以,总的来说:

Make sure this is all being done in FixedUpdate (or a function called/running in FixedUpdate time) due to how the torque's direction will likely need to change from one physics step to another. So, altogether:

Rigidbody hoverRB; // hovercraft's rigidbody

...

void FixedUpdate() 
{    
    Quaternion deltaQuat = Quaternion.FromToRotation(hoverRB.transform.up, Vector3.up);

    Vector3 axis;
    float angle
    deltaQuat.ToAngleAxis(out angle, out axis);

    float dampenFactor = 0.8f; // this value requires tuning
    hoverRB.AddTorque(-hoverRB.angularVelocity * dampenFactor, ForceMode.Acceleration);

    float adjustFactor = 0.5f; // this value requires tuning
    hoverRB.AddTorque(axis.normalized * angle * adjustFactor, ForceMode.Acceleration);
}

这篇关于使用扭矩稳定气垫船刚体直立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆