Beispiel #1
0
        public static bool Intersects(this IGeom geom, Bounds bounds)
        {
            //TODO - re-implement independent of geom, may speed this up
            var geom2 = new AABBox(bounds);

            return(Intersects(geom, geom2));
        }
 public static Sphere FromCollider(Collider c, BoundingSphereAlgorithm algorithm, bool local = false)
 {
     if (c is SphereCollider)
     {
         return(FromCollider(c as SphereCollider, local));
     }
     else if (c is CapsuleCollider)
     {
         return(FromCollider(c as CapsuleCollider, local));
     }
     else if (algorithm != BoundingSphereAlgorithm.FromBounds && c is MeshCollider)
     {
         return(FromMesh((c as MeshCollider).sharedMesh, algorithm, Trans.GetGlobal(c.transform)));
     }
     else
     {
         var bounds = AABBox.FromCollider(c, local);
         return(new Sphere(bounds.Center, bounds.Extents.magnitude));
     }
 }
Beispiel #3
0
        /// <summary>
        /// Attempts to calculate geometry for a collider. Not tested yet.
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static IPhysicsGeom GetGeom(this Collider c, BoundingSphereAlgorithm algorithm, bool local = false)
        {
            if (c == null)
            {
                return(null);
            }

            if (c is CharacterController)
            {
                return(Capsule.FromCollider(c as CharacterController, local));
            }
            if (c is CapsuleCollider)
            {
                return(Capsule.FromCollider(c as CapsuleCollider, local));
            }
            else if (c is BoxCollider)
            {
                return(Box.FromCollider(c as BoxCollider, local));
            }
            else if (c is SphereCollider)
            {
                return(Sphere.FromCollider(c as SphereCollider, local));
            }
            else if (algorithm != BoundingSphereAlgorithm.FromBounds && c is MeshCollider)
            {
                if (local)
                {
                    return(Sphere.FromMesh((c as MeshCollider).sharedMesh, algorithm));
                }
                else
                {
                    return(Sphere.FromMesh((c as MeshCollider).sharedMesh, algorithm, Trans.GetGlobal(c.transform)));
                }
            }
            else
            {
                //otherwise just return bounds as AABBox
                return(AABBox.FromCollider(c, local));
            }
        }
 public static bool Intersects(this IGeom geom, Bounds bounds)
 {
     //TODO - re-implement independent of geom, may speed this up
     var geom2 = new AABBox(bounds);
     return Intersects(geom, geom2);
 }
        public static Sphere FromPoints(Vector3[] points, BoundingSphereAlgorithm algorithm)
        {
            switch (algorithm)
            {
            case BoundingSphereAlgorithm.FromBounds:
            {
                var bounds = AABBox.FromPoints(points);
                return(new Sphere(bounds.Center, bounds.Extents.magnitude));
            }

            case BoundingSphereAlgorithm.Average:
            {
                Vector3 sum = Vector3.zero;
                foreach (var v in points)
                {
                    sum += v;
                }
                sum /= points.Length;
                float dist = 0f;
                float d;
                foreach (var v in points)
                {
                    d = (v - sum).sqrMagnitude;
                    if (d > dist)
                    {
                        dist = d;
                    }
                }
                dist = Mathf.Sqrt(dist);
                return(new Sphere(sum, dist));
            }

            case BoundingSphereAlgorithm.Ritter:
            {
                Vector3 xmin, xmax, ymin, ymax, zmin, zmax;
                xmin = ymin = zmin = Vector3.one * float.PositiveInfinity;
                xmax = ymax = zmax = Vector3.one * float.NegativeInfinity;
                foreach (var p in points)
                {
                    if (p.x < xmin.x)
                    {
                        xmin = p;
                    }
                    if (p.x > xmax.x)
                    {
                        xmax = p;
                    }
                    if (p.y < ymin.y)
                    {
                        ymin = p;
                    }
                    if (p.y > ymax.y)
                    {
                        ymax = p;
                    }
                    if (p.z < zmin.z)
                    {
                        zmin = p;
                    }
                    if (p.z > zmax.z)
                    {
                        zmax = p;
                    }
                }
                var xSpan   = (xmax - xmin).sqrMagnitude;
                var ySpan   = (ymax - ymin).sqrMagnitude;
                var zSpan   = (zmax - zmin).sqrMagnitude;
                var dia1    = xmin;
                var dia2    = xmax;
                var maxSpan = xSpan;
                if (ySpan > maxSpan)
                {
                    maxSpan = ySpan;
                    dia1    = ymin; dia2 = ymax;
                }
                if (zSpan > maxSpan)
                {
                    dia1 = zmin; dia2 = zmax;
                }
                var center = (dia1 + dia2) * 0.5f;
                var sqRad  = (dia2 - center).sqrMagnitude;
                var radius = Mathf.Sqrt(sqRad);
                foreach (var p in points)
                {
                    float d = (p - center).sqrMagnitude;
                    if (d > sqRad)
                    {
                        var r = Mathf.Sqrt(d);
                        radius = (radius + r) * 0.5f;
                        sqRad  = radius * radius;
                        var offset = r - radius;
                        center = (radius * center + offset * p) / r;
                    }
                }
                return(new Sphere(center, radius));
            }
            }

            return(new Sphere());
        }