예제 #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;
        }
예제 #2
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;
            }
        }
예제 #3
0
        /// <summary>
        /// Check and create collison info for two given AABB colliders.
        /// </summary>
        /// <param name="_aabb1"></param>
        /// <param name="_aabb2"></param>
        private void AABB_AABB(ABBoxCollider _aabb1, ABBoxCollider _aabb2)
        {
            _aabb1.ComputeAABB();
            _aabb2.ComputeAABB();

            if (_aabb1.Min.y > _aabb2.Max.y &&
                _aabb1.Max.y < _aabb2.Min.y &&
                _aabb1.Min.x < _aabb2.Max.x &&
                _aabb1.Max.x > _aabb2.Min.x)
            {
                // Collision occured between colliders.
                ContactDetected = true;

                // Difference between the 2 colliders.
                var difference = _aabb1.Position - _aabb2.Position;

                var xPenetration = _aabb1.Size.x / 2 + _aabb2.Size.x / 2 - Mathf.Abs(difference.x);

                if (xPenetration > 0.0f)
                {
                    var yPenetration = _aabb1.Size.y / 2 + _aabb2.Size.y / 2 - Mathf.Abs(difference.y);

                    if (yPenetration > 0.0f)
                    {
                        if (xPenetration < yPenetration)
                        {
                            Normal      = difference.x < 0 ? Vector2.right : Vector2.left;
                            Penetration = xPenetration;
                        }
                        else
                        {
                            Normal      = difference.y <= 0 ? Vector2.up : Vector2.down;
                            Penetration = yPenetration;
                        }
                    }
                }
            }
        }