Exemple #1
0
        public static bool IsColliding(CircleCollider c0, CircleCollider c1, out MTV mtv)
        {
            mtv = new MTV();
            var n  = math.normalizesafe(c0.Center - c1.Center);
            var p0 = new Projection(n, c0);
            var p1 = new Projection(n, c1);

            if (Projection.DoesOverlap(p0, p1))
            {
                mtv.Calculate(n, p0, p1, c0.Center, c1.Center);
                return(true);
            }
            return(false);
        }
Exemple #2
0
        public static bool IsColliding(
            CircleCollider circle,
            NativeArray <ColliderVertex> vertices,
            NativeArray <EdgeNormal> normals,
            float2 center,
            out MTV mtv)
        {
            mtv = MTV.Max;
            var shouldCheckCorner = true;

            for (int i = 0; i < normals.Length; i++)
            {
                var n  = normals[i].Value;
                var p0 = new Projection(n, circle);
                var p1 = new Projection(n, vertices);
                var d  = math.dot(circle.Center, n);
                shouldCheckCorner = shouldCheckCorner && !Projection.DoesOverlap(p1, d);

                if (!Projection.DoesOverlap(p0, p1))
                {
                    return(false);
                }

                mtv.Calculate(n, p0, p1, circle.Center, center);
            }

            if (shouldCheckCorner)
            {
                var closest = ClosestPoint(circle.Center, vertices);
                var n       = math.normalizesafe(closest - circle.Center);
                var p0      = new Projection(n, circle);
                var p1      = new Projection(n, vertices);

                if (!Projection.DoesOverlap(p0, p1))
                {
                    return(false);
                }

                mtv.Calculate(n, p0, p1, circle.Center, center);
            }

            return(true);
        }
Exemple #3
0
        public static bool IsColliding(
            NativeArray <ColliderVertex> v0,
            NativeArray <ColliderVertex> v1,
            NativeArray <EdgeNormal> n0,
            NativeArray <EdgeNormal> n1,
            float2 center0,
            float2 center1,
            out MTV mtv)
        {
            mtv = MTV.Max;
            for (int i = 0; i < n0.Length; i++)
            {
                var normal = n0[i].Value;
                var p0     = new Projection(normal, v0);
                var p1     = new Projection(normal, v1);
                if (!Projection.DoesOverlap(p0, p1))
                {
                    return(false);
                }

                mtv.Calculate(normal, p0, p1, center0, center1);
            }

            for (int i = 0; i < n1.Length; i++)
            {
                var normal = n1[i].Value;
                var p0     = new Projection(normal, v0);
                var p1     = new Projection(normal, v1);
                if (!Projection.DoesOverlap(p0, p1))
                {
                    return(false);
                }

                mtv.Calculate(normal, p0, p1, center1, center0);
            }

            return(true);
        }
        bool DoesCollide(PossibleCollision p, out MTV mtv)
        {
            var doesCollide = false;

            mtv = MTV.Max;
            if (p.SourceType == ColliderShapeType.Circle && p.TargetType == ColliderShapeType.Circle)
            {
                var c0 = CircleColliderFromEntity[p.SourceCollider];
                var c1 = CircleColliderFromEntity[p.TargetCollider];
                doesCollide = CollisionUtils.IsColliding(c0, c1, out mtv);
            }
            else if (p.SourceType == ColliderShapeType.Polygon && p.TargetType == ColliderShapeType.Circle)
            {
                var v = ColliderVertexFromEntity[p.SourceCollider].AsNativeArray();
                var n = ColliderNormalFromEntity[p.SourceCollider].AsNativeArray();
                var c = CircleColliderFromEntity[p.TargetCollider];
                doesCollide = CollisionUtils.IsColliding(c, v, n, p.SourceCenter, out mtv);
            }
            else if (p.SourceType == ColliderShapeType.Circle && p.TargetType == ColliderShapeType.Polygon)
            {
                var c = CircleColliderFromEntity[p.SourceCollider];
                var v = ColliderVertexFromEntity[p.TargetCollider].AsNativeArray();
                var n = ColliderNormalFromEntity[p.TargetCollider].AsNativeArray();
                doesCollide = CollisionUtils.IsColliding(c, v, n, p.TargetCenter, out mtv);
            }
            else if (p.SourceType == ColliderShapeType.Polygon && p.TargetType == ColliderShapeType.Polygon)
            {
                var v0 = ColliderVertexFromEntity[p.SourceCollider].AsNativeArray();
                var n0 = ColliderNormalFromEntity[p.SourceCollider].AsNativeArray();
                var v1 = ColliderVertexFromEntity[p.TargetCollider].AsNativeArray();
                var n1 = ColliderNormalFromEntity[p.TargetCollider].AsNativeArray();
                doesCollide = CollisionUtils.IsColliding(v0, v1, n0, n1, p.SourceCenter, p.TargetCenter, out mtv);
            }

            return(doesCollide);
        }
        void ApplyCollision(long id, Entity sourceCollider, Entity targetCollider, Entity targetEntity, MTV mtv)
        {
            var collisionBuffer = CollisionBufferFromEntity[sourceCollider];
            var sourcePos       = Positions[sourceCollider].Value;
            var targetPos       = Positions[targetCollider].Value;

            collisionBuffer.Add(new CollisionBuffer
            {
                Entity = targetEntity,
                IsNew  = !DoesExistInPreviousFrame(id),
                Point  = (sourcePos + targetPos / 2),
                Normal = mtv.Direction,
                Depth  = mtv.Magnitude,
            });
            CurrentFrameCollisions.TryAdd(id, 1);
        }