Esempio n. 1
0
 private void OnCollision(_Collision collision)
 {
     if (rigidBody != null)
     {
         rigidBody.Collide(collision);
     }
 }
Esempio n. 2
0
 // --> Privados
 private void DetectCollisions(_Collider[] list)
 {
     foreach (_Collider other in list)
     {
         _Collision collision = DetectCollision(other);
         if (collision != null)
         {
             OnCollision(collision);
         }
     }
 }
Esempio n. 3
0
    public void Collide(_Collision collision)
    {
        Vector3 nUnit  = collision.NormalUnit;
        Vector3 tUnit1 = Vector3.Cross(nUnit, Vector3.up);
        Vector3 tUnit2 = Vector3.Cross(nUnit, tUnit1);

        float vn  = Vector3.Dot(nUnit, velocity);
        float vt1 = Vector3.Dot(tUnit1, velocity);
        float vt2 = Vector3.Dot(tUnit2, velocity);

        float v2n;
        float m2;

        if (collision.Rigidbody != null)
        {
            v2n = Vector3.Dot(nUnit, collision.Rigidbody.velocity);
            m2  = collision.Rigidbody.Mass;
        }
        else
        {
            v2n = -vn;
            m2  = mass;
        }

        float vFn = (vn * (mass - m2) + 2 * m2 * v2n) / (mass + m2);

        Vector3 variation;

        if (collision.Rigidbody != null)
        {
            variation = (transform.position - collision.CollisionPoint).normalized * (collision.CollisionRange + .01f) *
                        .5f;                         //vn / (vn+v2n);
        }
        else
        {
            variation = (transform.position - collision.CollisionPoint).normalized *
                        (collision.CollisionRange + .01f);
        }

        latePosition   = transform.position + variation;
        latePosition.y = 0;
        Vector3 lateVelocity = nUnit * vFn + tUnit1 * vt1 + tUnit2 * vt2;

        lateVelocity.y = 0;

        Vector3 velDif = (lateVelocity - velocity);
        Vector3 acc    = velDif / Time.fixedDeltaTime;
        Vector3 force  = acc * Mass;

        AddForce(force * cfInelasticidad, collision.CollisionPoint);

        lateUpdate = true;

        //Todo esto es para llamar las funciones de sonido (solo las bochas usan)
        if (gameObject.tag != "Bocha")
        {
            return;
        }

        if (collision.GameObject.tag == "Bocha")
        {
            SendMessage("BochaCollision", force.magnitude);
        }
        else if (collision.GameObject.tag == "Pared")
        {
            SendMessage("MesaCollision", force.magnitude);
        }
    }