Beispiel #1
0
    public static bool OverlapSphereBox(MySphereCollider sph, MyBoxCollider box)
    {
        Vector3 closestPointInAABB = Vector3.Min(Vector3.Max(sph.transform.position, box.Min), box.Max);
        float   distance           = Vector3.Distance(closestPointInAABB, sph.transform.position);

        return(distance < sph.Radius);
    }
    public override bool isColliding(MyCollider _other)
    {
        if (_other == null)
        {
            return(false);
        }
        if (_other is MySphereCollider)
        {
            MySphereCollider other    = (MySphereCollider)_other;
            float            distance = (this.Position - other.Position).sqrMagnitude;
            float            radii    = this.m_radius * transform.lossyScale.x + other.m_radius * other.transform.lossyScale.x;

            return(distance < (radii * radii));
        }
        else if (_other is MyBoxCollider)
        {
            MyBoxCollider other = (MyBoxCollider)_other;
            other.CalculateCubeCorners();
            Vector3 potentialAxis = this.Position - other.Position;
            potentialAxis = potentialAxis.normalized;

            float min1, max1, min2, max2;

            min1 = float.PositiveInfinity;
            max1 = float.NegativeInfinity;
            float tmp;
            for (int i = 0; i < other.CubeCorners.Length; i++)
            {
                tmp = Vector3.Dot(potentialAxis, other.cubeCorners[i]);
                if (tmp < min1)
                {
                    tmp = min1;
                }
                if (tmp > max1)
                {
                    max1 = tmp;
                }
            }

            min2 = float.PositiveInfinity;
            max2 = float.NegativeInfinity;

            tmp  = Vector3.Dot(potentialAxis, other.Position);
            min2 = tmp - this.ActualRadius;
            max2 = tmp + this.ActualRadius;

            if (min1 > min2 && min1 < max2)
            {
                return(true);
            }
            if (max1 > min2 && max1 < max2)
            {
                return(true);
            }
            return(false);
        }
        return(base.isColliding(_other));
    }
Beispiel #3
0
 void Update()
 {
     Collider[] hitColliders = Physics.OverlapSphere(transform.position, MyMath.squareRoot3_float);
     UpdateObb();
     //MyBoxCollider[] hitColliders = FindObjectsOfType<MyBoxCollider>();
     for (int i = 0; i < hitColliders.Length; i++)
     {
         if (hitColliders[i] != unityCollider)
         {
             MyBoxCollider collider = hitColliders[i].GetComponent <MyBoxCollider>();
             if (Intersects(this, collider))
             {
                 Debug.Log("碰撞");
             }
         }
     }
 }
 private MyCollision CollidedWithBox(MyRigidBody body, MyBoxCollider plane)
 {
     return null;
 }