Example #1
0
    void SetColliderEdit(Collider collider)
    {
        if ((EditCollider == null && collider != null) || (EditCollider != null && EditCollider != collider))
        {
            EditCollider = collider;
            ActiveEditorTracker editorTracker = ActiveEditorTracker.sharedTracker;
            Editor[]            editors       = editorTracker.activeEditors;
            for (int i = 1; i < editors.Length; i++)
            {
                Editor et = editors[i];
                if (EditCollider == (et.target as Collider))
                {
                    if (et is CustomBoxColliderEditor)
                    {
                        CustomBoxColliderEditor ced = et as CustomBoxColliderEditor;
                        ced.SetEditMode(true);
                    }
                    else if (et is CustomSphereCollider)
                    {
                        CustomSphereCollider ced = et as CustomSphereCollider;
                        ced.SetEditMode(true);
                    }
                    else if (et is CustomCapsuleColliderEditor)
                    {
                        CustomCapsuleColliderEditor ced = et as CustomCapsuleColliderEditor;
                        ced.SetEditMode(true);
                    }
                    break;
                }
            }
            return;
        }

        if ((EditCollider != null && EditCollider == collider) || (EditCollider != null && collider == null))
        {
            ActiveEditorTracker editorTracker = ActiveEditorTracker.sharedTracker;
            Editor[]            editors       = editorTracker.activeEditors;
            for (int i = 1; i < editors.Length; i++)
            {
                Editor et = editors[i];
                if (EditCollider == (et.target as Collider))
                {
                    if (et is CustomBoxColliderEditor)
                    {
                        CustomBoxColliderEditor ced = et as CustomBoxColliderEditor;
                        ced.SetEditMode(false);
                    }
                    else if (et is CustomSphereCollider)
                    {
                        CustomSphereCollider ced = et as CustomSphereCollider;
                        ced.SetEditMode(true);
                    }
                    else if (et is CustomCapsuleColliderEditor)
                    {
                        CustomCapsuleColliderEditor ced = et as CustomCapsuleColliderEditor;
                        ced.SetEditMode(true);
                    }
                    break;
                }
            }
            EditCollider = null;
        }
    }
    public bool BoxVsSphereCollision(CustomBoxCollider box, CustomSphereCollider sphere, out CustomCollisionInfo info)
    {
        info = new CustomCollisionInfo();
        var spherePosition = sphere.transform.position + sphere.colliderOffset;
        var boxPosition    = box.transform.position + box.colliderOffset;


        //List<Vector3> box1Points = box.GetOrientedBoxBounds().GetPoints();
        //List<Vector3> box2Points = new List<Vector3>();
        //box2Points.Add(spherePosition + sphere.U() * sphere.radius);
        //box2Points.Add(spherePosition - sphere.U() * sphere.radius);
        //box2Points.Add(spherePosition + sphere.V() * sphere.radius);
        //box2Points.Add(spherePosition - sphere.V() * sphere.radius);
        //box2Points.Add(spherePosition + sphere.W() * sphere.radius);
        //box2Points.Add(spherePosition - sphere.W() * sphere.radius);


        //var P1 = GetMinMaxObj(box1Points, box.V());
        //var P2 = GetMinMaxObj(box2Points, box.V());
        //var Q1 = GetMinMaxObj(box1Points, box.U());
        //var Q2 = GetMinMaxObj(box2Points, box.U());
        //var R1 = GetMinMaxObj(box1Points, box.W());
        //var R2 = GetMinMaxObj(box2Points, box.W());

        //var S1 = GetMinMaxObj(box1Points, sphere.V());
        //var S2 = GetMinMaxObj(box2Points, sphere.V());
        //var T1 = GetMinMaxObj(box1Points, sphere.U());
        //var T2 = GetMinMaxObj(box2Points, sphere.U());
        //var U1 = GetMinMaxObj(box1Points, sphere.W());
        //var U2 = GetMinMaxObj(box2Points, sphere.W());

        //bool P = P1.max < P2.min || P2.max < P1.min;
        //bool Q = Q1.max < Q2.min || Q2.max < Q1.min;
        //bool R = R1.max < R2.min || R2.max < R1.min;
        //bool S = S1.max < S2.min || S2.max < S1.min;
        //bool T = T1.max < T2.min || T2.max < T1.min;
        //bool U = U1.max < U2.min || U2.max < U1.min;

        ////Check if seperated
        //bool res = P || Q || R || S || T || U;

        //if (!res)
        //{
        //    info.intersectionSize.x =
        //        Mathf.Max(Mathf.Min((spherePosition + (sphere.U() * sphere.radius + sphere.V() * sphere.radius + sphere.W() * sphere.radius)).x, box.GetOrientedBoxBounds().max.x), 0);
        //    info.intersectionSize.y =
        //        Mathf.Max(Mathf.Min((spherePosition + (sphere.U() * sphere.radius + sphere.V() * sphere.radius + sphere.W() * sphere.radius)).y, box.GetOrientedBoxBounds().max.y), 0);
        //    info.intersectionSize.z =
        //        Mathf.Max(Mathf.Min((spherePosition + (sphere.U() * sphere.radius + sphere.V() * sphere.radius + sphere.W() * sphere.radius)).z, box.GetOrientedBoxBounds().max.z), 0);
        //    DrawBoundingVolume(box, Color.red, true);
        //    DrawBoundingVolume(sphere, Color.red, true, CustomCollider.ColliderType.Sphere);
        //}


        //return res;

        var normal = (boxPosition - spherePosition).normalized;
        var rad    = Mathf.Abs(Vector3.Dot(box.U(), normal) + Vector3.Dot(box.V(), normal) + Vector3.Dot(box.W(), normal));
        var dot    = Vector3.Dot(boxPosition, normal) - Vector3.Dot(spherePosition, normal);

        if (dot < rad)
        {
            var xMax1 = box.GetOrientedBoxBounds().max.x;
            var yMax1 = box.GetOrientedBoxBounds().max.y;
            var zMax1 = box.GetOrientedBoxBounds().max.z;

            var xMax2 = spherePosition.x + sphere.radius;
            var yMax2 = spherePosition.y + sphere.radius;
            var zMax2 = spherePosition.z + sphere.radius;

            info.intersectionSize.x = Mathf.Max(Mathf.Min(xMax2, xMax1), 0);
            info.intersectionSize.y = Mathf.Max(Mathf.Min(yMax2, yMax1), 0);
            info.intersectionSize.z = Mathf.Max(Mathf.Min(zMax2, zMax1), 0);
            DrawBoundingVolume(box, Color.red, true);
            DrawBoundingVolume(sphere, Color.red, true, CustomCollider.ColliderType.Sphere);
            return(true);
        }
        return(false);


        //Vector3 v;
        //var centerBox = box.transform.position + box.colliderOffset;
        //var currentCorner = box.GetOrientedBoxBounds().GetPoints()[0];
        //var max = Vector3.Distance(centerBox, spherePosition);
        //var box2sphere = boxPosition - spherePosition;
        //var box2SphereNormalised = box2sphere.normalized;
        //var points = box.GetOrientedBoxBounds().GetPoints();
        //var closestPoint = centerBox;
        //for (int i = 1; i < points.Count; ++i)
        //{
        //    currentCorner = points[i];
        //    if (Vector3.Distance(currentCorner, spherePosition) < max)
        //        closestPoint = currentCorner;
        //}

        //if ((sphere.radius - (spherePosition - closestPoint).magnitude) <= 0)
        //    return false;
        //else
        //{
        //    var xMax1 = box.GetOrientedBoxBounds().max.x;
        //    var yMax1 = box.GetOrientedBoxBounds().max.y;
        //    var zMax1 = box.GetOrientedBoxBounds().max.z;

        //    var xMax2 = sphere.radius - (spherePosition.x - closestPoint.x);
        //    var yMax2 = sphere.radius - (spherePosition.y - closestPoint.y);
        //    var zMax2 = sphere.radius - (spherePosition.z - closestPoint.z);

        //   // info.intersectionSize.x = sphere.radius - (spherePosition.x - closestPoint.x);
        //   // info.intersectionSize.y = sphere.radius - (spherePosition.y - closestPoint.y);
        //   // info.intersectionSize.z = sphere.radius - (spherePosition.z - closestPoint.z);

        //    info.intersectionSize.x = Mathf.Max(Mathf.Min(xMax2, xMax1), 0) * 2;
        //    info.intersectionSize.y = Mathf.Max(Mathf.Min(yMax2, yMax1), 0) * 2;
        //    info.intersectionSize.z = Mathf.Max(Mathf.Min(zMax2, zMax1), 0) * 2;

        //    return true;
        //}



        //if (box2sphere.magnitude - max - sphere.radius > 0 && box2sphere.magnitude > 0)
        //    return false;

        //DrawBoundingVolume(box, Color.red, true);
        //DrawBoundingVolume(sphere, Color.red, true, CustomCollider.ColliderType.Sphere);

        //var xMin1 = box.GetOrientedBoxBounds().min.x;
        //var xMax1 = box.GetOrientedBoxBounds().max.x;
        //var yMin1 = box.GetOrientedBoxBounds().min.y;
        //var yMax1 = box.GetOrientedBoxBounds().max.y;
        //var zMin1 = box.GetOrientedBoxBounds().min.z;
        //var zMax1 = box.GetOrientedBoxBounds().max.z;

        //var xMin2 = spherePosition.x - sphere.radius;
        //var xMax2 = spherePosition.x + sphere.radius;
        //var yMin2 = spherePosition.y - sphere.radius;
        //var yMax2 = spherePosition.y + sphere.radius;
        //var zMin2 = spherePosition.z - sphere.radius;
        //var zMax2 = spherePosition.z + sphere.radius;

        //info.intersectionSize.x = Mathf.Max(Mathf.Min(xMax2, xMax1), 0);
        //info.intersectionSize.y = Mathf.Max(Mathf.Min(yMax2, yMax1), 0);
        //info.intersectionSize.z = Mathf.Max(Mathf.Min(zMax2, zMax1), 0);

        //return true;
    }