int CrowdToolState::hitTestAgents(const float* s, const float* p) { if (!m_sample) return -1; dtCrowd* crowd = m_sample->getCrowd(); int isel = -1; float tsel = FLT_MAX; for (int i = 0; i < crowd->getAgentCount(); ++i) { const dtCrowdAgent* ag = crowd->getAgent(i); if (!ag->active) continue; float bmin[3], bmax[3]; getAgentBounds(ag, bmin, bmax); float tmin, tmax; if (isectSegAABB(s, p, bmin,bmax, tmin, tmax)) { if (tmin > 0 && tmin < tsel) { isel = i; tsel = tmin; } } } return isel; }
void CrowdTool::handleClick(const float* s, const float* p, bool shift) { if (!m_sample) return; InputGeom* geom = m_sample->getInputGeom(); if (!geom) return; if (m_mode == TOOLMODE_CREATE) { if (shift) { // Delete int isel = -1; float tsel = FLT_MAX; for (int i = 0; i < m_crowd.getAgentCount(); ++i) { const Agent* ag = m_crowd.getAgent(i); if (!ag->active) continue; float bmin[3], bmax[3]; getAgentBounds(ag, bmin, bmax); float tmin, tmax; if (isectSegAABB(s, p, bmin,bmax, tmin, tmax)) { if (tmin > 0 && tmin < tsel) { isel = i; tsel = tmin; } } } if (isel != -1) { m_crowd.removeAgent(isel); } } else { // Add dtNavMeshQuery* navquery = m_sample->getNavMeshQuery(); int idx = m_crowd.addAgent(p, m_sample->getAgentRadius(), m_sample->getAgentHeight(), navquery); if (idx != -1 && m_targetRef) m_crowd.requestMoveTarget(idx, m_targetRef, m_targetPos); } } else if (m_mode == TOOLMODE_MOVE_TARGET) { // Find nearest point on navmesh and set move request to that location. dtNavMeshQuery* navquery = m_sample->getNavMeshQuery(); const dtQueryFilter* filter = m_crowd.getFilter(); const float* ext = m_crowd.getQueryExtents(); navquery->findNearestPoly(p, ext, filter, &m_targetRef, m_targetPos); if (shift) { // Adjust target using tiny local search. for (int i = 0; i < m_crowd.getAgentCount(); ++i) { const Agent* ag = m_crowd.getAgent(i); if (!ag->active) continue; m_crowd.adjustMoveTarget(i, m_targetRef, m_targetPos); } } else { // Move target using paht finder for (int i = 0; i < m_crowd.getAgentCount(); ++i) { const Agent* ag = m_crowd.getAgent(i); if (!ag->active) continue; m_crowd.requestMoveTarget(i, m_targetRef, m_targetPos); } } } }