Esempio n. 1
0
        /// <summary>
        /// Check and create collision info between a circle and AABB collider.
        /// </summary>
        /// <param name="_circle"></param>
        /// <param name="_aabb"></param>
        private void Circle_AABB(ABCircleCollider _circle, ABBoxCollider _aabb)
        {
            AABB_Circle(_aabb, _circle);

            // because pair order gets flipped here, the normal also needs to be flipped.
            Normal = -Normal;
        }
Esempio n. 2
0
        /// <summary>
        /// Check and create collision info for two given circle colliders.
        /// </summary>
        /// <param name="_circle1"></param>
        /// <param name="_circle2"></param>
        private void Circle_Circle(ABCircleCollider _circle1, ABCircleCollider _circle2)
        {
            var normal = _circle2.Position - _circle1.Position;

            var distSqr   = normal.sqrMagnitude;
            var radiusSum = _circle1.Radius + _circle2.Radius;

            if (distSqr >= radiusSum * radiusSum)
            {
                ContactDetected = false;
                return;
            }

            ContactDetected = true;
            var distance = Mathf.Sqrt(distSqr);

            if (distance == 0.0f)
            {
                Penetration = _circle1.Radius;
                Normal      = Vector2.up;
            }
            else
            {
                Penetration = radiusSum - distance;
                Normal      = normal.normalized;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Check and create collision info between an AABB and circle collider.
        /// </summary>
        /// <param name="_aabb"></param>
        /// <param name="_circle"></param>
        private void AABB_Circle(ABBoxCollider _aabb, ABCircleCollider _circle)
        {
            _aabb.ComputeAABB();

            var xNear = Mathf.Max(_aabb.Min.x, Mathf.Min(_circle.Position.x, _aabb.Max.x));
            var yNear = Mathf.Max(_aabb.Max.y, Mathf.Min(_circle.Position.y, _aabb.Min.y));

            var closesPointToRect = new Vector2(xNear, yNear);

            var normal = _circle.Position - closesPointToRect;

            if (normal.sqrMagnitude < _circle.Radius * _circle.Radius)
            {
                // Collision between the circle and aabb.
                ContactDetected = true;
                Penetration     = _circle.Radius - normal.magnitude;
                Normal          = normal.normalized;
            }
        }