DotRecastNetSim/src/DotRecast.Detour.Crowd/CrowdAgent.cs

245 lines
7.9 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
2023-03-15 17:00:29 +03:00
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
2023-03-14 08:02:43 +03:00
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System;
using System.Collections.Generic;
2023-03-28 19:52:26 +03:00
using DotRecast.Core;
2023-03-14 08:02:43 +03:00
2023-03-16 19:09:10 +03:00
namespace DotRecast.Detour.Crowd
{
2023-03-25 09:43:20 +03:00
using static DotRecast.Core.RecastMath;
2023-03-16 19:09:10 +03:00
2023-04-19 18:04:09 +03:00
/// The type of navigation mesh polygon the agent is currently traversing.
/// @ingroup crowd
public enum CrowdAgentState
{
DT_CROWDAGENT_STATE_INVALID,
/// < The agent is not in a valid state.
DT_CROWDAGENT_STATE_WALKING,
/// < The agent is traversing a normal navigation mesh polygon.
DT_CROWDAGENT_STATE_OFFMESH, /// < The agent is traversing an off-mesh connection.
};
public enum MoveRequestState
{
DT_CROWDAGENT_TARGET_NONE,
DT_CROWDAGENT_TARGET_FAILED,
DT_CROWDAGENT_TARGET_VALID,
DT_CROWDAGENT_TARGET_REQUESTING,
DT_CROWDAGENT_TARGET_WAITING_FOR_QUEUE,
DT_CROWDAGENT_TARGET_WAITING_FOR_PATH,
DT_CROWDAGENT_TARGET_VELOCITY,
};
2023-03-16 19:48:49 +03:00
/// Represents an agent managed by a #dtCrowd object.
/// @ingroup crowd
public class CrowdAgent
{
public readonly long idx;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// The type of mesh polygon the agent is traversing. (See: #CrowdAgentState)
public CrowdAgentState state;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// True if the agent has valid path (targetState == DT_CROWDAGENT_TARGET_VALID) and the path does not lead to the
/// requested position, else false.
public bool partial;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// The path corridor the agent is using.
public PathCorridor corridor;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// The local boundary data for the agent.
public LocalBoundary boundary;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// Time since the agent's path corridor was optimized.
public float topologyOptTime;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// The known neighbors of the agent.
2023-05-07 12:10:20 +03:00
public List<CrowdNeighbour> neis = new List<CrowdNeighbour>();
2023-03-16 19:48:49 +03:00
/// The desired speed.
public float desiredSpeed;
2023-03-28 18:03:33 +03:00
public Vector3f npos = new Vector3f();
2023-03-16 19:48:49 +03:00
/// < The current agent position. [(x, y, z)]
2023-03-28 18:03:33 +03:00
public Vector3f disp = new Vector3f();
2023-03-16 19:48:49 +03:00
/// < A temporary value used to accumulate agent displacement during iterative
/// collision resolution. [(x, y, z)]
2023-03-28 18:03:33 +03:00
public Vector3f dvel = new Vector3f();
2023-03-16 19:48:49 +03:00
/// < The desired velocity of the agent. Based on the current path, calculated
/// from
/// scratch each frame. [(x, y, z)]
2023-03-28 18:03:33 +03:00
public Vector3f nvel = new Vector3f();
2023-03-16 19:48:49 +03:00
/// < The desired velocity adjusted by obstacle avoidance, calculated from scratch each
/// frame. [(x, y, z)]
2023-03-28 18:03:33 +03:00
public Vector3f vel = new Vector3f();
2023-03-16 19:48:49 +03:00
/// < The actual velocity of the agent. The change from nvel -> vel is
/// constrained by max acceleration. [(x, y, z)]
/// The agent's configuration parameters.
public CrowdAgentParams option;
/// The local path corridor corners for the agent.
public List<StraightPathItem> corners = new List<StraightPathItem>();
public MoveRequestState targetState;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// < State of the movement request.
public long targetRef;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// < Target polyref of the movement request.
2023-03-28 18:03:33 +03:00
public Vector3f targetPos = new Vector3f();
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// < Target position of the movement request (or velocity in case of
/// DT_CROWDAGENT_TARGET_VELOCITY).
public PathQueryResult targetPathQueryResult;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// < Path finder query
public bool targetReplan;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// < Flag indicating that the current path is being replanned.
public float targetReplanTime;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
/// <Time since the agent's target was replanned.
public float targetReplanWaitTime;
public CrowdAgentAnimation animation;
public CrowdAgent(int idx)
{
this.idx = idx;
corridor = new PathCorridor();
boundary = new LocalBoundary();
animation = new CrowdAgentAnimation();
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
public void Integrate(float dt)
2023-03-16 19:48:49 +03:00
{
// Fake dynamic constraint.
float maxDelta = option.maxAcceleration * dt;
2023-05-05 02:44:48 +03:00
Vector3f dv = VSub(nvel, vel);
float ds = VLen(dv);
2023-03-16 19:48:49 +03:00
if (ds > maxDelta)
2023-05-05 02:44:48 +03:00
dv = VScale(dv, maxDelta / ds);
vel = VAdd(vel, dv);
2023-03-16 19:48:49 +03:00
// Integrate
2023-05-05 02:44:48 +03:00
if (VLen(vel) > 0.0001f)
npos = VMad(npos, vel, dt);
2023-03-16 19:48:49 +03:00
else
2023-04-12 17:33:15 +03:00
vel = Vector3f.Zero;
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
public bool OverOffmeshConnection(float radius)
2023-03-16 19:48:49 +03:00
{
if (0 == corners.Count)
return false;
2023-05-05 02:44:48 +03:00
bool offMeshConnection = ((corners[corners.Count - 1].GetFlags()
2023-03-16 19:48:49 +03:00
& NavMeshQuery.DT_STRAIGHTPATH_OFFMESH_CONNECTION) != 0)
? true
: false;
if (offMeshConnection)
{
2023-05-05 02:44:48 +03:00
float distSq = VDist2DSqr(npos, corners[corners.Count - 1].GetPos());
2023-03-16 19:48:49 +03:00
if (distSq < radius * radius)
return true;
}
return false;
2023-03-14 08:02:43 +03:00
}
2023-05-05 02:44:48 +03:00
public float GetDistanceToGoal(float range)
2023-03-16 19:48:49 +03:00
{
if (0 == corners.Count)
return range;
2023-05-05 02:44:48 +03:00
bool endOfPath = ((corners[corners.Count - 1].GetFlags() & NavMeshQuery.DT_STRAIGHTPATH_END) != 0) ? true : false;
2023-03-16 19:48:49 +03:00
if (endOfPath)
2023-05-05 02:44:48 +03:00
return Math.Min(VDist2D(npos, corners[corners.Count - 1].GetPos()), range);
2023-03-16 19:48:49 +03:00
return range;
}
2023-05-05 02:44:48 +03:00
public Vector3f CalcSmoothSteerDirection()
2023-03-16 19:48:49 +03:00
{
2023-03-28 18:03:33 +03:00
Vector3f dir = new Vector3f();
2023-03-16 19:48:49 +03:00
if (0 < corners.Count)
{
int ip0 = 0;
int ip1 = Math.Min(1, corners.Count - 1);
2023-05-05 02:44:48 +03:00
var p0 = corners[ip0].GetPos();
var p1 = corners[ip1].GetPos();
2023-03-16 19:48:49 +03:00
2023-05-05 02:44:48 +03:00
var dir0 = VSub(p0, npos);
var dir1 = VSub(p1, npos);
2023-04-29 07:06:21 +03:00
dir0.y = 0;
dir1.y = 0;
2023-03-16 19:48:49 +03:00
2023-05-05 02:44:48 +03:00
float len0 = VLen(dir0);
float len1 = VLen(dir1);
2023-03-16 19:48:49 +03:00
if (len1 > 0.001f)
2023-05-05 02:44:48 +03:00
dir1 = VScale(dir1, 1.0f / len1);
2023-03-16 19:48:49 +03:00
2023-04-29 07:06:21 +03:00
dir.x = dir0.x - dir1.x * len0 * 0.5f;
dir.y = 0;
dir.z = dir0.z - dir1.z * len0 * 0.5f;
2023-03-16 19:48:49 +03:00
2023-05-05 02:44:48 +03:00
VNormalize(ref dir);
2023-03-16 19:48:49 +03:00
}
return dir;
}
2023-05-05 02:44:48 +03:00
public Vector3f CalcStraightSteerDirection()
2023-03-16 19:48:49 +03:00
{
2023-03-28 18:03:33 +03:00
Vector3f dir = new Vector3f();
2023-03-16 19:48:49 +03:00
if (0 < corners.Count)
{
2023-05-05 02:44:48 +03:00
dir = VSub(corners[0].GetPos(), npos);
2023-04-29 07:06:21 +03:00
dir.y = 0;
2023-05-05 02:44:48 +03:00
VNormalize(ref dir);
2023-03-16 19:48:49 +03:00
}
return dir;
}
2023-05-05 02:44:48 +03:00
public void SetTarget(long refs, Vector3f pos)
2023-03-16 19:48:49 +03:00
{
targetRef = refs;
2023-04-12 17:53:28 +03:00
targetPos = pos;
2023-03-16 19:48:49 +03:00
targetPathQueryResult = null;
if (targetRef != 0)
{
targetState = MoveRequestState.DT_CROWDAGENT_TARGET_REQUESTING;
}
else
{
targetState = MoveRequestState.DT_CROWDAGENT_TARGET_FAILED;
}
}
}
2023-04-29 07:06:21 +03:00
}