Beispiel #1
0
        /// <summary>
        /// Applies a transformation and returns the transformed one.
        /// </summary>
        /// <param name="tr">Transformation to apply.</param>
        /// <returns>Transformed hitbox.</returns>
        public Hitbox TransformHitbox(Transform tr)
        {
            var result = new Hitbox();

            foreach (var shape in Vertices)
            {
                var list = new List <Vector2f>();
                foreach (var pt in shape.Item1)
                {
                    list.Add(tr.TransformPoint(pt));
                }
                result.Vertices.Add(new Tuple <List <Vector2f>, CombineMode>(list, shape.Item2));
            }

            return(result);
        }
Beispiel #2
0
        public virtual object Clone()
        {
            Hitbox result = new Hitbox();

            result.Position = Position;
            result.Rotation = Rotation;
            result.Scale    = Scale;
            result.Origin   = Origin;
            foreach (var shape in Vertices)
            {
                var list = new List <Vector2f>();
                foreach (var pt in shape.Item1)
                {
                    list.Add(pt);
                }
                result.Vertices.Add(new Tuple <List <Vector2f>, CombineMode>(list, shape.Item2));
            }
            return(result);
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }
Beispiel #5
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>
 /// <returns>True if there is a collision, false otherwise.</returns>
 public bool Collision(Hitbox box2) => Collision(box2, new Vector2f(-999999, -999999));