public Link(Vector2d16 dimensions, Vector2d16 position, int depth) { //Console.WriteLine(position); dimensions /= 2; this.position = position; if (depth-- <= 0) { _1 = new Leaf(); _2 = new Leaf(); _3 = new Leaf(); _4 = new Leaf(); } else { _1 = new Link(dimensions, new Vector2d16(position._1 - dimensions._1 / 2, position._2 - dimensions._2 / 2), depth); _2 = new Link(dimensions, new Vector2d16(position._1 + dimensions._1 / 2, position._2 - dimensions._2 / 2), depth); _3 = new Link(dimensions, new Vector2d16(position._1 + dimensions._1 / 2, position._2 + dimensions._2 / 2), depth); _4 = new Link(dimensions, new Vector2d16(position._1 - dimensions._1 / 2, position._2 + dimensions._2 / 2), depth); /*_1 = new Link(dimensions / 2, new Vector2d16(position._1 + dimensions._1 / 4, position._2 + dimensions._2 * 3 / 4), depth); * _2 = new Link(dimensions / 2, new Vector2d16(position._1 + dimensions._1 * 3/ 4, position._2 + dimensions._2 * 3 / 4), depth); * _3 = new Link(dimensions / 2, new Vector2d16(position._1 + dimensions._1 / 4, position._2 + dimensions._2 / 4), depth); * _4 = new Link(dimensions / 2, new Vector2d16(position._1 + dimensions._1 * 3 / 4, position._2 + dimensions._2 / 4), depth);*/ } }
public override void Add(GameObject obj) { Vector2d16 bb = new Vector2d16(1, 1); if ((obj.Position._1 - position._1 < bb._1 >> 1) || (obj.Position._2 - position._2 < bb._2 >> 1)) { AddToArray(obj); //Console.WriteLine(position); return; } if (obj.Position._1 < position._1) { if (obj.Position._2 > position._2) { _1.Add(obj); } else { _3.Add(obj); } } else if (obj.Position._2 > position._2) { _2.Add(obj); } else { _4.Add(obj); } }
protected void GetLocalVisuals(List <IRenderable> result) { //Console.WriteLine(objects.ToString()); if (objects != null) { foreach (GameObject gameObj in objects) { if (gameObj is IRenderable) { IRenderable renderable = gameObj as IRenderable; Vector2d16 aMin = (gameObj).Position - (renderable.GetVisualBB()); Vector2d16 aMax = (gameObj).Position + (renderable.GetVisualBB()); //Vector2d16 bMin = Renderer.worldPosition - Renderer.Dimensions * 0.5; //Vector2d16 bMax = Renderer.worldPosition + Renderer.Dimensions * 0.5; Vector2d16 bMin = Renderer.worldPosition; Vector2d16 bMax = Renderer.worldPosition + Renderer.Dimensions; if ( (aMin._1 <= bMax._1 & aMax._1 >= bMin._1) && (aMin._2 <= bMax._2 & aMax._2 >= bMin._2) ) { result.Add(renderable); } } } } }
public QuadTree(Vector2d16 dimensions, int depth) { this.dimensions = dimensions; if (depth == 0) { root = new Leaf(); return; } root = new Link(dimensions, dimensions / 2, depth); }
private static bool Collide(Rectangle rectangle1, Vector2d16 pos1, Rectangle rectangle2, Vector2d16 pos2) { Vector2d16 aMin = pos1; Vector2d16 aMax = pos1 + rectangle1.dimensions; Vector2d16 bMin = pos2; Vector2d16 bMax = pos2 + rectangle2.dimensions; if ( (aMin._1 <= bMax._1 & aMax._1 >= bMin._1) && (aMin._2 <= bMax._2 & aMax._2 >= bMin._2) ) { return(true); } return(false); }