1
0
forked from 0ad/0ad
0ad/source/maths/BoundingSphere.h

60 lines
1.6 KiB
C
Raw Normal View History

/* Copyright (C) 2019 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/Vector3D.h"
class CBoundingBoxAligned;
class CBoundingSphere
{
public:
CBoundingSphere() : m_Radius(0.0f) { }
CBoundingSphere(const CVector3D& center, float radius) : m_Center(center), m_Radius(radius) { }
const CVector3D& GetCenter() const
{
return m_Center;
}
float GetRadius() const
{
return m_Radius;
}
/**
* Construct a bounding sphere that encompasses a bounding box
* swept through all possible rotations around the origin.
*/
static CBoundingSphere FromSweptBox(const CBoundingBoxAligned& bbox);
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-02 00:05:26 +01:00
/**
* Check if the ray, defined by an origin point and a direction unit vector
* interesects with the sphere. The direction should be normalized.
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-02 00:05:26 +01:00
*/
bool RayIntersect(const CVector3D& origin, const CVector3D& dir) const;
private:
CVector3D m_Center;
float m_Radius;
};
#endif // INCLUDED_BOUNDINGSPHERE