Beispiel #1
0
        /// <summary>
        /// Check the collision between the hitbox and a point.
        /// </summary>
        /// <param name="pt">Point.</param>
        /// <param name="infinitePt">A point that can not be in the hitbox in any way.</param>
        /// <returns>True if there is a collision, false otherwise.</returns>
        public bool Collision(Vector2f pt, Vector2f infinitePt)
        {
            pt = InverseTransform.TransformPoint(pt);
            bool finalHit = false;

            foreach (var shape in Vertices)
            {
                bool currentHit = false;

                List <Segment> list = new List <Segment>();
                {
                    var      tmp = shape.Item1.ToArray();
                    Vector2f old = tmp.First();
                    for (int i = 1; i <= tmp.Length; i++)
                    {
                        list.Add(new Segment(old, tmp[i % tmp.Length]));
                        old = tmp[i % tmp.Length];
                    }
                }
                int hit = 0;
                foreach (var seg in list)
                {
                    if (seg.Collision(new Segment(infinitePt, pt)))
                    {
                        hit++;
                    }
                }
                currentHit = hit % 2 == 1;

                if (currentHit)
                {
                    if (shape.Item2 == CombineMode.ADD)
                    {
                        finalHit = true;
                    }
                    else if (shape.Item2 == CombineMode.REMOVE)
                    {
                        finalHit = false;
                    }
                    else if (shape.Item2 == CombineMode.INTERSECT)
                    {
                        finalHit = !finalHit;
                    }
                }
            }
            return(finalHit);
        }
Beispiel #2
0
 protected virtual Vector2f ToLocalCoordinates(Vector2f source)
 {
     return(InverseTransform.TransformPoint(source));
 }