Ejemplo n.º 1
0
        public static bool detectCircleEdge(scTransform circleTransform, scCircleShape circle, scTransform edgeTransform, scEdgeShape edge)
        {
            var transformedEdgeVertices = new List<Vector2>();
            transformedEdgeVertices.Add(scTransformUtils.applyTransform(edgeTransform, edge.start));
            transformedEdgeVertices.Add(scTransformUtils.applyTransform(edgeTransform, edge.end));

            var transformedCircleCenter = scTransformUtils.applyTransform(circleTransform, circle.localPosition);

            //get nearest edge vertex to circle center
            var distanceToFirstVertex = transformedEdgeVertices[0] - transformedCircleCenter;
            var distanceToSecondVertex = transformedEdgeVertices[1] - transformedCircleCenter;

            var circleAxis = distanceToFirstVertex.Length() <= distanceToSecondVertex.Length() ? distanceToFirstVertex : distanceToSecondVertex;
            circleAxis.Normalize();
            var edgeAxis = scCollisionHelpers.getNormal(transformedEdgeVertices[0], transformedEdgeVertices[1]);

            {
                var lineSegmentCircle = scCollisionHelpers.projectCircleOnAxis(transformedCircleCenter, circle.radius, circleAxis);
                var lineSegmentEdge = scCollisionHelpers.projectShapeOnAxis(transformedEdgeVertices, circleAxis);
                if (!scCollisionHelpers.overlapsOnSameAxis(lineSegmentCircle, lineSegmentEdge))
                {
                    return false;
                }
            }
            {
                var lineSegmentCircle = scCollisionHelpers.projectCircleOnAxis(transformedCircleCenter, circle.radius, edgeAxis);
                var lineSegmentEdge = scCollisionHelpers.projectShapeOnAxis(transformedEdgeVertices, edgeAxis);
                if (!scCollisionHelpers.overlapsOnSameAxis(lineSegmentCircle, lineSegmentEdge))
                {
                    return false;
                }
            }
            return true;
        }
Ejemplo n.º 2
0
        public static bool detectCircleCircle(scTransform aTransform, scCircleShape aShape, scTransform bTransform, scCircleShape bShape)
        {
            var aCenter = aTransform.position + aShape.localPosition;
            var bCenter = bTransform.position + bShape.localPosition;

            var deltaVector = aCenter - bCenter;

            return deltaVector.Length() < aShape.radius + bShape.radius;
        }
Ejemplo n.º 3
0
        public static bool detectCircleRectangle(scTransform circleTransform, scCircleShape circle, scTransform rectangleTransform, scRectangleShape rectangle)
        {
            var transformedRectangleVertices = new List<Vector2>();
            foreach (var vertex in rectangle.vertices)
            {
                transformedRectangleVertices.Add(scTransformUtils.applyTransform(rectangleTransform, vertex));
            }

            var axes = new List<Vector2>();
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[0], transformedRectangleVertices[1]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[1], transformedRectangleVertices[2]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[2], transformedRectangleVertices[3]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[3], transformedRectangleVertices[0]));
            var transformedCircleCenter = scTransformUtils.applyTransform(circleTransform, circle.localPosition);

            //get nearest edge vertex to circle center
            var distancesToVertices = new List<Vector2>();

            distancesToVertices.Add(transformedRectangleVertices[0] - transformedCircleCenter);
            distancesToVertices.Add(transformedRectangleVertices[1] - transformedCircleCenter);
            distancesToVertices.Add(transformedRectangleVertices[2] - transformedCircleCenter);
            distancesToVertices.Add(transformedRectangleVertices[3] - transformedCircleCenter);

            var circleAxis = distancesToVertices[0];
            foreach (var distance in distancesToVertices)
            {
                if (distance.Length() < circleAxis.Length())
                {
                    circleAxis = distance;
                }
            }

            circleAxis.Normalize();
            axes.Add(circleAxis);

            foreach (var axis in axes)
            {
                var lineSegmentRectangleA = scCollisionHelpers.projectCircleOnAxis(transformedCircleCenter, circle.radius, axis);
                var lineSegmentRectangleB = scCollisionHelpers.projectShapeOnAxis(transformedRectangleVertices, axis);
                if (!scCollisionHelpers.overlapsOnSameAxis(lineSegmentRectangleA, lineSegmentRectangleB))
                {
                    return false;
                }
            }
            return true;
        }
Ejemplo n.º 4
0
 public scBoundingBox getBoundingBox(scTransform transform)
 {
     var transformedStart = scTransformUtils.applyTransform(transform, start);
     var transformedEnd = scTransformUtils.applyTransform(transform, end);
     return scBoundingUtils.createFromBoundingVertices(transformedStart, transformedEnd);
 }
Ejemplo n.º 5
0
 public scBoundingBox getBoundingBox(scTransform transform)
 {
     return new scBoundingBox() { position = localPosition + transform.position, halfExtents = new Vector2(radius) };
 }
Ejemplo n.º 6
0
        public scBoundingBox getBoundingBox(scTransform transform)
        {
            var lower = new Vector2();
            var upper = new Vector2();

            var rotated = new Vector2[4];
            rotated[0] = transform.position + vertices[0].Rotate(transform.rotation.radians);
            rotated[1] = transform.position + vertices[1].Rotate(transform.rotation.radians);
            rotated[2] = transform.position + vertices[2].Rotate(transform.rotation.radians);
            rotated[3] = transform.position + vertices[3].Rotate(transform.rotation.radians);

            lower.X = rotated.Min(v => v.X);
            lower.Y = rotated.Min(v => v.Y);
            upper.X = rotated.Max(v => v.X);
            upper.Y = rotated.Max(v => v.Y);

            return scBoundingUtils.createFromBoundingVertices(lower, upper);
        }
Ejemplo n.º 7
0
        public void DrawShape(scDebugData debugData, SpriteBatch spriteBatch, scShape shape, scTransform transform)
        {
            switch (shape.ShapeType)
            {
                case scShapeType.Circle:
                {
                    var circle = (scCircleShape)shape;
                    spriteBatch.DrawCircle(transform.position + circle.localPosition, circle.radius, debugData.CircleShapeSides, debugData.CircleShapeColor);
                    break;
                }
                case scShapeType.Rectangle:
                {
                    var rectangle = (scRectangleShape)shape;

                    var A = transform.position + rectangle.vertices[0].Rotate(transform.rotation.radians);
                    var B = transform.position + rectangle.vertices[1].Rotate(transform.rotation.radians);
                    var C = transform.position + rectangle.vertices[2].Rotate(transform.rotation.radians);
                    var D = transform.position + rectangle.vertices[3].Rotate(transform.rotation.radians);

                    spriteBatch.DrawLine(A, B, debugData.RectangleShapeColor);
                    spriteBatch.DrawLine(B, C, debugData.RectangleShapeColor);
                    spriteBatch.DrawLine(C, D, debugData.RectangleShapeColor);
                    spriteBatch.DrawLine(D, A, debugData.RectangleShapeColor);

                    break;
                }
                case scShapeType.Edge:
                {
                    var edge = (scEdgeShape)shape;
                    var start = scTransformUtils.applyTransform(transform, edge.start);
                    var end = scTransformUtils.applyTransform(transform, edge.end);
                    spriteBatch.DrawLine(start, end, debugData.EdgeShapeColor);
                    break;
                }
            }
        }
Ejemplo n.º 8
0
        public static bool detectRectangleRectangle(scTransform aTransform, scRectangleShape aShape, scTransform bTransform, scRectangleShape bShape)
        {
            var transformedRectangleVerticesA = new List<Vector2>();
            var transformedRectangleVerticesB = new List<Vector2>();
            foreach (var vertex in aShape.vertices)
            {
                transformedRectangleVerticesA.Add(scTransformUtils.applyTransform(aTransform, vertex));
            }
            foreach (var vertex in bShape.vertices)
            {
                transformedRectangleVerticesB.Add(scTransformUtils.applyTransform(bTransform, vertex));
            }

            var axes = new List<Vector2>();
            // add axes of aShape
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVerticesA[0], transformedRectangleVerticesA[1]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVerticesA[1], transformedRectangleVerticesA[2]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVerticesA[2], transformedRectangleVerticesA[3]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVerticesA[3], transformedRectangleVerticesA[0]));
            // add axes of bShape
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVerticesB[0], transformedRectangleVerticesB[1]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVerticesB[1], transformedRectangleVerticesB[2]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVerticesB[2], transformedRectangleVerticesB[3]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVerticesB[3], transformedRectangleVerticesB[0]));

            foreach (var axis in axes)
            {
                var lineSegmentRectangleA = scCollisionHelpers.projectShapeOnAxis(transformedRectangleVerticesA, axis);
                var lineSegmentRectangleB = scCollisionHelpers.projectShapeOnAxis(transformedRectangleVerticesB, axis);
                if (!scCollisionHelpers.overlapsOnSameAxis(lineSegmentRectangleA, lineSegmentRectangleB))
                {
                    return false;
                }
            }

            return true;
        }
Ejemplo n.º 9
0
        public static bool detectRectangleEdge(scTransform rectangleTransform, scRectangleShape rectangle, scTransform edgeTransform, scEdgeShape edge)
        {
            var transformedRectangleVertices = new List<Vector2>();
            foreach (var vertex in rectangle.vertices)
            {
                transformedRectangleVertices.Add(scTransformUtils.applyTransform(rectangleTransform, vertex));
            }

            var edgeStart = scTransformUtils.applyTransform(edgeTransform, edge.start);
            var edgeEnd = scTransformUtils.applyTransform(edgeTransform, edge.end);

            var transformedEdgeVertices = new List<Vector2>();
            transformedEdgeVertices.Add(edgeStart);
            transformedEdgeVertices.Add(edgeEnd);

            var axes = new List<Vector2>();
            axes.Add(scCollisionHelpers.getNormal(edgeStart, edgeEnd));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[0], transformedRectangleVertices[1]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[1], transformedRectangleVertices[2]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[2], transformedRectangleVertices[3]));
            axes.Add(scCollisionHelpers.getNormal(transformedRectangleVertices[3], transformedRectangleVertices[0]));

            foreach (var axis in axes)
            {
                var lineSegmentRectangle = scCollisionHelpers.projectShapeOnAxis(transformedRectangleVertices, axis);
                var lineSegmentEdge = scCollisionHelpers.projectShapeOnAxis(transformedEdgeVertices, axis);
                if (!scCollisionHelpers.overlapsOnSameAxis(lineSegmentRectangle, lineSegmentEdge))
                {
                    return false;
                }
            }

            return true;
        }
Ejemplo n.º 10
0
 public static bool detectEdgeEdge(scTransform aTransform, scEdgeShape aShape, scTransform bTransform, scEdgeShape bShape)
 {
     return false;
 }
Ejemplo n.º 11
0
 public static Vector2 applyTransform(scTransform transform, Vector2 vector)
 {
     return transform.position + vector.Rotate(transform.rotation.radians);
 }