private CollisionManager() { this.currentDrawType = DRAW_TYPE.DRAW_ALL; this.bodyGlobalSet = new XboxHashSet<CollisionBody>(); this.bodyGroupList = new List<CollisionBody>(); this.bodyGroupHashSet = new XboxHashSet<CollisionBody>(); this.primitiveBatch = new PrimitiveBatch(Program.GAME.GraphicsDevice); font = Program.GAME.Content.Load<SpriteFont>("MenuFont"); int poolSize = (int)Math.Pow(4, nodeDepth - 2); this.nodeArrayPool = new List<QuadTreeNode[]>(poolSize); for (int i = 0; i < poolSize; i++) { QuadTreeNode[] array = new QuadTreeNode[4]; array[0] = new QuadTreeNode(); array[1] = new QuadTreeNode(); array[2] = new QuadTreeNode(); array[3] = new QuadTreeNode(); this.nodeArrayPool.Add(array); } this.bodySetPool = new List<XboxHashSet<CollisionBody>>(poolSize); for (int i = 0; i < poolSize; i++) { this.bodySetPool.Add(new XboxHashSet<CollisionBody>(maxNodeCapacity + 1)); } }
public void reset(/*QuadTreeNode parent, */float x, float y, float width, float height, int depth, int maxBodyCount, int maxDepth) { this.depth = depth; this.x1 = x; this.y1 = y; this.x2 = x1 + width; this.y2 = y1 + height; this.width = width; this.height = height; //this.parent = null; this.children = null; this.items = Program.GAME.CollisionManager.obtainCollisionBodySet(); this.pointA = new Vector2(); this.pointA.X = this.x1; this.pointA.Y = this.y1; this.pointB = new Vector2(); this.pointB.X = this.x1 + this.width; this.pointB.Y = this.y1; this.pointC = new Vector2(); this.pointC.X = this.x1 + this.width; this.pointC.Y = this.y1 + this.height; this.pointD = new Vector2(); this.pointD.X = this.x1; this.pointD.Y = this.y1 + this.height; this.maxBodyCount = maxBodyCount; this.maxDepth = maxDepth; }
public void GetCollidableObjects(XboxHashSet<CollisionBody> list, ref Rectangle boundingBox) { if (children == null) { foreach (CollisionBody bodyFromList in this.items) { if (!boundingBox.Intersects(bodyFromList.AABB)) continue; list.Add(bodyFromList); } } else { float minX = boundingBox.X, minY = boundingBox.Y, maxX = boundingBox.X + boundingBox.Width, maxY = boundingBox.Y + boundingBox.Height; for (int i = 0; i < 4; i++) { bool inside = true; QuadTreeNode child = this.children[i]; if (child.x1 > maxX || child.x2 < minX || child.y1 > maxY || child.y2 < minY) inside = false; if (inside) { child.GetCollidableObjects(list, ref boundingBox); } } } }
public void GetCollidableObjects(XboxHashSet<CollisionBody> list, CollisionBody container, ref Rectangle boundingBox) { float minX = boundingBox.X, minY = boundingBox.Y, maxX = boundingBox.X + boundingBox.Width, maxY = boundingBox.Y + boundingBox.Height; this.GetCollidableObjects(list, container, minX, minY, maxX, maxY); }
public void GetCollidableObjects(XboxHashSet<CollisionBody> list, CollisionBody container) { float minX = 0, minY = 0, maxX = 0, maxY = 0; container.BuildAABB(out minX, out minY, out maxX, out maxY); this.GetCollidableObjects(list, container, minX, minY, maxX, maxY); }
public void GetCollidableObjects(XboxHashSet<CollisionBody> list, CollisionBody container, float minX, float minY, float maxX, float maxY) { if (children == null) { //if (this.containerList.Contains(container)) //{ /*foreach (CollisionBody bodyFromList in this.items) { list.Add(bodyFromList); }*/ list.MergeInto(this.items); //} } else { for (int i = 0; i < 4; i++) { bool inside = true; QuadTreeNode child = this.children[i]; if (child.x1 > maxX || child.x2 < minX || child.y1 > maxY || child.y2 < minY) inside = false; if (inside) { if (child.children == null) { /*foreach (CollisionBody bodyFromList in child.items) { list.Add(bodyFromList); }*/ list.MergeInto(child.items); } else { child.GetCollidableObjects(list, container); } } } } }
public void redeemCollisionBodySet(XboxHashSet<CollisionBody> set) { set.Clear(); this.bodySetPool.Add(set); }
public XboxHashSet<CollisionBody> obtainCollisionBodySet() { if (this.bodySetPool.Count > 0) { XboxHashSet<CollisionBody> set = this.bodySetPool[0]; bodySetPool.RemoveAt(0); return set; } else { XboxHashSet<CollisionBody> set = null; for (int i = 0; i < 10; i++) { set = new XboxHashSet<CollisionBody>(this.maxNodeCapacity + 1); if (i < 9) bodySetPool.Add(set); } return set; } }