@@ -2328,46 +2328,45 @@ void CClientGame::ProcessServerControlBind(CControlFunctionBind* pBind)
23282328CClientEntity* CClientGame::CheckClientSideEntityClick (float fScreenX , float fScreenY )
23292329{
23302330 if (!m_pMarkerManager)
2331- return NULL ;
2332-
2333- CClientMarker* pClosestMarker = NULL ;
2331+ return nullptr ;
2332+
2333+ CCamera* pCamera = g_pGame->GetCamera ();
2334+ CMatrix matCamera;
2335+ pCamera->GetMatrix (&matCamera);
2336+ CVector vecOrigin = matCamera.vPos ;
2337+
2338+ CVector vecTarget, vecScreen (fScreenX , fScreenY , 300 .0f );
2339+ g_pCore->GetGraphics ()->CalcWorldCoors (&vecScreen, &vecTarget);
2340+
2341+ CVector vecRayDir = vecTarget - vecOrigin;
2342+ vecRayDir.Normalize ();
2343+
2344+ CClientMarker* pClosestMarker = nullptr ;
23342345 float fClosestDist = 99999 .9f ;
23352346
2336- CFastList<CClientMarker*>::const_iterator iter = m_pMarkerManager->m_Markers .begin ();
2337- for (; iter != m_pMarkerManager->m_Markers .end (); ++iter)
2347+ for (auto * pMarker : m_pMarkerManager->m_Markers )
23382348 {
2339- CClientMarker* pMarker = *iter;
2340- if (pMarker && pMarker->IsStreamedIn () && pMarker->IsVisible ())
2341- {
2342- CVector vecPosition;
2343- pMarker->GetPosition (vecPosition);
2344-
2345- CVector vecScreen;
2346- g_pCore->GetGraphics ()->CalcScreenCoors (&vecPosition, &vecScreen);
2349+ if (!pMarker || !pMarker->IsStreamedIn () || !pMarker->IsVisible ())
2350+ continue ;
23472351
2348- if (pMarker->IsClientSideOnScreen ())
2349- {
2350- CSphere boundingSphere = pMarker->GetWorldBoundingSphere ();
2351-
2352- CVector vecEdgePos = boundingSphere.vecPosition ;
2353- vecEdgePos.fX += boundingSphere.fRadius ;
2354-
2355- CVector vecCenterScreen, vecEdgeScreen;
2356- g_pCore->GetGraphics ()->CalcScreenCoors (&boundingSphere.vecPosition , &vecCenterScreen);
2357- g_pCore->GetGraphics ()->CalcScreenCoors (&vecEdgePos, &vecEdgeScreen);
2358-
2359- float fScreenRadius = abs (vecEdgeScreen.fX - vecCenterScreen.fX );
2360-
2361- float fDistX = vecCenterScreen.fX - fScreenX ;
2362- float fDistY = vecCenterScreen.fY - fScreenY ;
2363- float fDist = sqrt (fDistX * fDistX + fDistY * fDistY );
2364-
2365- if (fDist < fScreenRadius && fDist < fClosestDist )
2366- {
2367- fClosestDist = fDist ;
2368- pClosestMarker = pMarker;
2369- }
2370- }
2352+ if (!pMarker->IsClientSideOnScreen ())
2353+ continue ;
2354+
2355+ CSphere boundingSphere = pMarker->GetWorldBoundingSphere ();
2356+
2357+ CVector vecToSphere = boundingSphere.vecPosition - vecOrigin;
2358+ float fProjection = vecToSphere.DotProduct (&vecRayDir);
2359+
2360+ if (fProjection <= 0 .0f )
2361+ continue ;
2362+
2363+ CVector vecClosestPoint = vecOrigin + vecRayDir * fProjection ;
2364+ float fDistanceToRay = (boundingSphere.vecPosition - vecClosestPoint).Length ();
2365+
2366+ if (fDistanceToRay <= boundingSphere.fRadius && fProjection < fClosestDist )
2367+ {
2368+ fClosestDist = fProjection ;
2369+ pClosestMarker = pMarker;
23712370 }
23722371 }
23732372
@@ -2441,7 +2440,11 @@ bool CClientGame::ProcessMessageForCursorEvents(HWND hwnd, UINT uMsg, WPARAM wPa
24412440
24422441 CVector vecCollision;
24432442 ElementID CollisionEntityID = INVALID_ELEMENT_ID;
2444- CClientEntity* pCollisionEntity = NULL ;
2443+ CClientEntity* pCollisionEntity = nullptr ;
2444+ float fObjectDistance = 99999 .9f ;
2445+
2446+ CClientEntity* pClientSideEntity = CheckClientSideEntityClick (static_cast <float >(iX), static_cast <float >(iY));
2447+
24452448 if (bCollision && pColPoint)
24462449 {
24472450 vecCollision = pColPoint->GetPosition ();
@@ -2451,6 +2454,7 @@ bool CClientGame::ProcessMessageForCursorEvents(HWND hwnd, UINT uMsg, WPARAM wPa
24512454 CClientEntity* pEntity = pPools->GetClientEntity ((DWORD*)pGameEntity->GetInterface ());
24522455 if (pEntity)
24532456 {
2457+ fObjectDistance = (vecCollision - vecOrigin).Length ();
24542458 pCollisionEntity = pEntity;
24552459 if (!pEntity->IsLocalEntity ())
24562460 CollisionEntityID = pEntity->GetID ();
@@ -2464,18 +2468,21 @@ bool CClientGame::ProcessMessageForCursorEvents(HWND hwnd, UINT uMsg, WPARAM wPa
24642468
24652469 // Destroy the colpoint so we don't get a leak
24662470 if (pColPoint)
2467- {
24682471 pColPoint->Destroy ();
2469- }
24702472
2471- if (!pCollisionEntity )
2473+ if (pClientSideEntity )
24722474 {
2473- CClientEntity* pClientSideEntity = CheckClientSideEntityClick ((float )iX, (float )iY);
2474- if (pClientSideEntity)
2475+ CVector vecMarkerPos;
2476+ pClientSideEntity->GetPosition (vecMarkerPos);
2477+ float fMarkerDistance = (vecMarkerPos - vecOrigin).Length ();
2478+
2479+ if (fMarkerDistance < fObjectDistance )
24752480 {
24762481 pCollisionEntity = pClientSideEntity;
24772482 if (!pClientSideEntity->IsLocalEntity ())
24782483 CollisionEntityID = pClientSideEntity->GetID ();
2484+
2485+ vecCollision = vecMarkerPos;
24792486 }
24802487 }
24812488
0 commit comments