嘿,感謝您在高級方面的幫助。
我已經觀看了一些關于如何在 Unity 中添加太陽系和軌道重力的 youtube 視頻,并最終使用這個來幫助太陽系重力部分。
https://www.youtube.com/watch?v=Ouu3D_VHx9o&t=114s&ab_channel=Brackeys
但就在我決定嘗試讓我的行星繞太陽運行之后,我使用這個維基頁面來計算數學方程
- https://en.wikipedia.org/wiki/Orbital_speed
但由于某種原因,要么我的行星飛離太陽,要么開始飛向地球。我已經環顧了 2 天,似乎無法使其作業并嘗試了不同型別的可能性。
這是我的代碼
public class Planets : MonoBehaviour
{
const float G = 100F;
public Rigidbody rb;
public float CurrentV;
private void FixedUpdate()
{
Planets[] attractors = FindObjectsOfType<Planets>();
foreach (Planets AllPlanets in attractors)
{
if (AllPlanets != this)
{
Orbiting(AllPlanets, CurrentV);
Attract(AllPlanets);
}
}
}
void Attract(Planets objToAttract)
{
Rigidbody RbTpAttract = objToAttract.rb;
Vector3 direction = rb.position - RbTpAttract.position;
float distance = direction.magnitude;
float ForceMagnitude = G * (rb.mass * RbTpAttract.mass) / Mathf.Pow(distance, 2);
Vector3 Force = direction.normalized * ForceMagnitude;
RbTpAttract.AddForce(Force);
}
void Orbiting(Planets objToAttract, float CV)
{
Rigidbody RbTpAttract = objToAttract.rb;
Vector3 direction = rb.position - RbTpAttract.position;
float distance = direction.magnitude;
float ForceMagnitude = Mathf.Sqrt((G * rb.mass) / (2 / distance - 1 / RbTpAttract.mass));
Vector3 Force = direction.normalized * ForceMagnitude;
RbTpAttract.velocity = Force;
}
}
uj5u.com熱心網友回復:
問題是軌道速度的公式用于推導軌道上物體的速度,但您將其用作施加于每個物體彼此相向的恒定推力的一種形式。這有點像計算移動汽車的速度,然后將相同的速度作為脈沖應用回去!
軌道上的物體所受的唯一力是你從牛頓定律中得到的力G * m * m / r*r。但是,為了實際運行,行星將需要一個初始速度——這可以從軌道速度公式中計算出來。在給定的距離計算它,并在 Start() 上以垂直于軌道平面的方向和朝向太陽的方向(或任何你想要的軌道)應用它,你可以從dir = Vector3.Cross(sunDir, Vector3.up).normalized
請注意,引力系統在依賴歐拉積分的物理引擎(例如 PhysX)中數值不穩定。為此,您需要諸如 Runge-Kutta 積分之類的東西,否則如果您讓模擬運行足夠長的時間,行星最終將失去其軌道。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416063.html
標籤:
