Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (isInit)
        {
            for (int j = sphereColliders.Count - 1; j >= 0; --j)
            {
                for (int k = j - 1; k >= 0; --k)
                {
                    NSphereCollider a = sphereColliders[j], b = sphereColliders[k];
                    if (!toDelete.Contains(a) && !toDelete.Contains(b))
                    {
                        if (a.CheckCollision(b))
                        {
                            if (a.particle.mass > b.particle.mass)
                            {
                                toDelete.Add(b);
                            }
                            else
                            {
                                toDelete.Add(a);
                            }
                            RespondToCollision(a, b);
                        }
                    }
                }
            }

            foreach (NSphereCollider col in toDelete)
            {
                Destroy(col.gameObject);
            }
            toDelete.Clear();
        }
    }
Exemplo n.º 2
0
    private void RespondToCollision(NSphereCollider a, NSphereCollider b)
    {
        float   aMass = a.particle.mass, bMass = b.particle.mass;
        Vector3 aVel = a.particle.velocity, bVel = b.particle.velocity;

        a.particle.velocity = (aMass * aVel + bMass * bVel) / (aMass + bMass);
    }
Exemplo n.º 3
0
    public bool CheckCollision(NSphereCollider other)
    {
        Vector3 difference = particle.position - other.particle.position;
        float   sumRadii   = radius + other.radius;
        float   distanceSq = Vector3.Dot(difference, difference);

        return(distanceSq <= sumRadii * sumRadii);
    }
Exemplo n.º 4
0
 public void RemoveItem(NSphereCollider col)
 {
     sphereColliders.Remove(col);
 }