Beispiel #1
0
        public static CollisionDetails CheckCollision(CheckCollisionSettings settings, Collider_Circle colOne, Collider_Circle colTwo)
        {
            Vector3 colOnePos = colOne.GetPosition();
            Vector3 colTwoPos = colTwo.GetPosition();

            //	Zero one of the axis based on the upAxis for 2D collision
            colOnePos = new Vector3(
                (settings.upAxis == VectorAxis.X) ? 0 : colOnePos.x,
                (settings.upAxis == VectorAxis.Y) ? 0 : colOnePos.y,
                (settings.upAxis == VectorAxis.Z) ? 0 : colOnePos.z);

            //	Zero one of the axis based on the upAxis for 2D collision
            colTwoPos = new Vector3(
                (settings.upAxis == VectorAxis.X) ? 0 : colTwoPos.x,
                (settings.upAxis == VectorAxis.Y) ? 0 : colTwoPos.y,
                (settings.upAxis == VectorAxis.Z) ? 0 : colTwoPos.z);

            float sqrDistance  = colOnePos.SqrDistanceTo(colTwoPos);
            float colOneRadius = colOne.ScaledRadius;
            float colTwoRadius = colTwo.ScaledRadius;

            float totalSqrRadius = (colOneRadius + colTwoRadius) * (colOneRadius + colTwoRadius);

            if (sqrDistance > totalSqrRadius)
            {
                return(null);
            }

            CollisionDetails details = new CollisionDetails(colOne, colTwo);

            return(details);
        }
        /// <summary>
        /// Returns true if both CollisionDetails have matching AteColliders.
        /// </summary>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (this.GetType() != obj.GetType())
            {
                return(false);
            }

            CollisionDetails details = (CollisionDetails)obj;
            //	Two match checks because they are order independant, as long as both match.
            bool matchOne = (this.colOne == details.colOne) && (this.colTwo == details.colTwo);
            bool matchTwo = (this.colOne == details.colTwo) && (this.colTwo == details.colOne);

            return(matchOne || matchTwo);
        }
        /// <summary>
        /// Checks registered collider pairs for active collisions.
        /// Cleans the registered colliders and collider pairs of null values before running.
        /// </summary>
        public List <CollisionDetails> FindCollisionDetails()
        {
            CleanRegistrationData();

            List <CollisionDetails> returnDetails = new List <CollisionDetails> ();

            //	For now, do not cache these so they can be tweaked with live in editor
            CheckCollisionSettings settings = new CheckCollisionSettings(maxCheckDistance, upAxis);
            //TODO: Eventually add #if UnityEditor for changing settings in editor but caching settings in game

            float sqrMaxCheckDistance = maxCheckDistance * maxCheckDistance;

            for (int i = 0; i < _colliderPairs.Count; i++)
            {
                //	Skip if they're too far away to compare
                float distance = _colliderPairs[i].v1.GetPosition().SqrDistanceTo(_colliderPairs[i].v2.GetPosition());
                if (distance > sqrMaxCheckDistance)
                {
                    continue;
                }

                //	If particular collision should be ignored
                if (_colliderPairs[i].v1.MyArea != null && _colliderPairs[i].v2.MyArea != null)
                {
                    if (_colliderPairs[i].v1.ignoreAreas.Contains(_colliderPairs[i].v2.MyArea))
                    {
                        continue;
                    }
                    if (_colliderPairs[i].v2.ignoreAreas.Contains(_colliderPairs[i].v1.MyArea))
                    {
                        continue;
                    }
                }

                CollisionDetails details = _colliderPairs[i].v1.CheckCollision(settings, _colliderPairs[i].v2);

                if (details != null)
                {
                    returnDetails.Add(details);
                }
            }

            return(returnDetails);
        }
Beispiel #4
0
        public static CollisionDetails CheckCollision(CheckCollisionSettings settings, Collider_Sphere colOne, Collider_Sphere colTwo)
        {
            Vector3 colOnePos = colOne.GetPosition();
            Vector3 colTwoPos = colTwo.GetPosition();

            float sqrDistance  = colOnePos.SqrDistanceTo(colTwoPos);
            float colOneRadius = colOne.ScaledRadius;
            float colTwoRadius = colTwo.ScaledRadius;

            float totalSqrRadius = (colOneRadius + colTwoRadius) * (colOneRadius + colTwoRadius);

            if (sqrDistance > totalSqrRadius)
            {
                return(null);
            }

            CollisionDetails details = new CollisionDetails(colOne, colTwo);

            return(details);
        }