Posted on

Navigation meshes, crowds, agents and obstacles

The key components are; TiledNavMeshComponent, NavMeshCrowdComponent, NavMeshObstacle and NavMeshAgent. As can be seen from the video in the editor you can now import models then generate navigable areas for agents to move around. Within the editor you can then wire up input devices to scripts that then perform ray -> mesh intersection to move your agents. The entire scene (models, nav mesh, agents, scripts and input handling) can then be saved to file or published as a code project from within the editor. A typical script (that is saved into the scene file) is included below. You would typically trigger this script in response to an event (i.e. an input device event – by adding a Mouse input device to the scene and wiring up its onMouseButtonDown event to the script’s execute method or in response to an object entering a collision volume. (see the collision volume support in the video log).


-- find the agent, nav mesh and crowd
scene            = sdk:GetScene()
agent            = scene:Find('Agent')
navMesh          = scene:Find('NavMesh')
agentComponent   = ext_NavigationScript.GetComponent_Navigation_NavMeshAgentComponent(agent)
crowdComponent   = ext_NavigationScript.GetComponent_Navigation_NavMeshCrowdComponent(navMesh)
navMeshComponent = ext_NavigationScript.GetComponent_Navigation_TiledNavMeshComponent(navMesh)

-- unproject the current mouse pos at both near / far clip planes
mousePos     = sdk:GetMousePosition()
pickRayNear  = fireflyscript.vec3()
pickRayFar   = fireflyscript.vec3()
sdk:UnprojectNearFar(mousePos:GetX(), mousePos:GetY(), pickRayNear, pickRayFar)

-- obtain navigation mesh transform
navMesh      = fireflyscript.CastSceneItemToPickableSceneItem(navMesh)
worldToLocal = navMesh:GetWorldMatrixInverse()
localToWorld = navMesh:GetWorldMatrix()

-- test pick ray against nav mesh geometry
geom                   = navMeshComponent:GetMesh()
localPickRayNear       = worldToLocal * fireflyscript.vec4(pickRayNear, 1)
localPickRayFar        = worldToLocal * fireflyscript.vec4(pickRayFar, 1)
localIntersectionPoint = fireflyscript.vec3()
geom:LineTest(fireflyscript.vec3(localPickRayNear), fireflyscript.vec3(localPickRayFar), localIntersectionPoint)

-- move the agent(s)
crowdComponent:SetTargetPoint(fireflyscript.vec3(localToWorld * fireflyscript.vec4(localIntersectionPoint,1)))
Liked it? Take a second to support ben morris on Patreon!

Leave a Reply

Your email address will not be published. Required fields are marked *