Exemplo n.º 1
0
        /// <summary>
        /// Create a new combined hitbox containing multiple shapes from other created hitboxes.
        /// </summary>
        /// <param name="hitbox1">Base hitbox.</param>
        /// <param name="hitbox2">Other hitbox.</param>
        /// <param name="combine">The way the other hitbox will be computed.</param>
        /// <returns>Combined hitbox.</returns>
        public static Hitbox Combine(Hitbox hitbox1, Hitbox hitbox2, CombineMode combine = CombineMode.DEFAULT)
        {
            hitbox1 = hitbox1.TransformHitbox();
            hitbox2 = hitbox2.TransformHitbox();
            Hitbox result = new Hitbox();

            foreach (var v in hitbox1.Vertices)
            {
                result.Vertices.Add(new Tuple <List <Vector2f>, CombineMode>(v.Item1, v.Item2));
            }
            foreach (var v in hitbox2.Vertices)
            {
                if (combine == CombineMode.DEFAULT)
                {
                    result.Vertices.Add(new Tuple <List <Vector2f>, CombineMode>(v.Item1, v.Item2));
                }
                else
                {
                    result.Vertices.Add(new Tuple <List <Vector2f>, CombineMode>(v.Item1, combine));
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Check the collision between two hitboxes. May be very heavy for complexe hitboxes in
        /// certain cases.
        /// </summary>
        /// <param name="box2">Second hitbox.</param>
        /// <param name="infinitePt">A point that can not be in any hitbox in any way.</param>
        /// <returns>True if there is a collision, false otherwise.</returns>
        public bool Collision(Hitbox box2, Vector2f infinitePt)
        {
            Hitbox box1 = TransformHitbox();

            box2 = box2.TransformHitbox();
            foreach (var shape in box1.Vertices)
            {
                if (shape.Item2 != CombineMode.REMOVE)
                {
                    foreach (var pt in shape.Item1)
                    {
                        if (box2.Collision(pt))
                        {
                            return(true);
                        }
                    }
                }
            }
            foreach (var shape in box2.Vertices)
            {
                if (shape.Item2 != CombineMode.REMOVE)
                {
                    foreach (var pt in shape.Item1)
                    {
                        if (box1.Collision(pt))
                        {
                            return(true);
                        }
                    }
                }
            }
            List <Vector2f> inters = new List <Vector2f>();

            foreach (var shape1 in box1.Vertices)
            {
                foreach (var shape2 in box2.Vertices)
                {
                    var            vert1 = shape1.Item1;
                    var            vert2 = shape2.Item1;
                    List <Segment> list1 = new List <Segment>();
                    List <Segment> list2 = new List <Segment>();

                    {
                        var      tmp = vert1.ToArray();
                        Vector2f old = tmp.First();
                        for (int i = 1; i <= tmp.Length; i++)
                        {
                            list1.Add(new Segment(old, tmp[i % tmp.Length]));
                            old = tmp[i % tmp.Length];
                        }
                    }
                    {
                        var      tmp = vert2.ToArray();
                        Vector2f old = tmp.First();
                        for (int i = 1; i <= tmp.Length; i++)
                        {
                            list2.Add(new Segment(old, tmp[i % tmp.Length]));
                            old = tmp[i % tmp.Length];
                        }
                    }
                    foreach (var seg1 in list1)
                    {
                        foreach (var seg2 in list2)
                        {
                            if (seg1.Collision(seg2))
                            {
                                inters.Add(seg1.Intersection(seg2));
                            }
                        }
                    }
                }
            }
            foreach (var pt in inters)
            {
                if (box1.Collision(pt, infinitePt) && box2.Collision(pt, infinitePt))
                {
                    return(true);
                }
            }
            return(false);
        }