简体   繁体   English

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

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

So, this seems to be a strange one (to me). 因此,这对我来说似乎是一个奇怪的人。 I'm relatively new to Unity, so I'm sure this is me misunderstanding something. 我是Unity的新手,所以我确定这是我误会的东西。

I'm working on a VSEPR tutorial module in unity. 我正在统一开发VSEPR教程模块。 VSEPR is the model by which electrons repel each other and form the shape (geometry) of atoms. VSEPR是电子相互排斥并形成原子形状(几何形状)的模型。

I'm simulating this by making (in this case) 4 rods (cylinder primitives) and using rigidbody.AddForce to apply an equal force against all of them. 我通过制作(在本例中)4个杆(圆柱图元)并使用“刚体.AddForce”对所有杆施加相等的力来进行模拟。 This works beautifully, as long as the force is equal. 只要作用力相等,效果就很好。 In the following image you'll see the rods are beautifully equi-distant at 109.47 degrees (actually you can "see" their attached objects, two lone pairs and two electron bonds...the rods are obscured in the atom shell.) 在下图中,您将看到棒以109.47度等距优美的距离(实际上,您可以“看到”它们的附着对象,两个孤对和两个电子键……这些棒在原子壳中被遮盖了。)

(BTW the atom's shell is just a sphere primitive - painted all pretty.) (顺便说一句,原子的壳只是一个球体图元-涂得很漂亮。)

HOWEVER, in the real world, the lone pairs actually exert SLIGHTLY more force...so when I add this additional force to the model...instead of just pushing the other electron rods a little farther away, it pushes the ENTIRE 4-bond structure outside the atom's shell. 但是,在现实世界中,孤对实际上施加的力稍大...因此,当我向模型添加此附加力时,...不仅将其他电子棒推得更远,反而推了整个4原子壳外的键结构。

THE REASON THIS SEEMS ODD IS...two things. 这个问题的原因是...两件事。

  1. All the rods are children of the shell...so I thought that made them somewhat immobile compared to the shell (Ie if they moved, the shell would move with them...which would be fine). 所有的杆都是外壳的子杆...所以我认为这使它们与外壳相比有些不动(也就是说,如果它们移动了,外壳就会随它们一起移动...这很好)。

  2. I have a ConfiguableJoint holding the rods to the center of the atoms shell ( 0,0,0). 我有一个ConfiguableJoint,将杆固定在原子壳(0,0,0)的中心。 The x/y/z-motion is set to fixed. x / y / z运动设置为固定。 I thought this should keep the rods fairly immutably attached to the 0,0,0 center of the shell...but I guess not. 我认为这应该使杆保持不变地连接到壳体的0,0,0中心...但是我想不是。

POINTS OF INTEREST: 兴趣点:

  • The rods only push on each other, by a script attached to one rod adding force to an adjacent rod. 杆仅通过附在一根杆上的脚本向彼此推挤,从而向相邻杆施加力。 they do not AddForce to the atom shell or anything else. 他们没有将AddForce添加到原子外壳或其他任何东西。

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

CODE: 码:

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

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

I think I found my own answer...unless someone has suggestions for a better way to handle this. 我想我找到了自己的答案...除非有人提出了更好的解决方案的建议。

I simply reduced the mass of the electron rods, and increased the mass of the sphere. 我只是减少了电子棒的质量,而增加了球体的质量。 I'm not sure if this is the "best practices" solution or a kluge...so input still welcomed :-) 我不确定这是否是“最佳做法”解决方案还是一个混搭...所以仍然欢迎输入:-)

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

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