简体   繁体   English

pl.net 的轨迹(线性渲染器)不反映实际运动 Unity 3d

[英]Trajectory(linerenderer) of a planet doesn't reflect the actual motion Unity 3d

I'm trying to recreate a solar system and i'm using newton's law.我正在尝试重建一个太阳系,并且我正在使用牛顿定律。 The actual force works great but when i try to "predict" the path of a pl.net it just doesn't work: to predict it i use a lineRenderer whose points get placed on the position at the Tth instant ( T being time ).实际的力效果很好,但是当我尝试“预测”pl.net 的路径时,它就是行不通:为了预测它,我使用了一个 lineRenderer,它的点在第 T 时刻被放置在 position 上(T 是时间) . When i start the game they are close but as time passes the pl.net actually goes in "orbit" while the line shoots up.当我开始游戏时,它们很接近,但随着时间的推移,pl.net 实际上进入了“轨道”,而线路则上升了。 I can't understand why this happens seen that i calculate the pl.net's position in the same way as the line's one.我无法理解为什么会发生这种情况,因为我以与线路相同的方式计算 pl.net 的 position。 I also tried instantiating a ball but same result.我也尝试实例化一个球,但结果相同。

public void UpdateSpeed(Vector3 acceleration,float time)
{
    velocity += acceleration * time;
}

public void UpdatePosition(float time)
{
    transform.position +=velocity *time;
}

public void UpdateLine(float time)
{
    position += velocity * time;
    Debug.Log(position);
    Instantiate(ball, position, Quaternion.identity);
    line.positionCount++;
    line.SetPosition(line.positionCount-1, position);
}

and here the function that computes acceleration这里是计算加速度的 function

 public Vector3 CalculateAcceleration(GameObject subj, float time)
{
    Vector3 Acceleration = Vector3.zero;
    foreach (var pl in planets)
    {
        if (pl != subj)
        {
            float sqrDistance= Mathf.Pow(Vector3.Distance(subj.transform.position, pl.transform.position), 2);
            Vector3 direction = (pl.transform.position - subj.transform.position).normalized;
            Acceleration += direction * (pl.GetComponent<Rigidbody>().mass * GravitationalConstant)/sqrDistance;
        }
    }
    return Acceleration;
}

is the rigidbody attached to the pl.net making a difference for the position?连接到 pl.net 的刚体对 position 有影响吗?

If you are having trouble with the orbit, calculate the OrbitFirst then call it to move your object with different time, just modify delta time below如果您在轨道上遇到问题,请计算 OrbitFirst 然后调用它以不同的时间移动您的 object,只需修改下面的增量时间

LineRenderer lr; 

//Build the orbit path/line
void CalculateOrbitEllipse()
{
    Vector3[] points = new Vector3[segments + 1];
    for (int i = 0; i < segments; i++)
    {
        Vector2 position2D = orbitPath.Evaluate((float)i / (float)segments);
        points[i] = new Vector3(position2D.x, 0f, position2D.y);
    }
    points[segments] = points[0];

    lr.positionCount = segments + 1;
    lr.SetPositions(points);
}


//make the planet move, 
//you can invoke this using your time variable, Time.deltaTime see below
IEnumerator AnimationOrbit()
{
    if (orbitPeriod < 0.1f)
    {
        orbitPeriod = 0.1f;
    }
    float orbitSpeed = 0.1f / orbitPeriod;
    while (orbitActive)
    {
        orbitProgress += Time.deltaTime * orbitSpeed;
        orbitProgress %= 1f;
        SetOrbitingObjectPosition();
        lr = GetComponent<LineRenderer>();
        CalculateOrbitEllipse();
        yield return null;
    }
 }


 void SetOrbitingObjectPosition()
 {
    Vector2 orbitPos = orbitPath.Evaluate(orbitProgress);
    orbitingObject.localPosition = new Vector3(orbitPos.x, 0, orbitPos.y);        
 }

so as often happens i'm an idiot.所以经常发生我是个白痴。 I was calculating in the acceleration function distances with the actual bodies positions instead of the simulated position so i always had a false value that was increasing instead of varying我正在计算加速度 function 与实际身体位置的距离而不是模拟的 position 所以我总是有一个错误的值正在增加而不是变化

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

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