refactor: Return normal, not squared distances from public functions

This is to reduce ambiguity at the cost of performance.
This commit is contained in:
Vsevolod Kremianskii 2020-11-15 23:43:08 +07:00
parent 117012b750
commit 9d6b107fb5
12 changed files with 15 additions and 24 deletions

View file

@ -40,7 +40,7 @@ namespace reone {
namespace game { namespace game {
static const float kKeepPathDuration = 1000.0f; static const float kKeepPathDuration = 1000.0f;
static const float kMaxConversationDistance = 8.0f; static const float kMaxConversationDistance = 3.0f;
ActionExecutor::ActionExecutor(Game *game) : _game(game) { ActionExecutor::ActionExecutor(Game *game) : _game(game) {
if (!game) { if (!game) {
@ -142,7 +142,7 @@ bool ActionExecutor::navigateCreature(Creature &creature, const glm::vec3 &dest,
const glm::vec3 &origin = creature.position(); const glm::vec3 &origin = creature.position();
float distToDest = glm::distance2(origin, dest); float distToDest = glm::distance2(origin, dest);
if (distToDest <= distance) { if (distToDest <= distance * distance) {
creature.setMovementType(Creature::MovementType::None); creature.setMovementType(Creature::MovementType::None);
creature.clearPath(); creature.clearPath();
return true; return true;

View file

@ -73,7 +73,7 @@ bool CollisionDetector::rayTestObjects(const RaycastProperties &props, RaycastRe
if (props.flags & kRaycastSelectable && !object->isSelectable()) continue; if (props.flags & kRaycastSelectable && !object->isSelectable()) continue;
float dist = object->distanceTo(glm::vec2(props.origin)); float dist = object->distanceTo(glm::vec2(props.origin));
if (dist > props.maxDistance * props.maxDistance) continue; if (dist > props.maxDistance) continue;
invTransform = glm::inverse(object->transform()); invTransform = glm::inverse(object->transform());
origin = invTransform * glm::vec4(props.origin, 1.0f); origin = invTransform * glm::vec4(props.origin, 1.0f);

View file

@ -100,7 +100,9 @@ bool SelectionOverlay::handleMouseMotion(const SDL_MouseMotionEvent &event) {
} }
bool SelectionOverlay::handleMouseButtonDown(const SDL_MouseButtonEvent &event) { bool SelectionOverlay::handleMouseButtonDown(const SDL_MouseButtonEvent &event) {
if (event.clicks > 1 || _selectedActionIdx == -1) return false; if (event.clicks > 1 ||
_selectedActionIdx == -1 |
_selectedActionIdx >= _actions.size()) return false;
shared_ptr<Area> area(_game->module()->area()); shared_ptr<Area> area(_game->module()->area());
ObjectSelector &selector = area->objectSelector(); ObjectSelector &selector = area->objectSelector();

View file

@ -57,9 +57,7 @@ namespace reone {
namespace game { namespace game {
static const float kDefaultFieldOfView = 75.0f; static const float kDefaultFieldOfView = 75.0f;
static const float kDrawDebugDistance = 64.0f; static const float kMaxDistanceToTestCollision = 8.0f;
static const float kPartyMemberFollowDistance = 4.0f;
static const float kMaxDistanceToTestCollision = 64.0f;
static const float kElevationTestZ = 1024.0f; static const float kElevationTestZ = 1024.0f;
static const float kCreatureObstacleTestZ = 0.1f; static const float kCreatureObstacleTestZ = 0.1f;
static const int kMaxSoundCount = 4; static const int kMaxSoundCount = 4;
@ -596,7 +594,7 @@ shared_ptr<SpatialObject> Area::getObjectAt(int x, int y) const {
props.direction = glm::normalize(toWorld - fromWorld); props.direction = glm::normalize(toWorld - fromWorld);
props.objectTypes = { ObjectType::Creature, ObjectType::Door, ObjectType::Placeable }; props.objectTypes = { ObjectType::Creature, ObjectType::Door, ObjectType::Placeable };
props.except = partyLeader.get(); props.except = partyLeader.get();
props.maxDistance = 1000.0f; props.maxDistance = 2.0f * kSelectionDistance;
RaycastResult result; RaycastResult result;
@ -746,7 +744,7 @@ void Area::updateSounds() {
float minDist = blueprint->minDistance(); float minDist = blueprint->minDistance();
float dist = soundPtr->distanceTo(cameraPosition); float dist = soundPtr->distanceTo(cameraPosition);
if (dist < minDist * minDist || dist > maxDist * maxDist) continue; if (dist < minDist || dist > maxDist) continue;
soundDistances.push_back(make_pair(soundPtr, dist)); soundDistances.push_back(make_pair(soundPtr, dist));
} }

View file

@ -41,11 +41,11 @@ void SpatialObject::addItem(const shared_ptr<Item> &item) {
} }
float SpatialObject::distanceTo(const glm::vec2 &point) const { float SpatialObject::distanceTo(const glm::vec2 &point) const {
return glm::distance2(glm::vec2(_position), point); return glm::distance(glm::vec2(_position), point);
} }
float SpatialObject::distanceTo(const glm::vec3 &point) const { float SpatialObject::distanceTo(const glm::vec3 &point) const {
return glm::distance2(_position, point); return glm::distance(_position, point);
} }
bool SpatialObject::contains(const glm::vec3 &point) const { bool SpatialObject::contains(const glm::vec3 &point) const {

View file

@ -33,8 +33,6 @@ namespace reone {
namespace game { namespace game {
static const float kSelectionDistance = 64.0f;
ObjectSelector::ObjectSelector(const Area *area, const Party *party) : ObjectSelector::ObjectSelector(const Area *area, const Party *party) :
_area(area), _party(party) { _area(area), _party(party) {

View file

@ -24,6 +24,8 @@ namespace reone {
namespace game { namespace game {
const float kSelectionDistance = 8.0f;
class Area; class Area;
class Party; class Party;

View file

@ -40,9 +40,7 @@ void Pathfinder::load(const vector<Path::Point> &points, const unordered_map<int
for (auto &adjPointIdx : point.adjPoints) { for (auto &adjPointIdx : point.adjPoints) {
const Path::Point &adjPoint = points[adjPointIdx]; const Path::Point &adjPoint = points[adjPointIdx];
adjPointVec = glm::vec3(adjPoint.x, adjPoint.y, pointZ.find(adjPointIdx)->second); adjPointVec = glm::vec3(adjPoint.x, adjPoint.y, pointZ.find(adjPointIdx)->second);
float distance = glm::distance2(pointVec, adjPointVec); float distance = glm::distance2(pointVec, adjPointVec);
_edges[i].push_back({ static_cast<uint16_t>(adjPointIdx), distance }); _edges[i].push_back({ static_cast<uint16_t>(adjPointIdx), distance });
} }
} }

View file

@ -128,11 +128,6 @@ void ModelNodeSceneNode::renderSingle() const {
mesh->render(_modelSceneNode->textureOverride()); mesh->render(_modelSceneNode->textureOverride());
} }
float ModelNodeSceneNode::getDistanceFromCenter(const glm::vec3 &point) const {
glm::vec3 center(_absoluteTransform * glm::vec4(_modelNode->getCenterOfAABB(), 1.0f));
return glm::distance2(center, point);
}
const ModelSceneNode *ModelNodeSceneNode::modelSceneNode() const { const ModelSceneNode *ModelNodeSceneNode::modelSceneNode() const {
return _modelSceneNode; return _modelSceneNode;
} }

View file

@ -38,8 +38,6 @@ public:
void renderSingle() const; void renderSingle() const;
float getDistanceFromCenter(const glm::vec3 &point) const;
bool shouldRender() const; bool shouldRender() const;
bool isTransparent() const; bool isTransparent() const;

View file

@ -156,7 +156,7 @@ void SceneGraph::getLightsAt(const glm::vec3 &position, vector<LightSceneNode *>
for (auto &light : _lights) { for (auto &light : _lights) {
float distance = light->distanceTo(position); float distance = light->distanceTo(position);
float radius = light->radius(); float radius = light->radius();
if (distance > radius * radius) continue; if (distance > radius) continue;
distances.insert(make_pair(light, distance)); distances.insert(make_pair(light, distance));
lights.push_back(light); lights.push_back(light);

View file

@ -56,7 +56,7 @@ void SceneNode::render() const {
} }
float SceneNode::distanceTo(const glm::vec3 &point) const { float SceneNode::distanceTo(const glm::vec3 &point) const {
return glm::distance2(glm::vec3(_absoluteTransform[3]), point); return glm::distance(glm::vec3(_absoluteTransform[3]), point);
} }
const SceneNode *SceneNode::parent() const { const SceneNode *SceneNode::parent() const {