繁体   English   中英

unity 3d:刚体.AddForce(forceDirection.normalized *力)将对象移离父级和关节

[英]unity 3d: rigidbody.AddForce(forceDirection.normalized * force) moving objects away from parent and joint

因此,这对我来说似乎是一个奇怪的人。 我是Unity的新手,所以我确定这是我误会的东西。

我正在统一开发VSEPR教程模块。 VSEPR是电子相互排斥并形成原子形状(几何形状)的模型。

我通过制作(在本例中)4个杆(圆柱图元)并使用“刚体.AddForce”对所有杆施加相等的力来进行模拟。 只要作用力相等,效果就很好。 在下图中,您将看到棒以109.47度等距优美的距离(实际上,您可以“看到”它们的附着对象,两个孤对和两个电子键……这些棒在原子壳中被遮盖了。)

(顺便说一句,原子的壳只是一个球体图元-涂得很漂亮。)

但是,在现实世界中,孤对实际上施加的力稍大...因此,当我向模型添加此附加力时,...不仅将其他电子棒推得更远,反而推了整个4原子壳外的键结构。

这个问题的原因是...两件事。

  1. 所有的杆都是外壳的子杆...所以我认为这使它们与外壳相比有些不动(也就是说,如果它们移动了,外壳就会随它们一起移动...这很好)。

  2. 我有一个ConfiguableJoint,将杆固定在原子壳(0,0,0)的中心。 x / y / z运动设置为固定。 我认为这应该使杆保持不变地连接到壳体的0,0,0中心...但是我想不是。

兴趣点:

  • 杆仅通过附在一根杆上的脚本向彼此推挤,从而向相邻杆施加力。 他们没有将AddForce添加到原子外壳或其他任何东西。

在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

码:

void RepulseLike() {

    if (control.disableRepulsionForce) { return; }  // No force when dragging

    //////////////////// DETERMINE IF FORCE APPLIED //////////////////////////////
    // Scroll through each Collider that this.BondStick bumps
    foreach (Collider found in Physics.OverlapSphere(transform.position, (1f))) {

        // Don't repel self
        if (found == this.collider) { continue; }

        // Check for charged particle
        if (found.gameObject.tag.IndexOf("Charge") < 0) { continue; }// No match "charge", not a charged particle   


        /////////////// APPLY FORCE ///////////////
        // F = k(q1*q2/r^2)
        // where 
        // k = Culombs constant which in this instance is represented by repulseChargeFactor
        // r = distance
        // q1 and q2 are the signed magnitudes of the charges, in this case -1 for electrons and +1 for protons

        // Swap from local to global variable for other methods
        other = found;

        /////////////////////////////////
        // Calculate pushPoints for SingleBonds
        forceDirection = (other.transform.position - transform.position) * magnetism; //magnetism = 1

        // F = k(q1*q2/distance^2), q1*q2 ia always one in this scenario. k is arbitrary in this scenario
        force = control.repulseChargeFactor * (1 / (Mathf.Pow(distance, 2)));

        found.rigidbody.AddForce(forceDirection.normalized * force);// * Time.fixedDeltaTime);

    }//Foreach Collider

}//RepulseLike Method

您可能希望使用球体来表示电子,因此要在其上施加力并根据该球体重新定向(旋转)棒。

我想我找到了自己的答案...除非有人提出了更好的解决方案的建议。

我只是减少了电子棒的质量,而增加了球体的质量。 我不确定这是否是“最佳做法”解决方案还是一个混搭...所以仍然欢迎输入:-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM