public Trigger(int x, int y, int w, int h, int id) { rect = new Rectanglef(x, y, w, h); this.id = id; if (count < id) count = id + 1; }
public Rectanglef(Rectanglef rect) { X = rect.X; Y = rect.Y; Width = rect.Width; Height = rect.Height; }
/// <summary> /// Bounding sphere style constructor 2 /// </summary> /// <param name="center">Center of the box</param> /// <param name="x">X radius from center</param> /// <param name="y">Y radius from center</param> public BoundingRectangle(Vector2 center, float x, float y) { dimensionsFromCenter = new Vector2(x, y); this.center = center; boundingRectangle = new Rectanglef(center.X - x, center.Y - y, 2 * x, 2 * y); }
/// <summary> /// Bounding sphere style constructor /// </summary> /// <param name="center">Center of the box</param> /// <param name="radius">Radius from center</param> public BoundingRectangle(Vector2 center, float radius) { dimensionsFromCenter = new Vector2(radius, radius); this.center = center; boundingRectangle = new Rectanglef(center.X - radius, center.Y - radius, 2 * radius, 2 * radius); }
/// <summary> /// Rectangle style constructor /// </summary> /// <param name="x">Left most X value</param> /// <param name="y">Top most Y value</param> /// <param name="width">Width of the box</param> /// <param name="height">Height of the box</param> public BoundingRectangle(float x, float y, float width, float height) { center = new Vector2(x + width / 2, y + height / 2); boundingRectangle = new Rectanglef(x, y, width, height); //boundingRectangle.IsEmpty; dimensionsFromCenter = new Vector2(width / 2, height / 2); }
public static Rectanglef Intersect(Rectanglef rect1, Rectanglef rect2) { Rectanglef result; if (rect1.Contains(rect2)) result = new Rectanglef(rect2); else if (rect2.Contains(rect1)) result = new Rectanglef(rect1); else if (!rect1.Intersects(rect2)) result = new Rectanglef(); else { result = new Rectanglef( Math.Max(rect1.X, rect2.X), Math.Max(rect1.Y, rect2.Y), Math.Min(rect1.Right, rect2.Right) - Math.Max(rect1.Left, rect2.Left), Math.Min(rect1.Bottom, rect2.Bottom) - Math.Max(rect1.Top, rect2.Top)); } return result; }
public void Contains(ref Rectanglef rectangle, out bool result) { result = rectangle.Left >= Left && rectangle.Right <= Right && rectangle.Top >= Top && rectangle.Bottom <= Bottom; }
public bool Contains(Rectanglef rectangle) { return rectangle.Left >= Left && rectangle.Right <= Right && rectangle.Top >= Top && rectangle.Bottom <= Bottom; }
public static void Union(ref Rectanglef rect1, ref Rectanglef rect2, out Rectanglef result) { float newX = Math.Min(rect1.X, rect2.X); float newY = Math.Min(rect1.Y, rect2.Y); float newW = Math.Max(rect1.Right, rect2.Right) - newX; float newH = Math.Max(rect1.Bottom, rect2.Bottom) - newY; result = new Rectanglef(newX, newY, newW, newH); }
public static Rectanglef Union(Rectanglef rect1, Rectanglef rect2) { float newX = Math.Min(rect1.X, rect2.X); float newY = Math.Min(rect1.Y, rect2.Y); float newW = Math.Max(rect1.Right, rect2.Right) - newX; float newH = Math.Max(rect1.Bottom, rect2.Bottom) - newY; Rectanglef result = new Rectanglef(newX, newY, newW, newH); return result; }
public bool Intersects(Rectanglef rect) { if (rect.Right < Left || rect.Left > Right || rect.Bottom < Top || rect.Top > Bottom) return false; return true; }
public static void Intersect(ref Rectanglef rect1, ref Rectanglef rect2, out Rectanglef result) { if (rect1.Contains(rect2)) result = new Rectanglef(rect2); else if (rect2.Contains(rect1)) result = new Rectanglef(rect1); else if (!rect1.Intersects(rect2)) result = new Rectanglef(); else { result = new Rectanglef( Math.Max(rect1.X, rect2.X), Math.Max(rect1.Y, rect2.Y), Math.Min(rect1.Right, rect2.Right) - Math.Max(rect1.Left, rect2.Left), Math.Min(rect1.Bottom, rect2.Bottom) - Math.Max(rect1.Top, rect2.Top)); } }
/// <summary> /// Checks for collision with a rectangle /// </summary> /// <param name="rectangle">Rectangle to check collision with</param> /// <returns>True if there is a collision</returns> public bool Collides(Rectanglef rectangle) { return boundingRectangle.Intersects(rectangle); }
/// <summary> /// Resolve inter penetration between two moveable objects /// </summary> /// <param name="overlap">Area of interpenetration</param> /// <param name="playerRectangle">Rectangle of the player</param> public void ResolveInterPenetrationWall(Rectanglef overlap, Rectanglef playerRectangle) { // If this happens, we've gotten stuck if (overlap.Contains(playerRectangle)) { int i = 0; } // If same width, collision is vertical else if (Math.Abs(overlap.Width - playerRectangle.Width) < 0.5) { if (Math.Abs(overlap.Y - playerRectangle.Y) < 0.5) { position.Y += (overlap.Height + 1); } else { position.Y -= (overlap.Height + 1); } } // If same height, collision is horizontal else if (Math.Abs(overlap.Height - playerRectangle.Height) < 0.5) { if (Math.Abs(overlap.X - playerRectangle.X) < 0.5) { position.X += (overlap.Width + 1); } else { position.X -= (overlap.Width + 1); } } else if (overlap.Width < overlap.Height) { if (Math.Abs(overlap.X - playerRectangle.X) < 0.0001) { position.X += (overlap.Width + 1); } else { position.X -= (overlap.Width + 1); } } else { if (Math.Abs(overlap.Y - playerRectangle.Y) < 0.0001) { position.Y += (overlap.Height + 1); } else { position.Y -= (overlap.Height + 1); } } }
/// <summary> /// Resolves a collision with a wall /// </summary> public void ResolveWallCollision(Rectanglef overlap) { // Normal Vector2 contactNormal = position - overlap.Center; if (contactNormal.LengthSquared() > 0) contactNormal.Normalize(); // Steps outlined in the class slides float Vs = -(1 + coefficientOfRestitution) * -Math.Abs(Vector2.Dot(velocity, contactNormal)); float deltaPD = (1 / mass); float g = Vs / deltaPD; contactNormal *= g; velocity += contactNormal / mass; movementDirection = velocity; if (movementDirection.LengthSquared() > 0) movementDirection.Normalize(); }
public Trigger(int x, int y, int w, int h) { rect = new Rectanglef(x, y, w, h); id = ++count; }