public WorldObject(Vector2 position, Vector2 size, Single rotation) { this.Position = position; this.Size = size; this.Rotation = rotation; this.Bounds = new RotatedRectangle(new RectangleF(this.Position - this.Size / 2, this.Size), this.Rotation); }
public GridLocation(UInt16 X, UInt16 Y, Single cellWidth, Single cellHeight) { this.X = X; this.Y = Y; this.Width = cellWidth; this.Height = cellHeight; this.Bounds = new RotatedRectangle(X, Y, Width, Height, 0); }
public GridLocation(Int16 X, Int16 Y, Vector2 worldCellSize) { this.X = X; this.Y = Y; this.Bounds = new RotatedRectangle(X * worldCellSize.X, Y * worldCellSize.Y, worldCellSize.X, worldCellSize.Y, 0); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { //Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); RectangleTexture = Content.Load<Texture2D>("Square"); MyFont = Content.Load<SpriteFont>("MyFont"); RectangleA = new RotatedRectangle(new RectangleF(100, 200, RectangleTexture.Width, RectangleTexture.Height), 0.0f); RectangleB = new RotatedRectangle(new RectangleF(300, 200, 230, 390), 0.0f); }
public Sprite(World world, Texture2D texture, Vector2 position, Vector2 size, Single rotation, Color color) { this.World = world; this.Texture = texture; this.Position = position; this.Size = size; this.Rotation = rotation; this.Color = color; this.Bounds = new RotatedRectangle(new RectangleF(this.Position - this.Size / 2, this.Size), this.Rotation); }
/// <summary> /// Determines if a collision has occurred on an axis of one of the planes parallel to the Rectangle. /// </summary> /// <param name="rectangle"></param> /// <param name="axis"></param> /// <param name="overlap"></param> /// <returns></returns> private bool IsAxisCollision(RotatedRectangle rectangle, Vector2 axis, out Single overlap) { // project both rectangles onto the axis Projection curProj = this.Project(axis); Projection otherProj = rectangle.Project(axis); // do the projections overlap? if (curProj.GetOverlap(otherProj) < 0) { overlap = 0; axis = Vector2.Zero; return false; } // get the overlap overlap = curProj.GetOverlap(otherProj); // check for containment if (curProj.Contains(otherProj) || otherProj.Contains(curProj)) { // get the overlap plus the distance from the minimum end points Single mins = Math.Abs(curProj.Min - otherProj.Min); Single maxs = Math.Abs(curProj.Max - otherProj.Max); // NOTE: depending on which is smaller you may need to negate the separating axis if (mins < maxs) overlap += mins; else overlap += maxs; } // and return true for an axis collision return true; }
/// <summary> /// Check to see if two <see cref="RotatedRectangle"/>s have collided. /// </summary> /// <param name="rectangle"></param> /// <param name="overlap"></param> /// <param name="collisionProjection"></param> /// <returns></returns> public bool Intersects(RotatedRectangle rectangle, out Single overlap, out Vector2 collisionProjection) { // Calculate the axes we will use to determine if a collision has occurred // Since the objects are rectangles, we only have to generate 4 axes (2 for // each rectangle) since we know the other 2 on a rectangle are parallel. Vector2[] axes = { UpperRight - UpperLeft, UpperRight - LowerRight, rectangle.UpperLeft - rectangle.LowerLeft, rectangle.UpperLeft - rectangle.UpperRight }; // Cycle through all of the axes we need to check. If a collision does not occur // on ALL of the axes, then a collision is NOT occurring. We can then exit out // immediately and notify the calling function that no collision was detected. If // a collision DOES occur on ALL of the axes, then there is a collision occurring // between the rotated rectangles. We know this to be true by the Seperating Axis Theorem. // In addition, overlap is tracked so that the smallest overlap can be stored for the caller. Single bestOverlap = Single.MaxValue; Vector2 bestCollisionProjection = Vector2.Zero; Single o; foreach (Vector2 axis in axes) { // required for accurate projections axis.Normalize(); if (!IsAxisCollision(rectangle, axis, out o)) { // if there is no axis collision, we can guarantee they do not overlap overlap = 0; collisionProjection = Vector2.Zero; return false; } // do we have the smallest overlap yet? if (o < bestOverlap) { bestOverlap = o; bestCollisionProjection = axis; } } // it is now guaranteed that the rectangles intersect for us to have gotten this far overlap = bestOverlap; collisionProjection = bestCollisionProjection; // now we want to make sure the collision projection vector points from the other rectangle to us Vector2 centerToCenter; centerToCenter.X = (rectangle.X + rectangle.Origin.X) - (this.X + this.Origin.X); centerToCenter.Y = (rectangle.Y + rectangle.Origin.Y) - (this.Y + this.Origin.Y); if (Vector2.Dot(collisionProjection, centerToCenter) > 0) Vector2.Negate(ref collisionProjection, out collisionProjection); return true; }
/// <summary> /// Checks to see if two <see cref="RotatedRectangle"/>s have collided. /// </summary> /// <param name="rectangle"></param> /// <returns></returns> public bool Intersects(RotatedRectangle rectangle) { Single overlap; Vector2 collisionProjection; return Intersects(rectangle, out overlap, out collisionProjection); }
public void ReCalcBounds() { this.Bounds = new RotatedRectangle(new RectangleF(this.Position - this.Size / 2, this.Size), this.Rotation); }