Beispiel #1
0
    // O(n²)
    private void FixedUpdate()
    {
        int positionInList = SimulatedObjects.IndexOf(this);

        foreach (Gravitation other in SimulatedObjects.Skip(positionInList))
        {
            float   distance  = Vector3.Distance(this.transform.position, other.transform.position);
            float   force     = GravitationConstant * this.Mass * other.Mass / Mathf.Clamp(Mathf.Pow(distance, 2), 0.1f, float.PositiveInfinity);
            Vector3 direction = (other.transform.position - this.transform.position).normalized;
            if (!this.isStatic)
            {
                rigidbody.AddForce(direction * force);
            }
            if (!other.isStatic)
            {
                other.rigidbody.AddForce(-direction * force);
            }
        }
    }
Beispiel #2
0
 private void OnDisable()
 {
     SimulatedObjects.Remove(this);
 }
Beispiel #3
0
 private void OnEnable()
 {
     SimulatedObjects.Add(this);
 }