Improve PickEntitiesAtPoint

First, do a ray intersection test with the bounding-sphere for all
entities on the map and then check the more detailed selection shape for
the remaining candidates. Do checks that require component lookups after
the ray intersection tests because these are relatively expensive.
The old method for figuring out which entities are below the mouse
cursor was incorrect because it does a 2D check to filter out the first
candidates which can lead to incorrect results with lower camera angles
and high buildings or buildings with a large footprint. Such problems
were avoided with quite a large radius for this 2D test and resulted in
a large number of candiate entities after this first test (200-500).
Also rename PickEntitiesAtPoint to PickEntityAtPoint and make it return
only one (the closest) match.

I've tested performance with the tracelogger by starting a map and then
moving the mouse in circles for one minute. The results were relatively
stable. I've compared the total time percentage of input.js:836, which
spends nearly all of the time in PickEntityAtPoint.
Ardennes Forest - Normal size:       Original: 41.46%    Patched: 31.6%
Ardennes Forest - Giant size:        Original: 40.59%    Patched: 51.55%

As we see, it's faster on normal map sizes but slower on giant maps with
a lot of entities.
This approach can be further improved with some kind of spatial
subdivision for the culling (like an octree), which would help the unit
renderer too. This way it should be possible to make it faster (and
still correct) on all map sizes and with a large total numbers of
entities.

This was SVN commit r16098.
This commit is contained in:
Yves 2015-01-01 23:05:26 +00:00
parent 7ac837fb90
commit 8e30410109
9 changed files with 161 additions and 116 deletions

View File

@ -32,11 +32,11 @@ const INPUT_MASSTRIBUTING = 10;
var inputState = INPUT_NORMAL; var inputState = INPUT_NORMAL;
const INVALID_ENTITY = 0;
var mouseX = 0; var mouseX = 0;
var mouseY = 0; var mouseY = 0;
var mouseIsOverObject = false; var mouseIsOverObject = false;
// Distance to search for a selatable entity in. Bigger numbers are slower.
var SELECTION_SEARCH_RADIUS = 100;
// Number of pixels the mouse can move before the action is considered a drag // Number of pixels the mouse can move before the action is considered a drag
var maxDragDelta = 4; var maxDragDelta = 4;
@ -278,13 +278,13 @@ function determineAction(x, y, fromMinimap)
if (!g_DevSettings.controlAll && !allOwnedByPlayer) if (!g_DevSettings.controlAll && !allOwnedByPlayer)
return undefined; return undefined;
var targets = [];
var target = undefined; var target = undefined;
if (!fromMinimap) if (!fromMinimap)
targets = Engine.PickEntitiesAtPoint(x, y, SELECTION_SEARCH_RADIUS); {
var ent = Engine.PickEntityAtPoint(x, y);
if (targets.length) if (ent != INVALID_ENTITY)
target = targets[0]; target = ent;
}
// decide between the following ordered actions // decide between the following ordered actions
// if two actions are possible, the first one is taken // if two actions are possible, the first one is taken
@ -854,14 +854,12 @@ function handleInputAfterGui(ev)
g_ShowAllStatusBars = (ev.type == "hotkeydown"); g_ShowAllStatusBars = (ev.type == "hotkeydown");
recalculateStatusBarDisplay(); recalculateStatusBarDisplay();
} }
else if (ev.hotkey == "session.highlightguarding")
if (ev.hotkey == "session.highlightguarding")
{ {
g_ShowGuarding = (ev.type == "hotkeydown"); g_ShowGuarding = (ev.type == "hotkeydown");
updateAdditionalHighlight(); updateAdditionalHighlight();
} }
else if (ev.hotkey == "session.highlightguarded")
if (ev.hotkey == "session.highlightguarded")
{ {
g_ShowGuarded = (ev.type == "hotkeydown"); g_ShowGuarded = (ev.type == "hotkeydown");
updateAdditionalHighlight(); updateAdditionalHighlight();
@ -876,9 +874,9 @@ function handleInputAfterGui(ev)
{ {
case "mousemotion": case "mousemotion":
// Highlight the first hovered entity (if any) // Highlight the first hovered entity (if any)
var ents = Engine.PickEntitiesAtPoint(ev.x, ev.y, SELECTION_SEARCH_RADIUS); var ent = Engine.PickEntityAtPoint(ev.x, ev.y);
if (ents.length) if (ent != INVALID_ENTITY)
g_Selection.setHighlightList([ents[0]]); g_Selection.setHighlightList([ent]);
else else
g_Selection.setHighlightList([]); g_Selection.setHighlightList([]);
@ -930,9 +928,9 @@ function handleInputAfterGui(ev)
{ {
case "mousemotion": case "mousemotion":
// Highlight the first hovered entity (if any) // Highlight the first hovered entity (if any)
var ents = Engine.PickEntitiesAtPoint(ev.x, ev.y, SELECTION_SEARCH_RADIUS); var ent = Engine.PickEntityAtPoint(ev.x, ev.y);
if (ents.length) if (ent != INVALID_ENTITY)
g_Selection.setHighlightList([ents[0]]); g_Selection.setHighlightList([ent]);
else else
g_Selection.setHighlightList([]); g_Selection.setHighlightList([]);
@ -980,15 +978,19 @@ function handleInputAfterGui(ev)
return false; return false;
} }
var ents = Engine.PickEntitiesAtPoint(ev.x, ev.y, SELECTION_SEARCH_RADIUS); var ent = Engine.PickEntityAtPoint(ev.x, ev.y);
g_Selection.setHighlightList(ents); if (ent != INVALID_ENTITY)
g_Selection.setHighlightList([ent]);
else
g_Selection.setHighlightList([]);
return false; return false;
case "mousebuttonup": case "mousebuttonup":
if (ev.button == SDL_BUTTON_LEFT) if (ev.button == SDL_BUTTON_LEFT)
{ {
var ents = Engine.PickEntitiesAtPoint(ev.x, ev.y, SELECTION_SEARCH_RADIUS); var ents = [];
if (!ents.length) var selectedEntity = Engine.PickEntityAtPoint(ev.x, ev.y);
if (selectedEntity == INVALID_ENTITY)
{ {
if (!Engine.HotkeyIsPressed("selection.add") && !Engine.HotkeyIsPressed("selection.remove")) if (!Engine.HotkeyIsPressed("selection.add") && !Engine.HotkeyIsPressed("selection.remove"))
{ {
@ -999,7 +1001,6 @@ function handleInputAfterGui(ev)
return true; return true;
} }
var selectedEntity = ents[0];
var now = new Date(); var now = new Date();
// If camera following and we select different unit, stop // If camera following and we select different unit, stop
@ -1052,7 +1053,7 @@ function handleInputAfterGui(ev)
prevClickedEntity = selectedEntity; prevClickedEntity = selectedEntity;
// We only want to include the first picked unit in the selection // We only want to include the first picked unit in the selection
ents = [ents[0]]; ents = [selectedEntity];
} }
// Update the list of selected units // Update the list of selected units

View File

@ -112,11 +112,6 @@
<object size="100%-16 128 100% 144" type="checkbox" style="ModernTickBox" checked="true"> <object size="100%-16 128 100% 144" type="checkbox" style="ModernTickBox" checked="true">
<action on="Press"> <action on="Press">
Engine.GameView_SetConstrainCameraEnabled(this.checked); Engine.GameView_SetConstrainCameraEnabled(this.checked);
// Make selection more durable at the expense of speed if unchecked.
if (this.checked)
SELECTION_SEARCH_RADIUS -= 200;
else
SELECTION_SEARCH_RADIUS += 200;
</action> </action>
</object> </object>

View File

@ -173,9 +173,9 @@ void PostNetworkCommand(ScriptInterface::CxPrivate* pCxPrivate, CScriptVal cmd1)
cmpCommandQueue->PostNetworkCommand(CScriptVal(cmd2)); cmpCommandQueue->PostNetworkCommand(CScriptVal(cmd2));
} }
std::vector<entity_id_t> PickEntitiesAtPoint(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x, int y, int range) entity_id_t PickEntityAtPoint(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x, int y)
{ {
return EntitySelection::PickEntitiesAtPoint(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), x, y, g_Game->GetPlayerID(), false, range); return EntitySelection::PickEntityAtPoint(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), x, y, g_Game->GetPlayerID(), false);
} }
std::vector<entity_id_t> PickFriendlyEntitiesInRect(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x0, int y0, int x1, int y1, int player) std::vector<entity_id_t> PickFriendlyEntitiesInRect(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int x0, int y0, int x1, int y1, int player)
@ -959,7 +959,7 @@ void GuiScriptingInit(ScriptInterface& scriptInterface)
scriptInterface.RegisterFunction<void, CScriptVal, &PostNetworkCommand>("PostNetworkCommand"); scriptInterface.RegisterFunction<void, CScriptVal, &PostNetworkCommand>("PostNetworkCommand");
// Entity picking // Entity picking
scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, &PickEntitiesAtPoint>("PickEntitiesAtPoint"); scriptInterface.RegisterFunction<entity_id_t, int, int, &PickEntityAtPoint>("PickEntityAtPoint");
scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, int, int, &PickFriendlyEntitiesInRect>("PickFriendlyEntitiesInRect"); scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, int, int, int, int, &PickFriendlyEntitiesInRect>("PickFriendlyEntitiesInRect");
scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, &PickFriendlyEntitiesOnScreen>("PickFriendlyEntitiesOnScreen"); scriptInterface.RegisterFunction<std::vector<entity_id_t>, int, &PickFriendlyEntitiesOnScreen>("PickFriendlyEntitiesOnScreen");
scriptInterface.RegisterFunction<std::vector<entity_id_t>, std::string, bool, bool, bool, &PickSimilarFriendlyEntities>("PickSimilarFriendlyEntities"); scriptInterface.RegisterFunction<std::vector<entity_id_t>, std::string, bool, bool, bool, &PickSimilarFriendlyEntities>("PickSimilarFriendlyEntities");

View File

@ -55,6 +55,23 @@ public:
{ {
return m_Radius; return m_Radius;
} }
/**
* Check if the ray, defined by an origin point and a direction unit vector
* interesects with the sphere
*/
bool RayIntersect(const CVector3D& origin, const CVector3D& dir) const
{
CVector3D v = m_Center - origin; // Vector v from the origin of the ray to the center of the sphere
float pcLen = dir.Dot(v); // Length of the projection of v onto the direction vector of the ray
if(pcLen <= 0)
return false; // Sphere behind the ray
// Get the shortest distance from the center of the sphere to the ray
v = (dir * pcLen) - v;
if (v.LengthSquared() > m_Radius * m_Radius)
return false; // Distance to sphere center more than radius
return true;
}
}; };
#endif // INCLUDED_BOUNDINGSPHERE #endif // INCLUDED_BOUNDINGSPHERE

View File

@ -26,6 +26,7 @@
#include "ICmpRangeManager.h" #include "ICmpRangeManager.h"
#include "ICmpSelectable.h" #include "ICmpSelectable.h"
#include "ICmpVisibility.h" #include "ICmpVisibility.h"
#include "ICmpVisual.h"
#include "graphics/Frustum.h" #include "graphics/Frustum.h"
#include "graphics/ModelAbstract.h" #include "graphics/ModelAbstract.h"
@ -264,6 +265,63 @@ public:
{ {
m_EnableDebugOverlays = enabled; m_EnableDebugOverlays = enabled;
} }
virtual void PickAllEntitiesAtPoint(std::vector<std::pair<CEntityHandle, CVector3D> >& outEntities, const CVector3D& origin, const CVector3D& dir, bool allowEditorSelectables)
{
// First, make a rough test with the worst-case bounding boxes to pick all
// entities/models that could possibly be hit by the ray.
std::vector<SUnit*> candidates;
for (size_t i = 0; i < m_Units.size(); ++i)
{
SUnit& unit = m_Units[i];
if (!unit.actor)
continue;
if (unit.sweptBounds.RayIntersect(origin, dir))
candidates.push_back(&unit);
}
// Now make a more precise test to get rid of the remaining false positives
float tmin, tmax;
CVector3D center;
for (size_t i = 0; i< candidates.size(); ++i)
{
const SUnit& unit = *candidates[i];
CmpPtr<ICmpVisual> cmpVisual(unit.entity);
if (!cmpVisual)
continue;
CBoundingBoxOriented selectionBox = cmpVisual->GetSelectionBox();
if (selectionBox.IsEmpty())
{
if (!allowEditorSelectables)
continue;
// Fall back to using old AABB selection method for decals
// see: http://trac.wildfiregames.com/ticket/1032
// Decals are flat objects without a selectionShape defined,
// but they should still be selectable in the editor to move them
// around or delete them after they are placed.
// Check campaigns/labels/ in the Actors tab of atlas for examples.
CBoundingBoxAligned aABBox = cmpVisual->GetBounds();
if (aABBox.IsEmpty())
continue;
if (!aABBox.RayIntersect(origin, dir, tmin, tmax))
continue;
aABBox.GetCentre(center);
}
else
{
if (!selectionBox.RayIntersect(origin, dir, tmin, tmax))
continue;
center = selectionBox.m_Center;
}
outEntities.push_back(std::make_pair(unit.entity, center));
}
}
}; };
void CCmpUnitRenderer::TurnStart() void CCmpUnitRenderer::TurnStart()

View File

@ -54,6 +54,19 @@ public:
virtual void UpdateUnitPos(tag_t tag, bool inWorld, const CVector3D& pos0, const CVector3D& pos1) = 0; virtual void UpdateUnitPos(tag_t tag, bool inWorld, const CVector3D& pos0, const CVector3D& pos1) = 0;
/**
* Return a list of visual entities along with their center point.
* Visual means they have an associated actor and a visual component,
* but they could still be hiden in the fog of war for a specific player,
* for example.
* NOTE: It's generally faster to do a lot of ray intersection tests than
* querying a lot of entities for component interfaces and doing these types
* of tests first.
*/
virtual void PickAllEntitiesAtPoint(std::vector<std::pair<CEntityHandle, CVector3D> >& outEntities,
const CVector3D& origin, const CVector3D& dir,
bool allowEditorSelectables) = 0;
/** /**
* Returns the frame offset from the last Interpolate message. * Returns the frame offset from the last Interpolate message.
*/ */

View File

@ -27,41 +27,54 @@
#include "simulation2/components/ICmpTemplateManager.h" #include "simulation2/components/ICmpTemplateManager.h"
#include "simulation2/components/ICmpSelectable.h" #include "simulation2/components/ICmpSelectable.h"
#include "simulation2/components/ICmpVisual.h" #include "simulation2/components/ICmpVisual.h"
#include "simulation2/components/ICmpUnitRenderer.h"
#include "simulation2/helpers/Spatial.h" #include "simulation2/helpers/Spatial.h"
#include "ps/CLogger.h" #include "ps/CLogger.h"
#include "ps/Profiler2.h" #include "ps/Profiler2.h"
std::vector<entity_id_t> EntitySelection::PickEntitiesAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables, int range) entity_id_t EntitySelection::PickEntityAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables)
{ {
PROFILE2("PickEntitiesAtPoint"); PROFILE2("PickEntityAtPoint");
CVector3D origin, dir; CVector3D origin, dir;
camera.BuildCameraRay(screenX, screenY, origin, dir); camera.BuildCameraRay(screenX, screenY, origin, dir);
CmpPtr<ICmpUnitRenderer> cmpUnitRenderer(simulation.GetSimContext().GetSystemEntity());
ENSURE(cmpUnitRenderer);
std::vector<std::pair<CEntityHandle, CVector3D> > entities;
cmpUnitRenderer->PickAllEntitiesAtPoint(entities, origin, dir, allowEditorSelectables);
if (entities.empty())
return INVALID_ENTITY;
// Filter for relevent entities in the list of candidates (all entities below the mouse)
std::vector<std::pair<float, CEntityHandle> > hits; // (dist^2, entity) pairs
for (size_t i = 0; i < entities.size(); ++i)
{
// Find the perpendicular distance from the object's centre to the picker ray
float dist2;
const CVector3D center = entities[i].second;
CVector3D closest = origin + dir * (center - origin).Dot(dir);
dist2 = (closest - center).LengthSquared();
hits.push_back(std::make_pair(dist2, entities[i].first));
}
// Sort hits by distance
struct SortFun {
bool operator() ( std::pair<float, CEntityHandle> i, std::pair<float, CEntityHandle> j) { return (i.first<j.first);}
} sortFun;
std::sort(hits.begin(), hits.end(), sortFun);
CmpPtr<ICmpRangeManager> cmpRangeManager(simulation, SYSTEM_ENTITY); CmpPtr<ICmpRangeManager> cmpRangeManager(simulation, SYSTEM_ENTITY);
ENSURE(cmpRangeManager); ENSURE(cmpRangeManager);
/* We try to approximate where the mouse is hovering by drawing a ray from for (size_t i = 0; i < hits.size(); ++i)
* the center of the camera and through the mouse then taking the position
* at which the ray intersects the terrain. */
// TODO: Do this smarter without being slow.
CVector3D pos3d = camera.GetWorldCoordinates(screenX, screenY, true);
// Change the position to 2D by removing the terrain height.
CFixedVector2D pos(fixed::FromFloat(pos3d.X), fixed::FromFloat(pos3d.Z));
// Get a rough group of entities using our approximated origin.
std::vector<entity_id_t> ents;
cmpRangeManager->GetSubdivision()->GetNear(ents, pos, entity_pos_t::FromInt(range));
// Filter for relevent entities and calculate precise distances.
std::vector<std::pair<float, entity_id_t> > hits; // (dist^2, entity) pairs
for (size_t i = 0; i < ents.size(); ++i)
{ {
CmpPtr<ICmpSelectable> cmpSelectable(simulation, ents[i]); const CEntityHandle& handle = hits[i].second;
CmpPtr<ICmpSelectable> cmpSelectable(handle);
if (!cmpSelectable) if (!cmpSelectable)
continue; continue;
CEntityHandle handle = cmpSelectable->GetEntityHandle();
// Check if this entity is only selectable in Atlas // Check if this entity is only selectable in Atlas
if (!allowEditorSelectables && cmpSelectable->IsEditorOnly()) if (!allowEditorSelectables && cmpSelectable->IsEditorOnly())
continue; continue;
@ -70,55 +83,9 @@ std::vector<entity_id_t> EntitySelection::PickEntitiesAtPoint(CSimulation2& simu
if (cmpRangeManager->GetLosVisibility(handle, player) == ICmpRangeManager::VIS_HIDDEN) if (cmpRangeManager->GetLosVisibility(handle, player) == ICmpRangeManager::VIS_HIDDEN)
continue; continue;
CmpPtr<ICmpVisual> cmpVisual(handle); return handle.GetId();
if (!cmpVisual)
continue;
CVector3D center;
float tmin, tmax;
CBoundingBoxOriented selectionBox = cmpVisual->GetSelectionBox();
if (selectionBox.IsEmpty())
{
if (!allowEditorSelectables)
continue;
// Fall back to using old AABB selection method for decals
// see: http://trac.wildfiregames.com/ticket/1032
CBoundingBoxAligned aABBox = cmpVisual->GetBounds();
if (aABBox.IsEmpty())
continue;
if (!aABBox.RayIntersect(origin, dir, tmin, tmax))
continue;
aABBox.GetCentre(center);
}
else
{
if (!selectionBox.RayIntersect(origin, dir, tmin, tmax))
continue;
center = selectionBox.m_Center;
}
// Find the perpendicular distance from the object's centre to the picker ray
float dist2;
CVector3D closest = origin + dir * (center - origin).Dot(dir);
dist2 = (closest - center).LengthSquared();
hits.push_back(std::make_pair(dist2, ents[i]));
} }
return INVALID_ENTITY;
// Sort hits by distance
std::sort(hits.begin(), hits.end()); // lexicographic comparison
// Extract the entity IDs
std::vector<entity_id_t> hitEnts;
hitEnts.reserve(hits.size());
for (size_t i = 0; i < hits.size(); ++i)
hitEnts.push_back(hits[i].second);
return hitEnts;
} }
/** /**

View File

@ -47,7 +47,7 @@ namespace EntitySelection
* *
* @return ordered list of selected entities with the closest first. * @return ordered list of selected entities with the closest first.
*/ */
std::vector<entity_id_t> PickEntitiesAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables, int range = 200); entity_id_t PickEntityAtPoint(CSimulation2& simulation, const CCamera& camera, int screenX, int screenY, player_id_t player, bool allowEditorSelectables);
/** /**
* Finds all selectable entities within the given screen coordinate rectangle, * Finds all selectable entities within the given screen coordinate rectangle,

View File

@ -622,17 +622,16 @@ QUERYHANDLER(PickObject)
// Normally this function would be called with a player ID to check LOS, // Normally this function would be called with a player ID to check LOS,
// but in Atlas the entire map is revealed, so just pass INVALID_PLAYER // but in Atlas the entire map is revealed, so just pass INVALID_PLAYER
std::vector<entity_id_t> ents = EntitySelection::PickEntitiesAtPoint(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), x, y, INVALID_PLAYER, msg->selectActors); entity_id_t ent = EntitySelection::PickEntityAtPoint(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), x, y, INVALID_PLAYER, msg->selectActors);;
// Multiple entities may have been picked, but they are sorted by distance, if (ent == INVALID_ENTITY)
// so only take the first one msg->id = INVALID_ENTITY;
if (!ents.empty()) else
{ {
msg->id = ents[0]; msg->id = ent;
// Calculate offset of object from original mouse click position // Calculate offset of object from original mouse click position
// so it gets moved by that offset // so it gets moved by that offset
CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), (entity_id_t)ents[0]); CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), ent);
if (!cmpPosition || !cmpPosition->IsInWorld()) if (!cmpPosition || !cmpPosition->IsInWorld())
{ {
// error // error
@ -650,11 +649,6 @@ QUERYHANDLER(PickObject)
msg->offsety = (int)(cy - y); msg->offsety = (int)(cy - y);
} }
} }
else
{
// No entity picked
msg->id = INVALID_ENTITY;
}
} }