From c31139fdecb1fe8ec2505344081f6509e8175865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D1=8C=D0=B1=D0=B5=D1=80=D1=82=20=D0=93=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=B5=D0=B2?= Date: Sun, 2 Mar 2025 19:19:15 +0600 Subject: [PATCH] kolliziya dobavlena --- BoundaryBox.h | 20 ++++++++++++++++++++ GameObjectManager.cpp | 26 +++++++++++++++----------- GameObjectManager.h | 2 ++ shaders/fragment.glsl | 8 ++++++++ shaders/vertex.glsl | 14 ++++++++++++++ 5 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 BoundaryBox.h create mode 100644 shaders/fragment.glsl create mode 100644 shaders/vertex.glsl diff --git a/BoundaryBox.h b/BoundaryBox.h new file mode 100644 index 0000000..cede401 --- /dev/null +++ b/BoundaryBox.h @@ -0,0 +1,20 @@ +#pragma once +#include "Math.h" + +namespace ZL { +class BoundaryBox { +public: + BoundaryBox(float width, float height) + : halfWidth(width/2) + , halfHeight(height/2) {} + + bool isInside(const Vector3f& position) const { + return (position.v[0] >= -halfWidth && position.v[0] <= halfWidth && + position.v[2] >= -halfHeight && position.v[2] <= halfHeight); + } + +private: + float halfWidth; + float halfHeight; +}; +} diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index 81e5914..8458d4b 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -399,26 +399,30 @@ void GameObjectManager::updateScene(size_t ms) { Environment::cameraShift.v[2] -= SPEED * ms; }*/ + Vector3f newPosition = Environment::cameraShift; if (Environment::upPressed) { - Environment::cameraShift.v[0] += directionVector.v[0] * ms; - Environment::cameraShift.v[2] += directionVector.v[1] * ms; + newPosition.v[0] += directionVector.v[0] * ms; + newPosition.v[2] += directionVector.v[1] * ms; } if (Environment::downPressed) { - Environment::cameraShift.v[0] -= directionVector.v[0] * ms; - Environment::cameraShift.v[2] -= directionVector.v[1] * ms; + newPosition.v[0] -= directionVector.v[0] * ms; + newPosition.v[2] -= directionVector.v[1] * ms; } if (Environment::rightPressed) { - Environment::cameraShift.v[2] += directionVector.v[0] * ms; - Environment::cameraShift.v[0] -= directionVector.v[1] * ms; + newPosition.v[2] += directionVector.v[0] * ms; + newPosition.v[0] -= directionVector.v[1] * ms; } if (Environment::leftPressed) { - Environment::cameraShift.v[2] -= directionVector.v[0] * ms; - Environment::cameraShift.v[0] += directionVector.v[1] * ms; + newPosition.v[2] -= directionVector.v[0] * ms; + newPosition.v[0] += directionVector.v[1] * ms; } - Environment::characterPos.v[0] = -Environment::cameraShift.v[0]; - Environment::characterPos.v[1] = -Environment::cameraShift.v[1]; - Environment::characterPos.v[2] = -Environment::cameraShift.v[2]; + Vector3f characterNewPos{-newPosition.v[0], -newPosition.v[1], -newPosition.v[2]}; + // Проверяем, что новая позиция внутри разрешенной зоны + if (walkArea.isInside(characterNewPos)) { + Environment::cameraShift = newPosition; + Environment::characterPos = characterNewPos; + } for (auto& [key, obj] : aoMgr.activeObjectsEntities) { float dist = sqrtf( diff --git a/GameObjectManager.h b/GameObjectManager.h index cae553e..d160bbf 100644 --- a/GameObjectManager.h +++ b/GameObjectManager.h @@ -10,6 +10,7 @@ #include #endif #include "OpenGlExtensions.h" +#include "BoundaryBox.h" // Добавляем новый include namespace ZL { @@ -67,6 +68,7 @@ private: Matrix4f projectionModelView, int screenWidth, int screenHeight, int& screenX, int& screenY); + BoundaryBox walkArea{800.0f, 800.0f}; // Изменяем размер с 400 на 800 }; } // namespace ZL diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl new file mode 100644 index 0000000..06e764e --- /dev/null +++ b/shaders/fragment.glsl @@ -0,0 +1,8 @@ +#version 130 + +in vec2 texCoord; +uniform sampler2D textureSampler; + +void main() { + gl_FragColor = texture2D(textureSampler, texCoord); +} diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl new file mode 100644 index 0000000..23d289a --- /dev/null +++ b/shaders/vertex.glsl @@ -0,0 +1,14 @@ +#version 130 + +in vec3 position; +in vec2 texcoord; + +uniform mat4 projection; +uniform mat4 modelView; + +out vec2 texCoord; + +void main() { + gl_Position = projection * modelView * vec4(position, 1.0); + texCoord = texcoord; +}