Unity physics - 如何让物体像 space 一样在重力作用下上下摆动?

Unity physics - how to make objects bob down then up as if in space with gravity?

好吧,刚接触 Unity 中的物理学,我也浏览过 Unity 的论坛,但是我不知道如何创建这种与比重相关的效果 -

我有这些质量为 100 的刚体,我可以将其推离平台(我的游戏是 space 中设置的一堆平台)进入开放 space。因为检查它们是否受重力影响,所以它们会掉落。

我需要的是让物体从平台上滑下来,缓慢下落,然后弹起并漂浮在那里。就像在 space 中被推开一样,但他们无法继续前进。只需向下,然后向上,停留并漂浮。

这似乎比我想象的要复杂得多,因为我玩过重力和高值它们直接下落(显然不是出来)而低值它们在推开时浮在上面。似乎没有一个既不下降也不上升的最佳位置,只是鲍勃。

我怎样才能做到这一点?我的质量是 100。

我的建议是实现一个试图类似于阿基米德原理的力函数。关键在于,潜力取决于物体的下沉程度:物体越往下沉,升力就越大。因此,这里的关键函数(也是最棘手的函数)是 volumeBeneathSurface.

double liftForce(object)
{
    const double waterLevel = 0.0;
    const double density = 1000.0; // kg/m^3 -- using water atm

    double vol = volumeBeneathSurface(object, waterLevel);

    double displacedMass = vol*density; // density = m/vol
    double displacedWeight = displacedMass*gravity; // F = m*a
    return displacedWeight;
}

现在,这里棘手的部分可能是计算水面以下的体积。想象一个可以旋转的非常棘手的几何体——它可以变得像你想要的那样复杂。最简单的情况可能是通过一个不旋转的盒子来近似你的形状。

double volumeBeneathSurface(object, double surfaceLevel)
{
     // assuming the object has `height`, `width`, and `depth`
     // also assuming its coordinate is references from the center of the object
     double r = object.y - surfaceLevel - object.height/2.0; // How much of the object is beneath the surface
     if (r > 0)
         return 0.0; // the object is purely above
     else if (r < object.height)
         return object.height*object.width*object.depth; // the whole object is beneath
     else
         return abs(r)*object.width*object.depth; // partly under
}

现实中的东西在水中上下摆动一点,但最终运动逐渐消失。这是由于能量转移到物体并在水中产生波浪。我们这里没有这样的效果,所以我们的对象很可能会永远上下摆动。你可以做的是给物体增加一些额外的摩擦力,这样运动就会淡出。