示例#1
0
 private static void Reflect(BulletBehaviour2 bullet)
 {
     if ((bullet.collisionNormal == Vector3.forward) || (bullet.collisionNormal == Vector3.back))
     {
         bullet.direction = new Vector3(bullet.direction.x, bullet.direction.y, -bullet.direction.z);
     }
     else if ((bullet.collisionNormal == Vector3.right) || (bullet.collisionNormal == Vector3.left))
     {
         bullet.direction = new Vector3(-bullet.direction.x, bullet.direction.y, bullet.direction.z);
     }
     else if ((bullet.collisionNormal == Vector3.up) || (bullet.collisionNormal == Vector3.down))
     {
         bullet.direction = new Vector3(bullet.direction.x, -bullet.direction.y, bullet.direction.z);
     }
 }
示例#2
0
    public static void CheckSphereVsAABBs(BulletBehaviour2 bullet, CubeBehaviour cube)
    {
        var x = Mathf.Max(cube.min.x, Mathf.Min(bullet.mCenter.x, cube.max.x));
        var y = Mathf.Max(cube.min.y, Mathf.Min(bullet.mCenter.y, cube.max.y));
        var z = Mathf.Max(cube.min.z, Mathf.Min(bullet.mCenter.z, cube.max.z));

        var distance = Mathf.Sqrt((x - bullet.mCenter.x) * (x - bullet.mCenter.x) +
                                  (y - bullet.mCenter.y) * (y - bullet.mCenter.y) +
                                  (z - bullet.mCenter.z) * (z - bullet.mCenter.z));

        if ((distance < bullet.radius) && (!bullet.isColliding))
        {
            float[] distances =
            {
                (cube.max.x - bullet.transform.position.x),
                (bullet.transform.position.x - cube.min.x),
                (cube.max.y - bullet.transform.position.y),
                (bullet.transform.position.y - cube.min.y),
                (cube.max.z - bullet.transform.position.z),
                (bullet.transform.position.z - cube.min.z)
            };

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

            for (int i = 0; i < 6; i++)
            {
                if (distances[i] < penetration)
                {
                    // determine the penetration distance
                    penetration = distances[i];
                    side        = sides[i];
                }
            }

            bullet.penetration     = penetration;
            bullet.collisionNormal = side;

            Reflect(bullet);
            cube.stop = false;
        }
    }