[英]unity 3d: rigidbody.AddForce(forceDirection.normalized * force) moving objects away from parent and joint
因此,這對我來說似乎是一個奇怪的人。 我是Unity的新手,所以我確定這是我誤會的東西。
我正在統一開發VSEPR教程模塊。 VSEPR是電子相互排斥並形成原子形狀(幾何形狀)的模型。
我通過制作(在本例中)4個桿(圓柱圖元)並使用“剛體.AddForce”對所有桿施加相等的力來進行模擬。 只要作用力相等,效果就很好。 在下圖中,您將看到棒以109.47度等距優美的距離(實際上,您可以“看到”它們的附着對象,兩個孤對和兩個電子鍵……這些棒在原子殼中被遮蓋了。)
(順便說一句,原子的殼只是一個球體圖元-塗得很漂亮。)
但是,在現實世界中,孤對實際上施加的力稍大...因此,當我向模型添加此附加力時,...不僅將其他電子棒推得更遠,反而推了整個4原子殼外的鍵結構。
這個問題的原因是...兩件事。
所有的桿都是外殼的子桿...所以我認為這使它們與外殼相比有些不動(也就是說,如果它們移動了,外殼就會隨它們一起移動...這很好)。
我有一個ConfiguableJoint,將桿固定在原子殼(0,0,0)的中心。 x / y / z運動設置為固定。 我認為這應該使桿保持不變地連接到殼體的0,0,0中心...但是我想不是。
興趣點:
碼:
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.