CrowdTool.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef CROWDTOOL_H
  19. #define CROWDTOOL_H
  20. #include "Sample.h"
  21. #include "DetourNavMesh.h"
  22. #include "DetourObstacleAvoidance.h"
  23. #include "ValueHistory.h"
  24. #include "DetourCrowd.h"
  25. // Tool to create crowds.
  26. struct CrowdToolParams
  27. {
  28. bool m_expandSelectedDebugDraw;
  29. bool m_showCorners;
  30. bool m_showCollisionSegments;
  31. bool m_showPath;
  32. bool m_showVO;
  33. bool m_showOpt;
  34. bool m_showNeis;
  35. bool m_expandDebugDraw;
  36. bool m_showLabels;
  37. bool m_showGrid;
  38. bool m_showNodes;
  39. bool m_showPerfGraph;
  40. bool m_showDetailAll;
  41. bool m_expandOptions;
  42. bool m_anticipateTurns;
  43. bool m_optimizeVis;
  44. bool m_optimizeTopo;
  45. bool m_obstacleAvoidance;
  46. float m_obstacleAvoidanceType;
  47. bool m_separation;
  48. float m_separationWeight;
  49. };
  50. class CrowdToolState : public SampleToolState
  51. {
  52. Sample* m_sample;
  53. dtNavMesh* m_nav;
  54. dtCrowd* m_crowd;
  55. float m_targetPos[3];
  56. dtPolyRef m_targetRef;
  57. dtCrowdAgentDebugInfo m_agentDebug;
  58. dtObstacleAvoidanceDebugData* m_vod;
  59. static const int AGENT_MAX_TRAIL = 64;
  60. static const int MAX_AGENTS = 128;
  61. struct AgentTrail
  62. {
  63. float trail[AGENT_MAX_TRAIL*3];
  64. int htrail;
  65. };
  66. AgentTrail m_trails[MAX_AGENTS];
  67. ValueHistory m_crowdTotalTime;
  68. ValueHistory m_crowdSampleCount;
  69. CrowdToolParams m_toolParams;
  70. bool m_run;
  71. public:
  72. CrowdToolState();
  73. virtual ~CrowdToolState();
  74. virtual void init(class Sample* sample);
  75. virtual void reset();
  76. virtual void handleRender();
  77. virtual void handleRenderOverlay(double* proj, double* model, int* view);
  78. virtual void handleUpdate(const float dt);
  79. inline bool isRunning() const { return m_run; }
  80. inline void setRunning(const bool s) { m_run = s; }
  81. void addAgent(const float* pos);
  82. void removeAgent(const int idx);
  83. void hilightAgent(const int idx);
  84. void updateAgentParams();
  85. int hitTestAgents(const float* s, const float* p);
  86. void setMoveTarget(const float* p, bool adjust);
  87. void updateTick(const float dt);
  88. inline CrowdToolParams* getToolParams() { return &m_toolParams; }
  89. private:
  90. // Explicitly disabled copy constructor and copy assignment operator.
  91. CrowdToolState(const CrowdToolState&);
  92. CrowdToolState& operator=(const CrowdToolState&);
  93. };
  94. class CrowdTool : public SampleTool
  95. {
  96. Sample* m_sample;
  97. CrowdToolState* m_state;
  98. enum ToolMode
  99. {
  100. TOOLMODE_CREATE,
  101. TOOLMODE_MOVE_TARGET,
  102. TOOLMODE_SELECT,
  103. TOOLMODE_TOGGLE_POLYS,
  104. };
  105. ToolMode m_mode;
  106. public:
  107. CrowdTool();
  108. virtual int type() { return TOOL_CROWD; }
  109. virtual void init(Sample* sample);
  110. virtual void reset();
  111. virtual void handleMenu();
  112. virtual void handleClick(const float* s, const float* p, bool shift);
  113. virtual void handleToggle();
  114. virtual void handleStep();
  115. virtual void handleUpdate(const float dt);
  116. virtual void handleRender();
  117. virtual void handleRenderOverlay(double* proj, double* model, int* view);
  118. };
  119. #endif // CROWDTOOL_H