0ad/source/maths/BoundingSphere.h
Yves 8e30410109 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.
2015-01-01 23:05:26 +00:00

78 lines
2.3 KiB
C++

/* Copyright (C) 2014 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INCLUDED_BOUNDINGSPHERE
#define INCLUDED_BOUNDINGSPHERE
#include "maths/BoundingBoxAligned.h"
#include "maths/Vector3D.h"
class CBoundingSphere
{
private:
CVector3D m_Center;
float m_Radius;
public:
CBoundingSphere() : m_Radius(0) { }
CBoundingSphere(const CVector3D& center, float radius) : m_Center(center), m_Radius(radius) { }
/**
* Construct a bounding sphere that encompasses a bounding box
* swept through all possible rotations around the origin.
*/
static CBoundingSphere FromSweptBox(const CBoundingBoxAligned& bbox)
{
float maxX = std::max(fabsf(bbox[0].X), fabsf(bbox[1].X));
float maxY = std::max(fabsf(bbox[0].Y), fabsf(bbox[1].Y));
float maxZ = std::max(fabsf(bbox[0].Z), fabsf(bbox[1].Z));
float radius = sqrtf(maxX*maxX + maxY*maxY + maxZ*maxZ);
return CBoundingSphere(CVector3D(0.f, 0.f, 0.f), radius);
}
const CVector3D& GetCenter()
{
return m_Center;
}
float GetRadius() const
{
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