public LinkedList <AlignedBoundingBox> GetColliding(AlignedBoundingBox searchBounds) { LinkedList <AlignedBoundingBox> result = new LinkedList <AlignedBoundingBox>(); collisionSearch(searchBounds, Root, ref result); return(result); }
public override bool isColliding(AlignedBoundingBox bounds) { Vector2 PDiff = this.Position - bounds.Position; Vector2 PSize = bounds.Size + new Vector2(this.Size, this.Size); return( PDiff.X < PSize.X / 2 && PDiff.X > -PSize.X / 2 && PDiff.Y < PSize.Y / 2 && PDiff.Y > -PSize.Y / 2); }
private void insert(AlignedBoundingBox newBounds, QuadtreeNode current) { newBounds.ContainerTree = this; if (current.isColliding(newBounds) && current.Depth < MaxDepth) { if (current[0] == null) { current.Subdivide(); } insert(newBounds, current[0]); insert(newBounds, current[1]); insert(newBounds, current[2]); insert(newBounds, current[3]); } else if (current.isColliding(newBounds)) { current.InsertBounds(newBounds); } }
public override AlignedBoundingBox GenerateBounds(Vector2 Scale) { Vector2 minVector = new Vector2(0, 0); Vector2 maxVector = new Vector2(0, 0); bool doTerminate = false; PolygonPoint current = this.First; while (current != First || !doTerminate) { if (current.Point.X <= minVector.X) { minVector.X = current.Point.X; } if (current.Point.Y <= minVector.Y) { minVector.Y = current.Point.Y; } if (current.Point.X >= maxVector.X) { maxVector.X = current.Point.X; } if (current.Point.Y >= maxVector.Y) { maxVector.Y = current.Point.Y; } current = current.Next; doTerminate = true; } Vector2 Position = (minVector + maxVector) / 2; Vector2 Size = maxVector - minVector; AlignedBoundingBox result = new AlignedBoundingBox(new Vector2(0, 0), Size); result.RelativeColliderPosition = -Position; return(result); }
private void collisionSearch(AlignedBoundingBox searchBounds, QuadtreeNode current, ref LinkedList <AlignedBoundingBox> list) { if (current.isColliding(searchBounds) && current[0] != null) { collisionSearch(searchBounds, current[0], ref list); collisionSearch(searchBounds, current[1], ref list); collisionSearch(searchBounds, current[2], ref list); collisionSearch(searchBounds, current[3], ref list); } else if (current[0] == null) { if (current.Boxes.Count != 0) { foreach (AlignedBoundingBox b in current.Boxes) { if (b.isColliding(searchBounds) && !list.Contains(b) && b != searchBounds) { list.AddFirst(b); } } } } }
public void InsertBounds(AlignedBoundingBox newBounds) { Boxes.AddLast(newBounds); }
public Tuple <bool, Vector2> CheckColliderElement(ColliderElement other, Vector2 thisPosition, Vector2 otherPosition, Vector2 thisSize, Vector2 otherSize) { //First figure out where the bounds are in terms of the position of the tile Polygon ColliderBox = new Polygon(); ColliderBox.addPoint(Vector2.Zero); ColliderBox.addPoint(new Vector2(TileSize, 0)); ColliderBox.addPoint(new Vector2(TileSize, TileSize)); ColliderBox.addPoint(new Vector2(0, TileSize)); PolygonCollider Box = new PolygonCollider(ColliderBox); AlignedBoundingBox otherBounds = other.GenerateBounds(new Vector2(1, 1)); Vector2 Diff = (thisPosition + new Vector2(Map.Width * TileSize, Map.Height * TileSize) / 2 - otherPosition); Vector2 SizeB = new Vector2(Map.Width * TileSize, Map.Height * TileSize) + otherBounds.Size; //Vector2 average = topLeftB; if (Diff.X < SizeB.X / 2 && Diff.X > -SizeB.X / 2 && Diff.Y < SizeB.Y / 2 && Diff.Y > -SizeB.Y / 2) { Vector2 Dp = otherPosition - thisPosition; int x0 = (int)Math.Floor((Dp.X - otherBounds.Size.X / 2) / TileSize); int x1 = (int)Math.Ceiling((Dp.X + otherBounds.Size.X / 2) / TileSize); int y0 = (int)Math.Floor((Dp.Y - otherBounds.Size.Y / 2) / TileSize); int y1 = (int)Math.Ceiling((Dp.Y + otherBounds.Size.Y / 2) / TileSize); if (x0 < 0) { x0 = 0; } if (x1 >= Map.Width) { x1 = Map.Width; } if (y0 < 0) { y0 = 0; } if (y1 >= Map.Height) { y1 = Map.Height; } Corner0 = new Vector2(x0, y0) * TileSize; Corner1 = new Vector2(x1, y1) * TileSize; for (int x = x0; x < x1; x++) { for (int y = y0; y < y1; y++) { if (Map.TileData[x, y] != -1) { long id = Map.TileData[x, y]; int ID; long flippedHorizontal = id & HORIZONTALFLIP; long flippedVertical = id & VERTICALFLIP; long flippedDiagonal = id & DIAGONALFLIP; ID = (int)(id & ~(HORIZONTALFLIP | VERTICALFLIP | DIAGONALFLIP)); // //Now check to see if the polygons collide. ColliderElement.FlipProperties FlipType = ColliderElement.FlipProperties.None; if (flippedHorizontal == HORIZONTALFLIP) { FlipType |= ColliderElement.FlipProperties.FlipHorizontal; } if (flippedVertical == VERTICALFLIP) { FlipType |= ColliderElement.FlipProperties.FlipVertical; } if (flippedDiagonal == DIAGONALFLIP) { FlipType |= ColliderElement.FlipProperties.FlipDiagonal; } PolygonCollider CheckAgainst; if (Map.GetTileSet(ID).CollisionShapes.ContainsKey(ID)) { Map.GetTileSet(ID).CollisionShapes.TryGetValue(ID, out CheckAgainst); Console.WriteLine("Custom shape"); } else { //return new Tuple<bool, Vector2>(true, Vector2.Zero); CheckAgainst = Box; } return(other.GetCollisionData(CheckAgainst.Collider, otherPosition, thisPosition + new Vector2(x, y) * TileSize, otherSize, thisSize, FlipType, new Vector2(TileSize / 2, TileSize))); } } } return(new Tuple <bool, Vector2>(false, Vector2.Zero)); } else { return(new Tuple <bool, Vector2>(false, Vector2.Zero)); } }
public void InsertBound(AlignedBoundingBox newBounds) { insert(newBounds, Root); }
public abstract bool isColliding(AlignedBoundingBox bounds);