Planeshift

DetourCrowd.h

Go to the documentation of this file.
00001 //
00002 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
00003 //
00004 // This software is provided 'as-is', without any express or implied
00005 // warranty.  In no event will the authors be held liable for any damages
00006 // arising from the use of this software.
00007 // Permission is granted to anyone to use this software for any purpose,
00008 // including commercial applications, and to alter it and redistribute it
00009 // freely, subject to the following restrictions:
00010 // 1. The origin of this software must not be misrepresented; you must not
00011 //    claim that you wrote the original software. If you use this software
00012 //    in a product, an acknowledgment in the product documentation would be
00013 //    appreciated but is not required.
00014 // 2. Altered source versions must be plainly marked as such, and must not be
00015 //    misrepresented as being the original software.
00016 // 3. This notice may not be removed or altered from any source distribution.
00017 //
00018 
00019 #ifndef DETOURCROWD_H
00020 #define DETOURCROWD_H
00021 
00022 #include "DetourNavMeshQuery.h"
00023 #include "DetourObstacleAvoidance.h"
00024 #include "DetourLocalBoundary.h"
00025 #include "DetourPathCorridor.h"
00026 #include "DetourProximityGrid.h"
00027 #include "DetourPathQueue.h"
00028 
00032 static const int DT_CROWDAGENT_MAX_NEIGHBOURS = 6;
00033 
00039 static const int DT_CROWDAGENT_MAX_CORNERS = 4;
00040 
00046 static const int DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS = 8;
00047 
00051 struct dtCrowdNeighbour
00052 {
00053         int idx;                
00054         float dist;             
00055 };
00056 
00059 enum CrowdAgentState
00060 {
00061         DT_CROWDAGENT_STATE_INVALID,            
00062         DT_CROWDAGENT_STATE_WALKING,            
00063         DT_CROWDAGENT_STATE_OFFMESH,            
00064 };
00065 
00068 struct dtCrowdAgentParams
00069 {
00070         float radius;                                           
00071         float height;                                           
00072         float maxAcceleration;                          
00073         float maxSpeed;                                         
00074 
00076         float collisionQueryRange;
00077 
00078         float pathOptimizationRange;            
00079 
00081         float separationWeight;
00082 
00084         unsigned char updateFlags;
00085 
00088         unsigned char obstacleAvoidanceType;    
00089 
00091         void* userData;
00092 };
00093 
00094 enum MoveRequestState
00095 {
00096         DT_CROWDAGENT_TARGET_NONE = 0,
00097         DT_CROWDAGENT_TARGET_FAILED,
00098         DT_CROWDAGENT_TARGET_VALID,
00099         DT_CROWDAGENT_TARGET_REQUESTING,
00100         DT_CROWDAGENT_TARGET_WAITING_FOR_QUEUE,
00101         DT_CROWDAGENT_TARGET_WAITING_FOR_PATH,
00102         DT_CROWDAGENT_TARGET_VELOCITY,
00103 };
00104 
00107 struct dtCrowdAgent
00108 {
00110         unsigned char active;
00111 
00113         unsigned char state;
00114 
00116         dtPathCorridor corridor;
00117 
00119         dtLocalBoundary boundary;
00120         
00122         float topologyOptTime;
00123         
00125         dtCrowdNeighbour neis[DT_CROWDAGENT_MAX_NEIGHBOURS];
00126 
00128         int nneis;
00129         
00131         float desiredSpeed;
00132 
00133         float npos[3];          
00134         float disp[3];
00135         float dvel[3];          
00136         float nvel[3];
00137         float vel[3];           
00138 
00140         dtCrowdAgentParams params;
00141 
00143         float cornerVerts[DT_CROWDAGENT_MAX_CORNERS*3];
00144 
00146         unsigned char cornerFlags[DT_CROWDAGENT_MAX_CORNERS];
00147 
00149         dtPolyRef cornerPolys[DT_CROWDAGENT_MAX_CORNERS];
00150 
00152         int ncorners;
00153         
00154         unsigned char targetState;                      
00155         dtPolyRef targetRef;                            
00156         float targetPos[3];                                     
00157         dtPathQueueRef targetPathqRef;          
00158         bool targetReplan;                                      
00159         float targetReplanTime;                         
00160 };
00161 
00162 struct dtCrowdAgentAnimation
00163 {
00164         unsigned char active;
00165         float initPos[3], startPos[3], endPos[3];
00166         dtPolyRef polyRef;
00167         float t, tmax;
00168 };
00169 
00173 enum UpdateFlags
00174 {
00175         DT_CROWD_ANTICIPATE_TURNS = 1,
00176         DT_CROWD_OBSTACLE_AVOIDANCE = 2,
00177         DT_CROWD_SEPARATION = 4,
00178         DT_CROWD_OPTIMIZE_VIS = 8,                      
00179         DT_CROWD_OPTIMIZE_TOPO = 16,            
00180 };
00181 
00182 struct dtCrowdAgentDebugInfo
00183 {
00184         int idx;
00185         float optStart[3], optEnd[3];
00186         dtObstacleAvoidanceDebugData* vod;
00187 };
00188 
00191 class dtCrowd
00192 {
00193         int m_maxAgents;
00194         dtCrowdAgent* m_agents;
00195         dtCrowdAgent** m_activeAgents;
00196         dtCrowdAgentAnimation* m_agentAnims;
00197         
00198         dtPathQueue m_pathq;
00199 
00200         dtObstacleAvoidanceParams m_obstacleQueryParams[DT_CROWD_MAX_OBSTAVOIDANCE_PARAMS];
00201         dtObstacleAvoidanceQuery* m_obstacleQuery;
00202         
00203         dtProximityGrid* m_grid;
00204         
00205         dtPolyRef* m_pathResult;
00206         int m_maxPathResult;
00207         
00208         float m_ext[3];
00209         dtQueryFilter m_filter;
00210         
00211         float m_maxAgentRadius;
00212 
00213         int m_velocitySampleCount;
00214 
00215         dtNavMeshQuery* m_navquery;
00216 
00217         void updateTopologyOptimization(dtCrowdAgent** agents, const int nagents, const float dt);
00218         void updateMoveRequest(const float dt);
00219         void checkPathValidity(dtCrowdAgent** agents, const int nagents, const float dt);
00220 
00221         inline int getAgentIndex(const dtCrowdAgent* agent) const  { return agent - m_agents; }
00222 
00223         bool requestMoveTargetReplan(const int idx, dtPolyRef ref, const float* pos);
00224 
00225         void purge();
00226         
00227 public:
00228         dtCrowd();
00229         ~dtCrowd();
00230         
00236         bool init(const int maxAgents, const float maxAgentRadius, dtNavMesh* nav);
00237         
00241         void setObstacleAvoidanceParams(const int idx, const dtObstacleAvoidanceParams* params);
00242 
00247         const dtObstacleAvoidanceParams* getObstacleAvoidanceParams(const int idx) const;
00248         
00252         const dtCrowdAgent* getAgent(const int idx);
00253 
00256         const int getAgentCount() const;
00257         
00262         int addAgent(const float* pos, const dtCrowdAgentParams* params);
00263 
00267         void updateAgentParameters(const int idx, const dtCrowdAgentParams* params);
00268 
00271         void removeAgent(const int idx);
00272         
00278         bool requestMoveTarget(const int idx, dtPolyRef ref, const float* pos);
00279 
00284         bool requestMoveVelocity(const int idx, const float* vel);
00285 
00289         bool resetMoveTarget(const int idx);
00290 
00295         int getActiveAgents(dtCrowdAgent** agents, const int maxAgents);
00296 
00300         void update(const float dt, dtCrowdAgentDebugInfo* debug);
00301         
00304         const dtQueryFilter* getFilter() const { return &m_filter; }
00305 
00308         dtQueryFilter* getEditableFilter() { return &m_filter; }
00309 
00312         const float* getQueryExtents() const { return m_ext; }
00313         
00316         inline int getVelocitySampleCount() const { return m_velocitySampleCount; }
00317         
00320         const dtProximityGrid* getGrid() const { return m_grid; }
00321 
00324         const dtPathQueue* getPathQueue() const { return &m_pathq; }
00325 
00327         const dtNavMeshQuery* getNavMeshQuery() const { return m_navquery; }
00328 };
00329 
00333 dtCrowd* dtAllocCrowd();
00334 
00338 void dtFreeCrowd(dtCrowd* ptr);
00339 
00340 
00341 #endif // DETOURCROWD_H
00342 
00344 
00345 // This section contains detailed documentation for members that don't have
00346 // a source file. It reduces clutter in the main section of the header.
00347