CrowdNavigation.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Urho3D.h>
  23. #include <Urho3D/Graphics/AnimatedModel.h>
  24. #include <Urho3D/Graphics/Camera.h>
  25. #include <Urho3D/Core/CoreEvents.h>
  26. #include <Urho3D/UI/Cursor.h>
  27. #include <Urho3D/Graphics/DebugRenderer.h>
  28. #include <Urho3D/Engine/Engine.h>
  29. #include <Urho3D/UI/Font.h>
  30. #include <Urho3D/Graphics/Graphics.h>
  31. #include <Urho3D/Input/Input.h>
  32. #include <Urho3D/Graphics/Light.h>
  33. #include <Urho3D/Graphics/Material.h>
  34. #include <Urho3D/Math/MathDefs.h>
  35. #include <Urho3D/Graphics/Model.h>
  36. #include <Urho3D/Navigation/CrowdAgent.h>
  37. #include <Urho3D/Navigation/DetourCrowdManager.h>
  38. #include <Urho3D/Navigation/DynamicNavigationMesh.h>
  39. #include <Urho3D/Navigation/Navigable.h>
  40. #include <Urho3D/Navigation/NavigationEvents.h>
  41. #include <Urho3D/Navigation/Obstacle.h>
  42. #include <Urho3D/Graphics/Octree.h>
  43. #include <Urho3D/Graphics/Renderer.h>
  44. #include <Urho3D/Resource/ResourceCache.h>
  45. #include <Urho3D/Scene/Scene.h>
  46. #include <Urho3D/Graphics/StaticModel.h>
  47. #include <Urho3D/UI/Text.h>
  48. #include <Urho3D/UI/UI.h>
  49. #include <Urho3D/Resource/XMLFile.h>
  50. #include <Urho3D/Graphics/Zone.h>
  51. #include "CrowdNavigation.h"
  52. #include <Urho3D/DebugNew.h>
  53. DEFINE_APPLICATION_MAIN(CrowdNavigation)
  54. CrowdNavigation::CrowdNavigation(Context* context) :
  55. Sample(context),
  56. drawDebug_(false)
  57. {
  58. }
  59. void CrowdNavigation::Start()
  60. {
  61. // Execute base class startup
  62. Sample::Start();
  63. // Create the scene content
  64. CreateScene();
  65. // Create the UI content
  66. CreateUI();
  67. // Setup the viewport for displaying the scene
  68. SetupViewport();
  69. // Hook up to the frame update and render post-update events
  70. SubscribeToEvents();
  71. }
  72. void CrowdNavigation::CreateScene()
  73. {
  74. ResourceCache* cache = GetSubsystem<ResourceCache>();
  75. scene_ = new Scene(context_);
  76. // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  77. // Also create a DebugRenderer component so that we can draw debug geometry
  78. scene_->CreateComponent<Octree>();
  79. scene_->CreateComponent<DebugRenderer>();
  80. // Create scene node & StaticModel component for showing a static plane
  81. Node* planeNode = scene_->CreateChild("Plane");
  82. planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f));
  83. StaticModel* planeObject = planeNode->CreateComponent<StaticModel>();
  84. planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl"));
  85. planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));
  86. // Create a Zone component for ambient lighting & fog control
  87. Node* zoneNode = scene_->CreateChild("Zone");
  88. Zone* zone = zoneNode->CreateComponent<Zone>();
  89. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  90. zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f));
  91. zone->SetFogColor(Color(0.5f, 0.5f, 0.7f));
  92. zone->SetFogStart(100.0f);
  93. zone->SetFogEnd(300.0f);
  94. // Create a directional light to the world. Enable cascaded shadows on it
  95. Node* lightNode = scene_->CreateChild("DirectionalLight");
  96. lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f));
  97. Light* light = lightNode->CreateComponent<Light>();
  98. light->SetLightType(LIGHT_DIRECTIONAL);
  99. light->SetCastShadows(true);
  100. light->SetShadowBias(BiasParameters(0.00025f, 0.5f));
  101. // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
  102. light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f));
  103. // Create randomly sized boxes. If boxes are big enough, make them occluders
  104. const unsigned NUM_BOXES = 20;
  105. for (unsigned i = 0; i < NUM_BOXES; ++i)
  106. {
  107. Node* boxNode = scene_->CreateChild("Box");
  108. float size = 1.0f + Random(10.0f);
  109. boxNode->SetPosition(Vector3(Random(80.0f) - 40.0f, size * 0.5f, Random(80.0f) - 40.0f));
  110. boxNode->SetScale(size);
  111. StaticModel* boxObject = boxNode->CreateComponent<StaticModel>();
  112. boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  113. boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
  114. boxObject->SetCastShadows(true);
  115. if (size >= 3.0f)
  116. boxObject->SetOccluder(true);
  117. }
  118. // Create a DynamicNavigationMesh component to the scene root
  119. DynamicNavigationMesh* navMesh = scene_->CreateComponent<DynamicNavigationMesh>();
  120. // Set the agent height large enough to exclude the layers under boxes
  121. navMesh->SetAgentHeight(10.0f);
  122. navMesh->SetTileSize(64);
  123. // Create a Navigable component to the scene root. This tags all of the geometry in the scene as being part of the
  124. // navigation mesh. By default this is recursive, but the recursion could be turned off from Navigable
  125. scene_->CreateComponent<Navigable>();
  126. // Add padding to the navigation mesh in Y-direction so that we can add objects on top of the tallest boxes
  127. // in the scene and still update the mesh correctly
  128. navMesh->SetPadding(Vector3(0.0f, 10.0f, 0.0f));
  129. // Now build the navigation geometry. This will take some time. Note that the navigation mesh will prefer to use
  130. // physics geometry from the scene nodes, as it often is simpler, but if it can not find any (like in this example)
  131. // it will use renderable geometry instead
  132. navMesh->Build();
  133. DetourCrowdManager* crowdManager = scene_->CreateComponent<DetourCrowdManager>();
  134. // Create Jack node that will follow the path
  135. CreateJack(Vector3(-5.0f, 0.0f, 20.0f));
  136. // Create some mushrooms
  137. const unsigned NUM_MUSHROOMS = 100;
  138. for (unsigned i = 0; i < NUM_MUSHROOMS; ++i)
  139. CreateMushroom(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f));
  140. // Create the camera. Limit far clip distance to match the fog
  141. cameraNode_ = scene_->CreateChild("Camera");
  142. Camera* camera = cameraNode_->CreateComponent<Camera>();
  143. camera->SetFarClip(300.0f);
  144. // Set an initial position for the camera scene node above the plane
  145. cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f));
  146. }
  147. void CrowdNavigation::CreateUI()
  148. {
  149. ResourceCache* cache = GetSubsystem<ResourceCache>();
  150. UI* ui = GetSubsystem<UI>();
  151. // Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will
  152. // control the camera, and when visible, it will point the raycast target
  153. XMLFile* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  154. SharedPtr<Cursor> cursor(new Cursor(context_));
  155. cursor->SetStyleAuto(style);
  156. ui->SetCursor(cursor);
  157. // Set starting position of the cursor at the rendering window center
  158. Graphics* graphics = GetSubsystem<Graphics>();
  159. cursor->SetPosition(graphics->GetWidth() / 2, graphics->GetHeight() / 2);
  160. // Construct new Text object, set string to display and font to use
  161. Text* instructionText = ui->GetRoot()->CreateChild<Text>();
  162. instructionText->SetText(
  163. "Use WASD keys to move, RMB to rotate view\n"
  164. "LMB to set destination, SHIFT+LMB to spawn a Jack\n"
  165. "MMB to add or remove obstacles\n"
  166. "F5 to save scene, F7 to load\n"
  167. "Space to toggle debug geometry"
  168. );
  169. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  170. // The text has multiple rows. Center them in relation to each other
  171. instructionText->SetTextAlignment(HA_CENTER);
  172. // Position the text relative to the screen center
  173. instructionText->SetHorizontalAlignment(HA_CENTER);
  174. instructionText->SetVerticalAlignment(VA_CENTER);
  175. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  176. }
  177. void CrowdNavigation::SetupViewport()
  178. {
  179. Renderer* renderer = GetSubsystem<Renderer>();
  180. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  181. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  182. renderer->SetViewport(0, viewport);
  183. }
  184. void CrowdNavigation::SubscribeToEvents()
  185. {
  186. // Subscribe HandleUpdate() function for processing update events
  187. SubscribeToEvent(E_UPDATE, HANDLER(CrowdNavigation, HandleUpdate));
  188. // Subscribe HandlePostRenderUpdate() function for processing the post-render update event, during which we request
  189. // debug geometry
  190. SubscribeToEvent(E_POSTRENDERUPDATE, HANDLER(CrowdNavigation, HandlePostRenderUpdate));
  191. // Subscribe HandleCrowdAgentFailure() function for resolving invalidation issues with agents, during which we
  192. // use a larger extents for finding a point on the navmesh to fix the agent's position
  193. SubscribeToEvent(E_CROWD_AGENT_FAILURE, HANDLER(CrowdNavigation, HandleCrowdAgentFailure));
  194. }
  195. void CrowdNavigation::MoveCamera(float timeStep)
  196. {
  197. // Right mouse button controls mouse cursor visibility: hide when pressed
  198. UI* ui = GetSubsystem<UI>();
  199. Input* input = GetSubsystem<Input>();
  200. ui->GetCursor()->SetVisible(!input->GetMouseButtonDown(MOUSEB_RIGHT));
  201. // Do not move if the UI has a focused element (the console)
  202. if (ui->GetFocusElement())
  203. return;
  204. // Movement speed as world units per second
  205. const float MOVE_SPEED = 20.0f;
  206. // Mouse sensitivity as degrees per pixel
  207. const float MOUSE_SENSITIVITY = 0.1f;
  208. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  209. // Only move the camera when the cursor is hidden
  210. if (!ui->GetCursor()->IsVisible())
  211. {
  212. IntVector2 mouseMove = input->GetMouseMove();
  213. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  214. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  215. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  216. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  217. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  218. }
  219. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  220. if (input->GetKeyDown('W'))
  221. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  222. if (input->GetKeyDown('S'))
  223. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  224. if (input->GetKeyDown('A'))
  225. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  226. if (input->GetKeyDown('D'))
  227. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  228. // Set destination or spawn a new jack with left mouse button
  229. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  230. SetPathPoint();
  231. // Add or remove objects with middle mouse button, then rebuild navigation mesh partially
  232. if (input->GetMouseButtonPress(MOUSEB_MIDDLE))
  233. AddOrRemoveObject();
  234. // Check for loading/saving the scene. Save the scene to the file Data/Scenes/CrowdNavigation.xml relative to the executable
  235. // directory
  236. if (input->GetKeyPress(KEY_F5))
  237. {
  238. File saveFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/CrowdNavigation.xml", FILE_WRITE);
  239. scene_->SaveXML(saveFile);
  240. }
  241. if (input->GetKeyPress(KEY_F7))
  242. {
  243. File loadFile(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Scenes/CrowdNavigation.xml", FILE_READ);
  244. scene_->LoadXML(loadFile);
  245. // Redetermine which nodes are "jacks" and "mushrooms"
  246. jackNodes_.Clear();
  247. PODVector<Node*> nodes;
  248. scene_->GetChildrenWithComponent<CrowdAgent>(nodes, true);
  249. for (unsigned i = 0; i < nodes.Size(); ++i)
  250. {
  251. jackNodes_.Push(SharedPtr<Node>(nodes[i]));
  252. }
  253. mushroomNodes_.Clear();
  254. scene_->GetChildrenWithComponent<Obstacle>(nodes, true);
  255. for (unsigned i = 0; i < nodes.Size(); ++i)
  256. {
  257. mushroomNodes_.Push(SharedPtr<Node>(nodes[i]));
  258. }
  259. }
  260. // Toggle debug geometry with space
  261. if (input->GetKeyPress(KEY_SPACE))
  262. drawDebug_ = !drawDebug_;
  263. }
  264. void CrowdNavigation::SetPathPoint()
  265. {
  266. Vector3 hitPos;
  267. Drawable* hitDrawable;
  268. DynamicNavigationMesh* navMesh = scene_->GetComponent<DynamicNavigationMesh>();
  269. if (Raycast(250.0f, hitPos, hitDrawable))
  270. {
  271. Vector3 pathPos = navMesh->FindNearestPoint(hitPos, Vector3(1.0f, 1.0f, 1.0f));
  272. if (GetSubsystem<Input>()->GetQualifierDown(QUAL_SHIFT))
  273. {
  274. // spawn a jack at the target position
  275. CreateJack(pathPos);
  276. }
  277. else
  278. {
  279. // Set crowd agents to move to the target
  280. for (unsigned i = 0; i < jackNodes_.Size(); ++i)
  281. {
  282. CrowdAgent* agent = jackNodes_[i]->GetComponent<CrowdAgent>();
  283. // The first agent will always move to the exact position, all other agents will select a random point nearby
  284. if (i == 0)
  285. {
  286. agent->SetMoveTarget(pathPos);
  287. }
  288. else
  289. {
  290. // Keep the random point somewhere on the navigation mesh
  291. Vector3 targetPos = navMesh->FindNearestPoint(pathPos + Vector3(Random(-4.5f, 4.5f), 0.0f, Random(-4.5f, 4.5f)), Vector3(1.0f, 1.0f, 1.0f));
  292. agent->SetMoveTarget(targetPos);
  293. }
  294. }
  295. }
  296. }
  297. }
  298. void CrowdNavigation::AddOrRemoveObject()
  299. {
  300. // Raycast and check if we hit a mushroom node. If yes, remove it, if no, create a new one
  301. Vector3 hitPos;
  302. Drawable* hitDrawable;
  303. if (Raycast(250.0f, hitPos, hitDrawable))
  304. {
  305. Node* hitNode = hitDrawable->GetNode();
  306. if (hitNode->GetName() == "Mushroom")
  307. {
  308. mushroomNodes_.Remove(hitNode);
  309. hitNode->Remove();
  310. }
  311. else
  312. {
  313. CreateMushroom(hitPos);
  314. }
  315. }
  316. }
  317. void CrowdNavigation::CreateJack(const Vector3& pos)
  318. {
  319. ResourceCache* cache = GetSubsystem<ResourceCache>();
  320. SharedPtr<Node> jackNode(scene_->CreateChild("Jack"));
  321. jackNode->SetPosition(pos);
  322. AnimatedModel* modelObject = jackNode->CreateComponent<AnimatedModel>();
  323. modelObject->SetModel(cache->GetResource<Model>("Models/Jack.mdl"));
  324. modelObject->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml"));
  325. modelObject->SetCastShadows(true);
  326. // Create the CrowdAgent
  327. CrowdAgent* agent = jackNode->CreateComponent<CrowdAgent>();
  328. agent->SetHeight(2.0f);
  329. jackNodes_.Push(jackNode);
  330. }
  331. Node* CrowdNavigation::CreateMushroom(const Vector3& pos)
  332. {
  333. ResourceCache* cache = GetSubsystem<ResourceCache>();
  334. Node* mushroomNode = scene_->CreateChild("Mushroom");
  335. mushroomNode->SetPosition(pos);
  336. mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f));
  337. mushroomNode->SetScale(2.0f + Random(0.5f));
  338. StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>();
  339. mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl"));
  340. mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml"));
  341. mushroomObject->SetCastShadows(true);
  342. // Create the navigation obstacle
  343. Obstacle* obstacle = mushroomNode->CreateComponent<Obstacle>();
  344. obstacle->SetRadius(mushroomNode->GetScale().x_);
  345. mushroomNodes_.Push(mushroomNode);
  346. return mushroomNode;
  347. }
  348. bool CrowdNavigation::Raycast(float maxDistance, Vector3& hitPos, Drawable*& hitDrawable)
  349. {
  350. hitDrawable = 0;
  351. UI* ui = GetSubsystem<UI>();
  352. IntVector2 pos = ui->GetCursorPosition();
  353. // Check the cursor is visible and there is no UI element in front of the cursor
  354. if (!ui->GetCursor()->IsVisible() || ui->GetElementAt(pos, true))
  355. return false;
  356. Graphics* graphics = GetSubsystem<Graphics>();
  357. Camera* camera = cameraNode_->GetComponent<Camera>();
  358. Ray cameraRay = camera->GetScreenRay((float)pos.x_ / graphics->GetWidth(), (float)pos.y_ / graphics->GetHeight());
  359. // Pick only geometry objects, not eg. zones or lights, only get the first (closest) hit
  360. PODVector<RayQueryResult> results;
  361. RayOctreeQuery query(results, cameraRay, RAY_TRIANGLE, maxDistance, DRAWABLE_GEOMETRY);
  362. scene_->GetComponent<Octree>()->RaycastSingle(query);
  363. if (results.Size())
  364. {
  365. RayQueryResult& result = results[0];
  366. hitPos = result.position_;
  367. hitDrawable = result.drawable_;
  368. return true;
  369. }
  370. return false;
  371. }
  372. void CrowdNavigation::HandleUpdate(StringHash eventType, VariantMap& eventData)
  373. {
  374. using namespace Update;
  375. // Take the frame time step, which is stored as a float
  376. float timeStep = eventData[P_TIMESTEP].GetFloat();
  377. // Move the camera, scale movement with time step
  378. MoveCamera(timeStep);
  379. for (unsigned i = 0; i < jackNodes_.Size(); ++i)
  380. {
  381. CrowdAgent* agent = jackNodes_[i]->GetComponent<CrowdAgent>();
  382. Vector3 vel = agent->GetActualVelocity();
  383. jackNodes_[i]->SetWorldDirection(vel);
  384. }
  385. }
  386. void CrowdNavigation::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  387. {
  388. // If draw debug mode is enabled, draw navigation mesh debug geometry
  389. if (drawDebug_)
  390. {
  391. DebugRenderer* debugRenderer = scene_->GetComponent<DebugRenderer>();
  392. DynamicNavigationMesh* navMesh = scene_->GetComponent<DynamicNavigationMesh>();
  393. navMesh->DrawDebugGeometry(debugRenderer, true);
  394. for (unsigned i = 0; i < jackNodes_.Size(); ++i)
  395. {
  396. CrowdAgent* agent = jackNodes_[i]->GetComponent<CrowdAgent>();
  397. agent->DrawDebugGeometry(true);
  398. }
  399. for (unsigned i = 0; i < mushroomNodes_.Size(); ++i)
  400. {
  401. Obstacle* obstacle = mushroomNodes_[i]->GetComponent<Obstacle>();
  402. obstacle->DrawDebugGeometry(true);
  403. }
  404. }
  405. }
  406. void CrowdNavigation::HandleCrowdAgentFailure(StringHash eventType, VariantMap& eventData)
  407. {
  408. using namespace CrowdAgentFailure;
  409. Node* node = static_cast<Node*>(eventData[P_NODE].GetPtr());
  410. CrowdAgent* agent = static_cast<CrowdAgent*>(eventData[P_CROWD_AGENT].GetPtr());
  411. CrowdAgentState agentState = (CrowdAgentState)eventData[P_CROWD_AGENT_STATE].GetInt();
  412. // If the agent's state is invalid, likely from spawning on the side of a box, find a point in a larger area
  413. if (agentState == CROWD_AGENT_INVALID)
  414. {
  415. DynamicNavigationMesh* navMesh = scene_->GetComponent<DynamicNavigationMesh>();
  416. // Get a point on the navmesh using more generous extents
  417. Vector3 newPt = navMesh->FindNearestPoint(node->GetWorldPosition(), Vector3(5.0f, 5.0f, 5.0f));
  418. // Set the new node position, CrowdAgent component will automatically reset the state of the agent
  419. node->SetWorldPosition(newPt);
  420. }
  421. }