public void LoadOb(string path) { bool ret = RecastHelper.load_ob_bin(path); if (!ret) { Debug.LogWarning("load map bin failed"); return; } int obCount = RecastHelper.get_ob_box_count(); }
Vector2 moveToNext(Vector2 vCurr, float moveDist) { Vector2 pos; Vector2 vDest = new Vector2(pathlist[0].x, pathlist[0].z); Vector2 v = vDest - vCurr; Vector2 vDir = v.normalized; if (moveDist <= v.magnitude) { ///移动距离小于当前路点至下一个路点的路程 pos = vCurr + moveDist * vDir; return(pos); } else { ///移动距离大于当前路点至下一个路点的路程 float[] start = new float[3] { vCurr.x, 0, vCurr.y }; float[] end = new float[3] { vDest.x, 0, vDest.y }; float[] hit = new float[3]; float[] hitNormal = new float[3]; bool bHit = RecastHelper.raycast(start, end, hit, hitNormal); if (bHit) { ///有阻挡 return(new Vector2(hit[0], hit[2])); } else { ///无阻挡 ///以当前目标路点为起始点开始 moveDist -= v.magnitude; vCurr = vDest; pathlist.RemoveAt(0); if (pathlist.Count > 0) { return(moveToNext(vDest, moveDist)); } else { return(vCurr); } } } }
public void LoadMap(string path) { m_ret = RecastHelper.load_map_bin(path); if (!m_ret) { Debug.LogWarning("load map bin failed"); return; } CreateDbgRenderMesh(); ///1 tri => 3 verts, 1 verts = 1 pos(vec3) => 3 float int vertCount = RecastHelper.get_mesh_vert_count(); float[] vertPos = new float[vertCount * 3]; RecastHelper.get_mesh_vert_pos(vertPos); Color col = new Color(); for (int i = 0; i < vertCount * 3; i += 9) { Vector3 a = new Vector3(vertPos[i + 0], vertPos[i + 1], vertPos[i + 2]); Vector3 b = new Vector3(vertPos[i + 3], vertPos[i + 4], vertPos[i + 5]); Vector3 c = new Vector3(vertPos[i + 6], vertPos[i + 7], vertPos[i + 8]); m_dbgRenderMesh.AddTriangle(new DbgRenderTriangle(a, b, c, col)); Vector3 triCenter = (a + b + c) / 3; float[] pos = new float[3]; pos[0] = triCenter.x; pos[1] = triCenter.y; pos[2] = triCenter.z; if (RecastHelper.is_valid_pos(pos)) { bornList.Add(triCenter); } } m_dbgRenderMesh.Rebuild(); BuildPathMap(); }
void calcTargetPos() { if (targetList.Count < 1) { return; } int index = Random.Range(0, targetList.Count); float[] sPos = new float[3]; sPos[0] = this.transform.position.x; sPos[1] = this.transform.position.y; sPos[2] = this.transform.position.z; float[] ePos = new float[3]; ePos[0] = targetList[index].x; ePos[1] = targetList[index].y; ePos[2] = targetList[index].z; int pathCount; int max_path_count = 256 * 3; float[] path = new float[max_path_count]; RecastHelper.find_path(sPos, ePos, path, out pathCount, out m_distace); recycle(); for (int i = 0; i < pathCount * 3; i += 3) { Vector3 point = new Vector3(path[i], path[i + 1], path[i + 2]); pathlist.Add(point); getObject(pathObjects.Count, point); } getObject(pathObjects.Count, transform.position, 1); getObject(pathObjects.Count, targetList[index], 2); }