0
\$\begingroup\$

I am making my own collision detection in MonoGame. I have a PolyLine class That has a property to return the normal of that PolyLine instance. I have a ConvexPolySprite class that has a List LineSegments. I hav a CircleSprite class that has a Center Property and a Radius Property. I am using a static class for the collision detection method. I am testing it on a single line segment. Vector2(200,0) => Vector2(300, 200)

The problem is it detects the collision anywhere along the path of line out into space. I cannot figure out why. Thanks in advance;

enter image description here

enter image description here

enter image description here

public class PolyLine
{
    //---------------------------------------------------------------------------------------------------------------------------
    // Class Properties

    /// <summary>
    /// Property for the upper left-hand corner of the owner of this instance
    /// </summary>
    public Vector2 ParentPosition { get; set; }

    /// <summary>
    /// Relative start point of the line segment
    /// </summary>
    public Vector2 RelativeStartPoint { get; set; }

    /// <summary>
    /// Relative end point of the line segment
    /// </summary>
    public Vector2 RelativeEndPoint { get; set; }

    /// <summary>
    /// Property that gets the absolute position of the starting point of the line segment
    /// </summary>
    public Vector2 AbsoluteStartPoint
    {
        get
        {
            return ParentPosition + RelativeStartPoint;
        }

    }//end of AbsoluteStartPoint

    /// <summary>
    /// Gets the absolute position of the end point of the line segment
    /// </summary>
    public Vector2 AbsoluteEndPoint
    {
        get
        {
            return ParentPosition + RelativeEndPoint;
        }

    }//end of AbsoluteEndPoint

    public Vector2 NormalizedLeftNormal
    {
        get
        {
            Vector2 P = AbsoluteEndPoint - AbsoluteStartPoint;
            P.Normalize();
            float x = P.X;
            float y = P.Y;
            return new Vector2(-y, x);
        }

    }//end of NormalizedLeftNormal

    //---------------------------------------------------------------------------------------------------------------------------
    // Class Constructors

    /// <summary>
    /// Sole ctor
    /// </summary>
    /// <param name="parentPosition"></param>
    /// <param name="relStart"></param>
    /// <param name="relEnd"></param>
    public PolyLine(Vector2 parentPosition, Vector2 relStart, Vector2 relEnd)
    {
        ParentPosition = parentPosition;
        RelativeEndPoint = relEnd;
        RelativeStartPoint = relStart;

    }//end of ctor

}//end of PolyLine class



public static bool Collided(CircleSprite circle, ConvexPolygonSprite poly)
{
            var distance = Vector2.Dot(circle.Position - 
                poly.LineSegments[0].AbsoluteEndPoint, poly.LineSegments[0].NormalizedLeftNormal) + circle.Radius;

            if (distance <= 0)
            {
                return false;
            }
            else
            {
                return true;
            }

}//end of collided
\$\endgroup\$
1
\$\begingroup\$

To me that looks like code for intersecting a circle with an infinite line, not a line segment. It's determining the distance from the circle centre to the closest point on the line, and if it's less than the circle radius it's a collision. There are various ways to limit it to a line segment, e.g. check if the closest point is within the line segment, or use a parametric equation for the line segment and check whether t is in the range 0 to 1.

Formulae for intersections can be found on the net, e.g.: http://mathworld.wolfram.com/Circle-LineIntersection.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.