C# Collision Math Help

Posted by user36037 on Game Development See other posts from Game Development or by user36037
Published on 2013-10-12T23:18:09Z Indexed on 2013/11/12 10:24 UTC
Read the original article Hit count: 197

Filed under:

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

© Game Development or respective owner

Related posts about monogame