private static void SearchRecursive(PathFinderAgent agent, Bounds2D targetBounds, Vector3 targetPos, float targetSqrDist, int targetBranch) { kDTreeBranch branch = branches[targetBranch]; if (branch.bounds.Overlap(targetBounds)) { if (branch.branchA != -1) { SearchRecursive(agent, targetBounds, targetPos, targetSqrDist, branch.branchA); SearchRecursive(agent, targetBounds, targetPos, targetSqrDist, branch.branchB); } else { for (int i = branch.start; i < branch.end; i++) { var realAgent = PathFinder.agents[agents[i].index]; if (agent != realAgent) { float curSqrDist = SomeMath.SqrDistance(targetPos, realAgent.positionVector3); if (curSqrDist < targetSqrDist) { if (agent.neighbourAgents.Count < agent.maxNeighbors) { agent.neighbourAgents.Add(realAgent); agent.neighbourSqrDistances.Add(curSqrDist); } else { for (int n = 0; n < agent.maxNeighbors; n++) { if (agent.neighbourSqrDistances[n] > curSqrDist) { agent.neighbourAgents[n] = realAgent; agent.neighbourSqrDistances[n] = curSqrDist; break; } } } } } } } } }
private void SearchRecursive(T target, Bounds2D targetBounds, Vector2 targetPos, float targetSqrDist, int targetBranch) { kDTreeBranch branch = branches[targetBranch]; if (branch.bounds.Overlap(targetBounds)) { if (branch.branchA != -1) { SearchRecursive(target, targetBounds, targetPos, targetSqrDist, branch.branchA); SearchRecursive(target, targetBounds, targetPos, targetSqrDist, branch.branchB); } else { for (int i = branch.start; i < branch.end; i++) { T realAgent = data[members[i].index]; if (target.Equals(realAgent) == false) { float curSqrDist = SomeMath.SqrDistance(targetPos, realAgent.position); if (curSqrDist < targetSqrDist) { if (target.neighbourAgents.Count < target.maxNeighbours) { target.neighbourAgents.Add(realAgent); target.neighbourSqrDistances.Add(curSqrDist); } else { for (int n = 0; n < target.maxNeighbours; n++) { if (target.neighbourSqrDistances[n] > curSqrDist) { target.neighbourAgents[n] = realAgent; target.neighbourSqrDistances[n] = curSqrDist; break; } } } } } } } } }