public new bool SnappedLinecast (Vector3 _a, Vector3 _b, GraphNode hint, out GraphHitInfo hit) {
hit = new GraphHitInfo ();
//System.DateTime startTime = System.DateTime.UtcNow;
LevelGridNode n1 = GetNearest (_a,NNConstraint.None).node as LevelGridNode;
LevelGridNode n2 = GetNearest (_b,NNConstraint.None).node as LevelGridNode;
if (n1 == null || n2 == null) {
hit.node = null;
hit.point = _a;
return true;
}
_a = inverseMatrix.MultiplyPoint3x4 ((Vector3)n1.position);
_a.x -= 0.5F;
_a.z -= 0.5F;
_b = inverseMatrix.MultiplyPoint3x4 ((Vector3)n2.position);
_b.x -= 0.5F;
_b.z -= 0.5F;
Int3 a = new Int3 (Mathf.RoundToInt (_a.x),Mathf.RoundToInt (_a.y),Mathf.RoundToInt (_a.z));
Int3 b = new Int3 (Mathf.RoundToInt (_b.x),Mathf.RoundToInt (_b.y),Mathf.RoundToInt (_b.z));
hit.origin = (Vector3)a;
//Debug.DrawLine (matrix.MultiplyPoint3x4 (a*100),matrix.MultiplyPoint3x4 (b*100),Color.yellow);
if (!n1.Walkable) {//nodes[a.z*width+a.x].walkable) {
hit.node = n1;//nodes[a.z*width+a.x];
hit.point = matrix.MultiplyPoint3x4 (new Vector3 (a.x+0.5F,0,a.z+0.5F));
hit.point.y = ((Vector3)hit.node.position).y;
return true;
}
int dx = Mathf.Abs (a.x-b.x);
int dz = Mathf.Abs (a.z-b.z);
LevelGridNode currentNode = n1;
while (true) {
if (currentNode == n2) { //a.x == b.x && a.z == b.z) {
//System.DateTime endTime2 = System.DateTime.UtcNow;
//float theTime2 = (endTime2-startTime).Ticks*0.0001F;
//Debug.Log ("Grid Linecast : Time "+theTime2.ToString ("0.00"));
return false;
}
//The nodes are at the same position in the graph when seen from above
if (currentNode.NodeInGridIndex == n2.NodeInGridIndex) {
hit.node = currentNode;
hit.point = (Vector3)currentNode.position;
return true;
}
dx = System.Math.Abs(a.x-b.x);
dz = System.Math.Abs(a.z-b.z);
int dir = 0;
if (dx >= dz) {
dir = b.x>a.x ? 1 : 3;
} else if (dz > dx) {
dir = b.z>a.z ? 2 : 0;
}
if (CheckConnection (currentNode,dir)) {
LevelGridNode other = nodes[currentNode.NodeInGridIndex+neighbourOffsets[dir] + width*depth*currentNode.GetConnectionValue(dir)] as LevelGridNode;
if (!other.Walkable) {
hit.node = other;
hit.point = (Vector3)other.position;
return true;
}
//Debug.DrawLine (matrix.MultiplyPoint3x4 (a*100),matrix.MultiplyPoint3x4 (newPos*100));
a = (Int3)inverseMatrix.MultiplyPoint3x4 ((Vector3)other.position);
currentNode = other;
} else {
hit.node = currentNode;
hit.point = (Vector3)currentNode.position;//matrix.MultiplyPoint3x4 (new Vector3 (a.x+0.5F,0,a.z+0.5F));
return true;
}
/*int e2 = err*2;
Int3 newPos = a;
if (e2 > -dz) {
err = err-dz;
dir = sx;
newPos.x += sx;
}
if (e2 < dx) {
err = err+dx;
dir += width*sz;
newPos.z += sz;
}
if (dir == 0) {
Debug.LogError ("Offset is zero, this should not happen");
return false;
}
for (int i=0;i<neighbourOffsets.Length;i++) {
if (neighbourOffsets[i] == dir) {
if (CheckConnection (nodes[a.z*width+a.x] as LevelGridNode,i)) {
if (!nodes[newPos.z*width+newPos.x].walkable) {
hit.node = nodes[a.z*width+a.x];
hit.point = matrix.MultiplyPoint3x4 (new Vector3 (a.x+0.5F,0,a.z+0.5F));
hit.point.y = ((Vector3)hit.node.position).y;
return true;
}
//Debug.DrawLine (matrix.MultiplyPoint3x4 (a*100),matrix.MultiplyPoint3x4 (newPos*100));
a = newPos;
break;
} else {
hit.node = nodes[a.z*width+a.x];
hit.point = matrix.MultiplyPoint3x4 (new Vector3 (a.x+0.5F,0,a.z+0.5F));
hit.point.y = ((Vector3)hit.node.position).y;
return true;
}
}
}*/
}
//Debug.DrawLine (_a,_b,Color.green);
//hit.success = true;
}