remove class SteerTarget

This commit is contained in:
ikpil 2023-07-25 23:59:51 +09:00
parent 7f10d5814d
commit 336ba6b132
3 changed files with 26 additions and 54 deletions

View File

@ -28,24 +28,21 @@ namespace DotRecast.Detour
{ {
private const int MAX_STEER_POINTS = 3; private const int MAX_STEER_POINTS = 3;
public static bool GetSteerTarget(DtNavMeshQuery navQuery, RcVec3f startPos, RcVec3f endPos,
public static SteerTarget GetSteerTarget(DtNavMeshQuery navQuery, RcVec3f startPos, RcVec3f endPos, float minTargetDist,
float minTargetDist, List<long> path) List<long> path,
out RcVec3f steerPos, out int steerPosFlag, out long steerPosRef)
{ {
steerPos = RcVec3f.Zero;
steerPosFlag = 0;
steerPosRef = 0;
// Find steer target. // Find steer target.
var straightPath = new List<StraightPathItem>(); var straightPath = new List<StraightPathItem>();
var result = navQuery.FindStraightPath(startPos, endPos, path, ref straightPath, MAX_STEER_POINTS, 0); var result = navQuery.FindStraightPath(startPos, endPos, path, ref straightPath, MAX_STEER_POINTS, 0);
if (result.Failed()) if (result.Failed())
{ {
return null; return false;
}
float[] steerPoints = new float[straightPath.Count * 3];
for (int i = 0; i < straightPath.Count; i++)
{
steerPoints[i * 3] = straightPath[i].GetPos().x;
steerPoints[i * 3 + 1] = straightPath[i].GetPos().y;
steerPoints[i * 3 + 2] = straightPath[i].GetPos().z;
} }
// Find vertex far enough to steer to. // Find vertex far enough to steer to.
@ -53,26 +50,22 @@ namespace DotRecast.Detour
while (ns < straightPath.Count) while (ns < straightPath.Count)
{ {
// Stop at Off-Mesh link or when point is further than slop away. // Stop at Off-Mesh link or when point is further than slop away.
if (((straightPath[ns].GetFlags() & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0) if (((straightPath[ns].flags & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0)
|| !InRange(straightPath[ns].GetPos(), startPos, minTargetDist, 1000.0f)) || !InRange(straightPath[ns].pos, startPos, minTargetDist, 1000.0f))
break; break;
ns++; ns++;
} }
// Failed to find good point to steer to. // Failed to find good point to steer to.
if (ns >= straightPath.Count) if (ns >= straightPath.Count)
return null; return false;
RcVec3f steerPos = RcVec3f.Of( steerPos = straightPath[ns].pos;
straightPath[ns].GetPos().x, steerPos.y = startPos.y;
startPos.y, steerPosFlag = straightPath[ns].flags;
straightPath[ns].GetPos().z steerPosRef = straightPath[ns].refs;
);
int steerPosFlag = straightPath[ns].GetFlags();
long steerPosRef = straightPath[ns].GetRef();
SteerTarget target = new SteerTarget(steerPos, steerPosFlag, steerPosRef, steerPoints); return true;
return target;
} }
public static bool InRange(RcVec3f v1, RcVec3f v2, float r, float h) public static bool InRange(RcVec3f v1, RcVec3f v2, float r, float h)

View File

@ -1,20 +0,0 @@
using DotRecast.Core;
namespace DotRecast.Detour
{
public class SteerTarget
{
public readonly RcVec3f steerPos;
public readonly int steerPosFlag;
public readonly long steerPosRef;
public readonly float[] steerPoints;
public SteerTarget(RcVec3f steerPos, int steerPosFlag, long steerPosRef, float[] steerPoints)
{
this.steerPos = steerPos;
this.steerPosFlag = steerPosFlag;
this.steerPosRef = steerPosRef;
this.steerPoints = steerPoints;
}
}
}

View File

@ -67,22 +67,21 @@ namespace DotRecast.Recast.DemoTool.Tools
while (0 < polys.Count && smoothPath.Count < MAX_SMOOTH) while (0 < polys.Count && smoothPath.Count < MAX_SMOOTH)
{ {
// Find location to steer towards. // Find location to steer towards.
SteerTarget steerTarget = PathUtils.GetSteerTarget(navQuery, iterPos, targetPos, SLOP, polys); if (!PathUtils.GetSteerTarget(navQuery, iterPos, targetPos, SLOP,
if (null == steerTarget) polys, out var steerPos, out var steerPosFlag, out var steerPosRef))
{ {
break; break;
} }
bool endOfPath = (steerTarget.steerPosFlag & DtNavMeshQuery.DT_STRAIGHTPATH_END) != 0 bool endOfPath = (steerPosFlag & DtNavMeshQuery.DT_STRAIGHTPATH_END) != 0
? true ? true
: false; : false;
bool offMeshConnection = (steerTarget.steerPosFlag bool offMeshConnection = (steerPosFlag & DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0
& DtNavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0
? true ? true
: false; : false;
// Find movement delta. // Find movement delta.
RcVec3f delta = steerTarget.steerPos.Subtract(iterPos); RcVec3f delta = steerPos.Subtract(iterPos);
float len = (float)Math.Sqrt(RcVec3f.Dot(delta, delta)); float len = (float)Math.Sqrt(RcVec3f.Dot(delta, delta));
// If the steer target is end of path or off-mesh link, do not move past the location. // If the steer target is end of path or off-mesh link, do not move past the location.
if ((endOfPath || offMeshConnection) && len < STEP_SIZE) if ((endOfPath || offMeshConnection) && len < STEP_SIZE)
@ -111,7 +110,7 @@ namespace DotRecast.Recast.DemoTool.Tools
} }
// Handle end of path and off-mesh links when close enough. // Handle end of path and off-mesh links when close enough.
if (endOfPath && PathUtils.InRange(iterPos, steerTarget.steerPos, SLOP, 1.0f)) if (endOfPath && PathUtils.InRange(iterPos, steerPos, SLOP, 1.0f))
{ {
// Reached end of path. // Reached end of path.
iterPos = targetPos; iterPos = targetPos;
@ -122,7 +121,7 @@ namespace DotRecast.Recast.DemoTool.Tools
break; break;
} }
else if (offMeshConnection && PathUtils.InRange(iterPos, steerTarget.steerPos, SLOP, 1.0f)) else if (offMeshConnection && PathUtils.InRange(iterPos, steerPos, SLOP, 1.0f))
{ {
// Reached off-mesh connection. // Reached off-mesh connection.
RcVec3f startPos = RcVec3f.Zero; RcVec3f startPos = RcVec3f.Zero;
@ -132,7 +131,7 @@ namespace DotRecast.Recast.DemoTool.Tools
long prevRef = 0; long prevRef = 0;
long polyRef = polys[0]; long polyRef = polys[0];
int npos = 0; int npos = 0;
while (npos < polys.Count && polyRef != steerTarget.steerPosRef) while (npos < polys.Count && polyRef != steerPosRef)
{ {
prevRef = polyRef; prevRef = polyRef;
polyRef = polys[npos]; polyRef = polys[npos];
@ -311,7 +310,7 @@ namespace DotRecast.Recast.DemoTool.Tools
{ {
var status = navQuery.FindRandomPointAroundCircle(startRef, spos, dist, filter, frand, constraint, var status = navQuery.FindRandomPointAroundCircle(startRef, spos, dist, filter, frand, constraint,
out var randomRef, out var randomPt); out var randomRef, out var randomPt);
if (status.Succeeded()) if (status.Succeeded())
{ {
points.Add(randomPt); points.Add(randomPt);