Esempio n. 1
0
        /// <summary>
        /// Projects a collider onto an axis.
        /// </summary>
        /// <param name="collider">The collider to project onto the axis.</param>
        /// <param name="axis">The axis on which to project the collider.</param>
        /// <returns>Returns a ProjectionResult containing an upper and lower magnitude of the projection along the axis.</returns>
        public static ProjectionResult Project(Collider collider, SATAxis axis)
        {
            float min = float.MaxValue, max = float.MinValue;

            Vector2 axisn = axis.Vector.Normalized();

            foreach (Vector2 p in collider.Points)
            {
                float dp = Vector2.Dot(axisn, p + collider.Position);
                min = Math.Min(dp, min);
                max = Math.Max(dp, max);
            }

            return(new ProjectionResult(min, max));
        }
Esempio n. 2
0
        public Collider(Rectangle rect)
        {
            Type = ColliderType.AABB;
            float hw = rect.Width / 2;
            float hh = rect.Height / 2;

            Position = new Vector2(rect.X + hw, rect.Y + hh);
            BBCenter = Position;
            BBRadius = new Vector2(hw, hh);

            Points = new Vector2[] {
                new Vector2(rect.X, rect.Y),
                new Vector2(rect.X + rect.Width, rect.Y),
                new Vector2(rect.X + rect.Width, rect.Y + rect.Height),
                new Vector2(rect.X, rect.Y + rect.Height)
            };
            Axes = new SATAxis[] {
                SATAxis.FromLine(new Vector2(rect.X, rect.Y), new Vector2(rect.X + 1, rect.Y)),
                SATAxis.FromLine(new Vector2(rect.X, rect.Y), new Vector2(rect.X, rect.Y - 1))
            };
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a Collider struct from a polygon defined by an array of vectors.
        /// </summary>
        /// <param name="poly">The array of vectors defining the polygon.</param>
        public Collider(Vector2[] poly, Vector2 position, Vector2?offset = null)
        {
            // Polygon type collider setup
            Type = ColliderType.Poly;

            // Copy the polygon into the Points array
            Points = new Vector2[poly.Length];
            poly.CopyTo(Points, 0);

            // Setup the temprary axis list
            var ta        = new List <SATAxis>();
            var gradients = new List <float>();

            // Initialise the bounding box values
            BBCenter = new Vector2();
            BBRadius = new Vector2();
            //Position = Points[0];

            Vector2 off = offset ?? new Vector2();

            for (int i = 0; i < Points.Length; i++)
            {
                Points[i] -= off;
            }
            position += off;
            Position  = position;

            float mx = Points[0].X - position.X;
            float Mx = Points[0].X - position.X;
            float my = Points[0].Y - position.Y;
            float My = Points[0].Y - position.Y;

            for (int i = 0, j = 1; i < Points.Length; i++, j = (j + 1) % Points.Length)
            {
                // Set the position to the minimal X/Y coords, and the bounding box radius to the width/height
                //Position.X = Math.Min(Position.X, Points[i].X);
                //Position.Y = Math.Min(Position.Y, Points[i].Y);
                //BBRadius.X = Math.Max(BBRadius.X, Math.Abs(Position.X - Points[i].X));
                //BBRadius.Y = Math.Max(BBRadius.Y, Math.Abs(Position.Y - Points[i].Y));
                // Update the polygon bounding box.
                mx = Math.Min(mx, Points[i].X - position.X);
                Mx = Math.Max(Mx, Points[i].X - position.X);
                my = Math.Min(my, Points[i].Y - position.Y);
                My = Math.Max(My, Points[i].Y - position.Y);

                // Calculate the gradient of the line
                float gradient = (Points[i].X - Points[j].X) / (Points[i].Y - Points[j].Y);

                // Ignore this axis if we've already stored it by checking against our known gradients.
                if (gradients.Contains(gradient))
                {
                    continue;
                }
                gradients.Add(gradient);

                // Add unique axes to the list
                ta.Add(SATAxis.FromLine(Points[i], Points[j]));
            }

            // Calculate the bounding radius.
            BBRadius = new Vector2(Mx - mx, My - my) / 2f;

            // Set the bounding box center to the middle of the bounding box.
            BBCenter = new Vector2(mx, my) + BBRadius + position;

            // Store all the computed axes into the array.
            Axes = ta.ToArray();
        }