From 5df93c0fc59a036013c586cf0991db9286b231ce Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 9 Feb 2023 16:01:37 +0800 Subject: [PATCH] 1 --- server/gameserver/mapinstance.cc | 48 ++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/server/gameserver/mapinstance.cc b/server/gameserver/mapinstance.cc index 4551894c..59a2222f 100644 --- a/server/gameserver/mapinstance.cc +++ b/server/gameserver/mapinstance.cc @@ -5,6 +5,8 @@ #include +#include + #include "DetourCommon.h" #include "mapinstance.h" @@ -16,6 +18,8 @@ #include "room.h" #include "entityfactory.h" #include "creature.h" +#include "glmhelper.h" + #include "mt/MetaMgr.h" #include "mt/Param.h" #include "mt/Map.h" @@ -886,15 +890,18 @@ bool MapInstance::SceneRaycast(const glm::vec3& orig, return false; } float nearest_distance = 0.0f; + CellNode* nearest_node = nullptr; glm::vec3 nearest_pt(0.0f, 0.0f, 0.0f); int raycast_index = MapMgr::Instance()->IncCurrRaycastIndex(); float len = 0.0f; + bool result = false; + bool end = false; do { glm::vec3 curr_pos = orig + dir * len; - len += 5.0f; if (len > max_distance) { len = max_distance; curr_pos = orig + dir * len; + end = true; } int grid_id = map_service_->GetGridId(curr_pos.x, curr_pos.z); list_head* grid_list[9]; @@ -908,8 +915,43 @@ bool MapInstance::SceneRaycast(const glm::vec3& orig, CellNode *node, *tmp; list_for_each_entry_safe(node, tmp, head, entry) { + if (node->tri->check_flag != raycast_index) { + glm::vec3 pos; + bool hited = glm::intersectLineTriangle + (orig, + dir, + node->tri->vert0, + node->tri->vert1, + node->tri->vert2, + pos + ); + node->tri->check_flag = raycast_index; + if (hited) { + float distance = GlmHelper::Norm(pos - orig); + if (nearest_node) { + if (distance <= len || nearest_distance <= len) { + result = true; + end = true; + } + if (distance < nearest_distance) { + nearest_node = node; + nearest_distance = distance; + nearest_pt = pos; + } + } else if (distance <= max_distance) { + nearest_node = node; + nearest_distance = distance; + nearest_pt = pos; + } + } + } } } - } while (true); - return false; + len += 5.0f; + } while (!end); + + if (result) { + hit_pos = nearest_pt; + } + return result; }