private void DebugDrawRec(Node node, Utility.FixedRect r) { Debug.DrawLine(new Vector3(r.xMin.ToInt(), 0, r.yMin.ToInt()), new Vector3(r.xMax.ToInt(), 0, r.yMin.ToInt()), Color.white); Debug.DrawLine(new Vector3(r.xMax.ToInt(), 0, r.yMin.ToInt()), new Vector3(r.xMax.ToInt(), 0, r.yMax.ToInt()), Color.white); Debug.DrawLine(new Vector3(r.xMax.ToInt(), 0, r.yMax.ToInt()), new Vector3(r.xMin.ToInt(), 0, r.yMax.ToInt()), Color.white); Debug.DrawLine(new Vector3(r.xMin.ToInt(), 0, r.yMax.ToInt()), new Vector3(r.xMin.ToInt(), 0, r.yMin.ToInt()), Color.white); if (node.child00 != null) { // Not a leaf node var c = r.center; DebugDrawRec(node.child11, Utility.MinMaxRect(c.x, c.y, r.xMax, r.yMax)); DebugDrawRec(node.child10, Utility.MinMaxRect(c.x, r.yMin, r.xMax, c.y)); DebugDrawRec(node.child01, Utility.MinMaxRect(r.xMin, c.y, c.x, r.yMax)); DebugDrawRec(node.child00, Utility.MinMaxRect(r.xMin, r.yMin, c.x, c.y)); } var a = node.linkedList; int count = 0; while (a != null && count < LeafSize + 1) { count++; Debug.DrawLine(node.linkedList.Position.ToVector3() + Vector3.up, a.Position.ToVector3() + Vector3.up, new Color(1, 1, 0, 0.5f)); a = a.Next; } }
public void SetBounds(Utility.FixedRect r) { _bounds = r; if (_root == null) { Clear(); } _root.rect = _bounds; }
private void Distribute(Node node, Utility.FixedRect r) { var c = r.center; node.child11 = new Node { rect = Utility.MinMaxRect(c.x, c.y, r.xMax, r.yMax) }; node.child10 = new Node { rect = Utility.MinMaxRect(c.x, r.yMin, r.xMax, c.y) }; node.child01 = new Node { rect = Utility.MinMaxRect(r.xMin, c.y, c.x, r.yMax) }; node.child00 = new Node { rect = Utility.MinMaxRect(r.xMin, r.yMin, c.x, c.y) }; while (node.linkedList != null) { var nx = node.linkedList.Next; if (node.linkedList.Position.x > c.x) { if (node.linkedList.Position.z > c.y) { Add(node.child11, node.linkedList); } else { Add(node.child10, node.linkedList); } } else { if (node.linkedList.Position.z > c.y) { Add(node.child01, node.linkedList); } else { Add(node.child00, node.linkedList); } } node.linkedList = nx; } node.linkedList = null; node.count = 0; }
public override void Execute(SceneObject sender, SceneObject reciever, object data) { var _battleScene = LogicCore.SP.SceneManager.CurrentScene as BattleScene; long fw = FixedMath.Create(Width) / 100; long fh = FixedMath.Create(Height) / 100; long fr = FixedMath.Sqrt((fw / 2).Mul(fw / 2) + fh.Mul(fh)); var center = new Vector2d(0, fh / 2); var q = FixedQuaternion.LookRotation(sender.Forward, Vector3d.up); Utility.FixedRect rect = new Utility.FixedRect(center, fw, fh); Utility.List.Clear(); _battleScene.FixedQuadTree.Query(sender as IFixedAgent, fr, Utility.List); for (int i = 0; i < Utility.List.Count; i++) { if (IsTarget(Utility.List[i])) { if (Utility.PositionIsInRect(rect, sender.Position, q, Utility.List[i].Position)) { EventManager.TriggerEvent(EventId, new RuntimeData(sender, Utility.List[i] as SceneObject, null)); } } } base.Execute(sender, reciever, data); }
private long QueryRec(Node node, Vector2d p, long radius, List <IFixedAgent> fixedAgents, Utility.FixedRect r) { //找到一个子节点 if (!node.HasChild) { var a = node.linkedList; while (a != null) { if (Vector2d.SqrDistance(p, new Vector2d(a.Position.x, a.Position.z)) < radius.Mul(radius)) { fixedAgents.Add(a); } a = a.Next; } } else { //搜索子节点 var c = r.center; if (p.x - radius < c.x) { if (p.y - radius < c.y) { radius = QueryRec(node.child00, p, radius, fixedAgents, Utility.MinMaxRect(r.xMin, r.yMin, c.x, c.y)); } if (p.y + radius > c.y) { radius = QueryRec(node.child01, p, radius, fixedAgents, Utility.MinMaxRect(r.xMin, c.y, c.x, r.yMax)); } } if (p.x + radius > c.x) { if (p.y - radius < c.y) { radius = QueryRec(node.child10, p, radius, fixedAgents, Utility.MinMaxRect(c.x, r.yMin, r.xMax, c.y)); } if (p.y + radius > c.y) { radius = QueryRec(node.child11, p, radius, fixedAgents, Utility.MinMaxRect(c.x, c.y, r.xMax, r.yMax)); } } } return(radius); }