서버-사이드 충돌판정 구현 중

This commit is contained in:
2025-06-21 00:36:50 +09:00
parent 66dc91dab3
commit 3a526edcf4
3 changed files with 45 additions and 3 deletions

View File

@@ -10,6 +10,7 @@
#include "utils/snowflake.h" #include "utils/snowflake.h"
#include "utils/utils.h" #include "utils/utils.h"
#include "vulkan_engine/asset/object/model.h" #include "vulkan_engine/asset/object/model.h"
#include "vulkan_engine/vulkan/physics.h"
#define LISTENIP "::" #define LISTENIP "::"
#define LISTENPORT 9010 #define LISTENPORT 9010
@@ -46,9 +47,12 @@ void EchoClient(utils::ThreadPool* tp, Network::IOCP* iocp,
std::lock_guard lock(MODELS_MUTEX); std::lock_guard lock(MODELS_MUTEX);
auto ID = model.ID; auto ID = model.ID;
if (MODELS.find(ID) == MODELS.end()) if (MODELS.find(ID) == MODELS.end()) {
spdlog::info("model received: {}:{}", model.name, ID.snowflake); spdlog::info("model received: {}:{}", model.name, ID.snowflake);
MODELS[ID] = std::move(model); MODELS[ID] = std::move(model);
} else {
MODELS[ID].Deserialize(recv_data);
}
MODELS[ID].needsUpdate = true; MODELS[ID].needsUpdate = true;
} break; } break;
default: default:
@@ -112,6 +116,7 @@ void ClientRefresher(utils::ThreadPool* tp, Network::IOCP* iocp) {
for (auto& model : MODELS) { for (auto& model : MODELS) {
if (model.second.name == "camera_lag") continue; if (model.second.name == "camera_lag") continue;
model.second.Update(delta_time); model.second.Update(delta_time);
veng::Physics::invokeOnColisionEvent(tp, MODELS);
for (auto& client : CLIENTS) { for (auto& client : CLIENTS) {
if (client->sock == 0) continue; if (client->sock == 0) continue;
std::vector<char> send_data; std::vector<char> send_data;

View File

@@ -36,10 +36,44 @@ void Physics::invokeOnColisionEvent(
} }
} }
void Physics::invokeOnColisionEvent(
gsl::not_null<utils::ThreadPool*> thread_pool,
std::unordered_map<utils::Snowflake, Model>& models) {
constexpr std::float_t EPSILON = std::numeric_limits<std::float_t>::epsilon();
for (std::unordered_map<utils::Snowflake, Model>::iterator
iter_A = models.begin();
iter_A != models.end(); iter_A++) {
for (auto iter_B = std::next(iter_A); iter_B != models.end(); iter_B++) {
auto& model_A = iter_A->second;
std::lock_guard<std::mutex> Alock(model_A.modding);
auto& model_B = iter_B->second;
std::lock_guard<std::mutex> Block(model_B.modding);
if (!model_A.colision || !model_B.colision) continue;
std::float_t distance =
glm::distance(model_A.position, model_B.position);
std::float_t modelA_radius = model_A.radius * model_A.scale.x;
std::float_t modelB_radius = model_B.radius * model_B.scale.x;
if (distance <= modelA_radius + modelB_radius) {
if (model_A.OnColision)
thread_pool->enqueueJob(model_A.OnColision,
std::make_shared<Model>(std::move(model_A)),
std::make_shared<Model>(std::move(model_B)));
/*model_A = iter_A->second;
model_B = iter_B->second;*/
if (model_B.OnColision)
thread_pool->enqueueJob(model_B.OnColision,
std::make_shared<Model>(std::move(model_B)),
std::make_shared<Model>(std::move(model_A)));
}
}
}
}
bool Physics::RayTrace(const glm::vec3& rayOrigin, const glm::vec3& rayDir, bool Physics::RayTrace(const glm::vec3& rayOrigin, const glm::vec3& rayDir,
const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v0, const glm::vec3& v1,
const glm::vec3& v2, std::float_t& outDistance) { const glm::vec3& v2, std::float_t& outDistance) {
const std::float_t EPSILON = std::numeric_limits<std::float_t>::epsilon(); constexpr std::float_t EPSILON = std::numeric_limits<std::float_t>::epsilon();
// 삼각형 엣지와 노멀 계산 // 삼각형 엣지와 노멀 계산
glm::vec3 edge1 = v1 - v0; glm::vec3 edge1 = v1 - v0;

View File

@@ -10,6 +10,9 @@ class Physics {
static void invokeOnColisionEvent( static void invokeOnColisionEvent(
gsl::not_null<utils::ThreadPool*> thread_pool, gsl::not_null<utils::ThreadPool*> thread_pool,
std::unordered_map<std::string, std::shared_ptr<Model>>& models); std::unordered_map<std::string, std::shared_ptr<Model>>& models);
static void invokeOnColisionEvent(
gsl::not_null<utils::ThreadPool*> thread_pool,
std::unordered_map<utils::Snowflake, Model>& models);
bool RayTrace(const glm::vec3& rayOrigin, const glm::vec3& rayDir, bool RayTrace(const glm::vec3& rayOrigin, const glm::vec3& rayDir,
const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2,