Ejemplo n.º 1
0
        /// <summary>
        /// This function will check if any vertex of a triangle is touching this triangle
        /// </summary>
        /// <param name="other">The <see cref="PTriangle"/> to check collision with</param>
        /// <returns>A bool indicating whether or not the <see cref="PTriangle"/>s are touching</returns>
        /// https://stackoverflow.com/questions/2778240/detection-of-triangle-collision-in-2d-space
        public bool IsTouching(PTriangle other)
        {
            bool inPointOne   = ContainsPoint(other.VertexOne);
            bool inPointTwo   = ContainsPoint(other.VertexTwo);
            bool inPointThree = ContainsPoint(other.VertexThree);

            bool inPointFour = other.ContainsPoint(this.VertexOne);
            bool inPointFive = other.ContainsPoint(this.VertexTwo);
            bool inPointSix  = other.ContainsPoint(this.VertexThree);

            if (inPointOne || inPointTwo || inPointThree)
            {
                return(true);
            }

            if (inPointFour || inPointFive || inPointSix)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks whether or not a given vertex is an ear of the given polygon
        /// </summary>
        /// <param name="vertices">Vertices composing the edges of the polygon</param>
        /// <param name="vert">The vertex to check</param>
        /// <param name="clockwise">Indicates which direction the vertices list rotates around the polygon</param>
        /// <returns>A bool indicating if the vertex is an ear</returns>
        private bool VertexIsEar(List <Vector2> vertices, int vert, bool clockwise)
        {
            if (vert < 0 || vert >= vertices.Count)
            {
                throw new ArgumentOutOfRangeException("Argument vert out of range");
            }

            // Get vertices for the relevant triangle
            int vert1 = vertices.GetPrevIndex(vert);
            int vert2 = vert;
            int vert3 = vertices.GetNextIndex(vert);

            // Determine if the angle at vert is reflex
            float crossProd = Vector2.CrossProduct(vertices[vert1], vertices[vert2], vertices[vert3]);

            if ((crossProd > 0 && clockwise) || (crossProd < 0 && !clockwise))
            {
                return(false);
            }

            // Check if any point is inside the triangle formed from this vertex
            PTriangle tri = new PTriangle(vertices[vert1], vertices[vert2], vertices[vert3]);

            for (int i = 0; i < vertices.Count; i++)
            {
                // No point checking the vertices of the current triangle
                if (i != vert1 && i != vert2 && i != vert3)
                {
                    if (tri.ContainsPoint(vertices[i]))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PRectangle"/> struct using 4 verticies
 /// </summary>
 /// <param name="v1">Top left corner</param>
 /// <param name="v2">Top right corner</param>
 /// <param name="v3">Bottom left corner</param>
 /// <param name="v4">Bottom right corner</param>
 public PRectangle(Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4)
 {
     TriOne = new PTriangle(v1, v2, v3);
     TriTwo = new PTriangle(v3, v2, v4);
     Area   = TriOne.Area + TriTwo.Area;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PRectangle"/> struct using 2 verticies
 /// </summary>
 /// <param name="v1">Top left corner</param>
 /// <param name="v2">Bottom right corner</param>
 public PRectangle(Vector2 v1, Vector2 v2)
 {
     TriOne = new PTriangle(v1, v2, new Vector2(v2.X, v1.Y));
     TriTwo = new PTriangle(v1, v2, new Vector2(v1.X, v2.Y));
     Area   = TriOne.Area + TriTwo.Area;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TriangleCollider"/> class using a triangle
 /// </summary>
 /// <param name="tri">Triangle to use for creating Collider</param>
 public TriangleCollider(PTriangle tri)
 {
     TrianglesInternal = new PTriangle[] { tri };
     VerticesInternal  = new Vector2[] { tri.VertexOne, tri.VertexTwo, tri.VertexThree };
 }