일단 충돌 구현...?

This commit is contained in:
2025-06-19 17:34:34 +09:00
parent dc2c106bad
commit 3e3769f912
8 changed files with 81 additions and 73 deletions

View File

@@ -14,7 +14,8 @@ void BeginPlay(veng::Engine& engine) {
player->position = glm::vec3(jitterDist(gen), jitterDist(gen), 0.f);
player->scale = glm::vec3(.02f);
player->colision = true;
player->OnColision = [](std::shared_ptr<veng::Model> self,
player->OnColision = [](utils::ThreadPool* thread_pool,
std::shared_ptr<veng::Model> self,
std::shared_ptr<veng::Model> other) {
auto self_owner = self->owner.lock();
auto other_owner = other->owner.lock();
@@ -51,6 +52,27 @@ void BeginPlay(veng::Engine& engine) {
std::lock_guard lock(other_player->modding);
other_player->position = glm::vec3(1.f, 0.f, 0.f);
other_player->scale = glm::vec3(.02f);
player->colision = true;
player->OnColision = [](utils::ThreadPool* thread_pool,
std::shared_ptr<veng::Model> self,
std::shared_ptr<veng::Model> other) {
auto self_owner = self->owner.lock();
auto other_owner = other->owner.lock();
if (other_owner == nullptr || self_owner == nullptr) return;
if (other_owner->ID == self->ID) return;
spdlog::info("[{}:{}] and [{}:{}] is nearby.", self->name,
self->ID.snowflake, other->name, other->ID.snowflake);
spdlog::info("[{}:{}] 's owner: [{}:{}]", self->name, self->ID.snowflake,
self_owner->name, self_owner->ID.snowflake);
spdlog::info("[{}:{}] 's owner: [{}:{}]", other->name,
other->ID.snowflake, other_owner->name,
other_owner->ID.snowflake);
other->colision = false;
other->visible = false;
other->shouldBeDestroyed = true;
};
other_player->colision = true;
}

View File

@@ -86,8 +86,9 @@ void Tick(veng::Engine& engine, std::float_t delta_time) {
bullet->owner = player;
bullet->scale = player->scale;
bullet->colision = true;
bullet->OnColision = [](std::shared_ptr<veng::Model> self,
std::shared_ptr<veng::Model> other) {
bullet->OnColision = [](utils::ThreadPool* thread_pool,
std::shared_ptr<veng::Model> self,
std::shared_ptr<veng::Model> other) {
auto self_owner = self->owner.lock();
auto other_owner = other->owner.lock();
if (other_owner == nullptr || self_owner == nullptr) return;
@@ -112,30 +113,35 @@ void Tick(veng::Engine& engine, std::float_t delta_time) {
}
static bool ad_laststate = false;
right = player->rotation * glm::vec3(0, 0, 1);
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_A) ==
GLFW_PRESS) {
right = player->rotation * glm::vec3(0, 0, 1);
player->angular_velocity = right * 6.f;
if (!ad_laststate) {
player->needsUpdate = true;
player->updateRedundantCount = 0;
player->updateRedundantCount = 1;
ad_laststate = true;
}
} else if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(),
}
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(),
GLFW_KEY_D) == GLFW_PRESS) {
right = player->rotation * glm::vec3(0, 0, 1);
player->angular_velocity = right * -6.f;
if (!ad_laststate) {
player->needsUpdate = true;
player->updateRedundantCount = 0;
player->updateRedundantCount = 1;
ad_laststate = true;
}
} else {
right = player->rotation * glm::vec3(0, 0, 1);
}
if (glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_A) ==
GLFW_RELEASE &&
glfwGetKey(engine.vulkan_graphics->window->GetHandle(), GLFW_KEY_D) ==
GLFW_RELEASE) {
player->angular_velocity = right * 0.f;
if (ad_laststate) {
player->needsUpdate = true;
player->updateRedundantCount = 0;
player->updateRedundantCount = 2;
ad_laststate = false;
}
}

View File

@@ -13,7 +13,7 @@
#define LISTENIP "::"
#define LISTENPORT 9010
#define TICKRATE 60
#define TICKRATE 30
constexpr double TICKDELAY = (1 / TICKRATE);
std::uint8_t CLIENTID = 0;

View File

@@ -20,15 +20,16 @@ void Engine::init() {
}
void Engine::LoadModelAsset(std::string path, std::string name) {
veng::Model model(vulkan_graphics);
model_assets_[name] = veng::Model(vulkan_graphics);
asset_loader_.setPath(path);
asset_loader_.loadModel(model);
asset_loader_.loadModel(model_assets_[name]);
model.vertex_buffer = vulkan_graphics->CreateVertexBuffer(model.vertices);
model.index_buffer = vulkan_graphics->CreateIndexBuffer(model.indices);
model.material.texture_handle =
model_assets_[name].vertex_buffer =
vulkan_graphics->CreateVertexBuffer(model_assets_[name].vertices);
model_assets_[name].index_buffer =
vulkan_graphics->CreateIndexBuffer(model_assets_[name].indices);
model_assets_[name].material.texture_handle =
vulkan_graphics->CreateTexture(asset_loader_.readTexture());
model_assets_[name] = std::move(model);
}
const Model* Engine::GetStaticModel(std::string name) {
@@ -117,15 +118,11 @@ void Engine::Update() {
if (Tick != nullptr) Tick(*this, delta_time);
std::vector<std::shared_ptr<Model>> models;
models.reserve(models_.size());
for (auto it = models_.begin(); it != models_.end();) {
auto& model = it->second;
if (std::abs(model->lifespan + 1.f) <
std::numeric_limits<float>::epsilon()) {
models.push_back(model);
++it;
continue;
}
@@ -141,8 +138,6 @@ void Engine::Update() {
}
model->lifespan -= delta_time;
models.push_back(model);
++it;
}
@@ -151,7 +146,7 @@ void Engine::Update() {
vulkan_graphics->RenderModel(it.second);
}
physics_controller_.invokeOnColisionEvent(thread_pool_, models);
physics_controller_.invokeOnColisionEvent(thread_pool_, models_);
vulkan_graphics->EndFrame();
}
@@ -176,11 +171,10 @@ void Engine::NetworkUpload(std::shared_ptr<Network::Socket> sock) {
data.insert(data.end(), header_serialized.begin(), header_serialized.end());
data.insert(data.end(), model.begin(), model.end());
if (it.second->updateRedundantCount >= 2) {
if (it.second->updateRedundantCount <= 1) {
it.second->needsUpdate = false;
it.second->updateRedundantCount = 0;
} else {
it.second->updateRedundantCount++;
it.second->updateRedundantCount--;
}
spdlog::debug("{} uploaded", it.second->name);
}
@@ -188,7 +182,7 @@ void Engine::NetworkUpload(std::shared_ptr<Network::Socket> sock) {
}
float GetAlpha(double old_time, double new_time) {
return (glfwGetTime() - old_time) / (new_time - old_time);
return (glfwGetTime() - old_time) / (new_time - old_time) * 0.5;
}
void Interpolation(Packet::Header header, std::shared_ptr<veng::Model> local,
@@ -256,17 +250,8 @@ void Engine::ResponseToServerAndRefresh(std::shared_ptr<Network::Socket> sock) {
auto spawnedModel =
SpawnLifedModel(model->asset_name, model->name, model->lifespan);
std::lock_guard lock(spawnedModel->modding);
spawnedModel->ID = model->ID;
spawnedModel->position = model->position;
spawnedModel->linear_velocity = model->linear_velocity;
spawnedModel->linear_acceleration = model->linear_acceleration;
spawnedModel->rotation = model->rotation;
spawnedModel->angular_velocity = model->angular_velocity;
spawnedModel->angular_acceleration = model->angular_acceleration;
spawnedModel->scale = model->scale;
spawnedModel->transform = model->transform;
spawnedModel->original_offset = model->original_offset;
spawnedModel->radius = model->radius;
@@ -275,6 +260,8 @@ void Engine::ResponseToServerAndRefresh(std::shared_ptr<Network::Socket> sock) {
spawnedModel->visible = model->visible;
spawnedModel->colision = model->colision;
spawnedModel->networkReplicated = false;
Interpolation(header, spawnedModel, model);
spdlog::info("model spawned: {}", model->ID.snowflake);
}
} break;

View File

@@ -5,37 +5,27 @@
namespace veng {
void Physics::invokeOnColisionEvent(
gsl::not_null<utils::ThreadPool*> thread_pool, std::vector<std::shared_ptr<Model>> models) {
gsl::not_null<utils::ThreadPool*> thread_pool,
std::unordered_map<std::string, std::shared_ptr<Model>>& models) {
constexpr std::float_t EPSILON = std::numeric_limits<std::float_t>::epsilon();
for (int first = 0; first < models.size(); first++) {
auto first_model = models[first];
if (first_model != nullptr || !first_model->colision) continue;
for (int second = first + 1; second < models.size(); second++) {
auto second_model = models[second];
if (second_model != nullptr || !second_model->colision) continue;
for (std::unordered_map<std::string, std::shared_ptr<Model>>::iterator
iter_A = models.begin();
iter_A != models.end(); iter_A++) {
auto model_A = iter_A->second;
if (!model_A->colision) continue;
for (auto iter_B = std::next(iter_A); iter_B != models.end(); iter_B++) {
auto model_B = iter_B->second;
if (!model_B->colision) continue;
std::float_t distance =
glm::distance(first_model->position, second_model->position);
std::float_t model1_radius = first_model->radius * first_model->scale.x;
std::float_t model2_radius = second_model->radius * second_model->scale.x;
if (distance <= model1_radius + model2_radius) {
if (first_model->OnColision != nullptr)
thread_pool->enqueueJob(
[OnColision = first_model->OnColision](
utils::ThreadPool* thread_pool, std::shared_ptr<Model> self,
std::shared_ptr<Model> other) {
OnColision(self, other);
},
first_model, second_model);
if (second_model->OnColision != nullptr)
thread_pool->enqueueJob(
[OnColision = second_model->OnColision](
utils::ThreadPool* thread_pool, std::shared_ptr<Model> self,
std::shared_ptr<Model> other) {
OnColision(self, other);
},
first_model, second_model);
break;
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, model_A, model_B);
if (model_B->OnColision)
thread_pool->enqueueJob(model_B->OnColision, model_A, model_B);
}
}
}

View File

@@ -51,8 +51,8 @@ class ThreadPool {
}
std::lock_guard<std::mutex> lock(jobQueueMutex);
auto boundFunc = [this, __job, ... __args = std::move(__args)]() mutable {
__job(this, std::move(__args)...);
auto boundFunc = [this, __job, ... __args = __args]() mutable {
__job(this, __args...);
};
auto task = std::packaged_task<void()>(std::move(boundFunc));
jobs_.push(std::move(task));

View File

@@ -8,6 +8,7 @@
#include "utils/snowflake.h"
#include "vulkan_engine/vulkan/buffer_handle.h"
#include "vulkan_engine/vulkan/vertex.h"
#include "utils/thread_pool.h"
extern std::uint8_t CLIENTID;
@@ -152,7 +153,8 @@ struct Model {
std::float_t lifespan = -1.f;
std::function<void(std::shared_ptr<Model> self, std::shared_ptr<Model> other)>
std::function<void(utils::ThreadPool* thread_pool, std::shared_ptr<Model> self,
std::shared_ptr<Model> other)>
OnColision = nullptr;
bool visible = true;

View File

@@ -1,14 +1,15 @@
#pragma once
#include "utils/thread_pool.h"
#include "vulkan_engine/asset/object/model.h"
#include "vulkan_engine/vulkan/vertex.h"
#include "utils/thread_pool.h"
namespace veng {
class Physics {
public:
void invokeOnColisionEvent(gsl::not_null<utils::ThreadPool*> thread_pool,
std::vector<std::shared_ptr<Model>> models);
void invokeOnColisionEvent(
gsl::not_null<utils::ThreadPool*> thread_pool,
std::unordered_map<std::string, std::shared_ptr<Model>>& models);
bool RayTrace(const glm::vec3& rayOrigin, const glm::vec3& rayDir,
const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2,