Пример #1
0
 private static void Reflect(BulletBehaviourAABB s)
 {
     if ((s.collisionNormal == Vector3.forward) || (s.collisionNormal == Vector3.back))
     {
         s.direction = new Vector3(s.direction.x, s.direction.y, -s.direction.z);
     }
     else if ((s.collisionNormal == Vector3.right) || (s.collisionNormal == Vector3.left))
     {
         s.direction = new Vector3(-s.direction.x, s.direction.y, s.direction.z);
     }
     else if ((s.collisionNormal == Vector3.up) || (s.collisionNormal == Vector3.down))
     {
         s.direction = new Vector3(s.direction.x, -s.direction.y, s.direction.z);
     }
 }
Пример #2
0
    public static void CheckAABBs(CubeBehaviour a, BulletBehaviourAABB c)
    {
        CubeBehaviour b        = c.cube;
        Contact       contactB = new Contact(b);

        if ((a.min.x <= b.max.x && a.max.x >= b.min.x) &&
            (a.min.y <= b.max.y && a.max.y >= b.min.y) &&
            (a.min.z <= b.max.z && a.max.z >= b.min.z))
        {
            // determine the distances between the contact extents
            float[] distances =
            {
                (b.max.x - a.min.x),
                (a.max.x - b.min.x),
                (b.max.y - a.min.y),
                (a.max.y - b.min.y),
                (b.max.z - a.min.z),
                (a.max.z - b.min.z)
            };

            float   penetration = float.MaxValue;
            Vector3 face        = Vector3.zero;

            // check each face to see if it is the one that connected
            for (int i = 0; i < 6; i++)
            {
                if (distances[i] < penetration)
                {
                    // determine the penetration distance
                    penetration = distances[i];
                    face        = faces[i];
                }
            }

            // set the contact properties
            contactB.face        = face;
            c.collisionNormal    = face;
            contactB.penetration = penetration;

            Reflect(c);

            // check if contact does not exist
            if (!a.contacts.Contains(contactB))
            {
                // remove any contact that matches the name but not other parameters
                for (int i = a.contacts.Count - 1; i > -1; i--)
                {
                    if (a.contacts[i].cube.name.Equals(contactB.cube.name))
                    {
                        a.contacts.RemoveAt(i);
                    }
                }

                // add the new contact
                a.contacts.Add(contactB);
                a.isColliding = true;
            }
        }
        else
        {
            if (a.contacts.Exists(x => x.cube.gameObject.name == b.gameObject.name))
            {
                a.contacts.Remove(a.contacts.Find(x => x.cube.gameObject.name.Equals(b.gameObject.name)));
                a.isColliding = false;

                if (a.gameObject.GetComponent <RigidBody3D>().bodyType == BodyType.DYNAMIC)
                {
                    a.gameObject.GetComponent <RigidBody3D>().isFalling = true;
                    a.isGrounded = false;
                }
            }
        }
    }