Код: Выделить всё
struct Player {
float posX, posY, posZ;
float rotX, rotY = 3.23f;
float jumpTimer = 0.0f;
bool isJumping = false;
bool isRunning = false;
bool isCrouching = false;
} player;
< /code>
my Player struct. < /p>
struct AABB {
float minX, minY, minZ;
float maxX, maxY, maxZ;
};
< /code>
Еще одна структура для проверки столкновений. < /p>
void processInput(GLFWwindow *window, float deltaTime) { /// TO-FIX (deltaTime)
if (edit_mode) return;
// FORWARD/BACKWARD MOVEMENT
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
player.posX += speed * sin(player.rotY);
player.posZ += speed * cos(player.rotY);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
player.posX -= speed * sin(player.rotY);
player.posZ -= speed * cos(player.rotY);
}
// LEFT/RIGHT STRAFING
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
player.posX += speed * cos(player.rotY);
player.posZ -= speed * sin(player.rotY);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
player.posX -= speed * cos(player.rotY);
player.posZ += speed * sin(player.rotY);
}
// RUN SPEED
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS && !is_Crouching) {
speed = 0.05f;
player.isRunning = true;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE) {
speed = 0.03f;
player.isRunning = false;
}
// CTRL CROUCH
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS && !player.isJumping) {
speed = 0.01f;
player.isCrouching = true;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_RELEASE) {
player.isCrouching = false;
}
// SPACE JUMP
bool isJumpKeyPressed = (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS);
if (isJumpKeyPressed && player.posY
Это мой код для перемещения позиции игрока.
Я пытаюсь внедрить систему обнаружения столкновений, но никто не работает на мою систему, поэтому мне было интересно, была ли моя система плохой или я просто не нахожу какую -либо хорошую систему обнаружения столкновений. Даже CHATGPT или Deepsek не могут мне помочь. < /P>
AABB computeAABB(const GameObject& obj) {
AABB box;
if (obj.vertices.empty()) return box;
box.minX = box.maxX = obj.vertices[0].x;
box.minY = box.maxY = obj.vertices[0].y;
box.minZ = box.maxZ = obj.vertices[0].z;
for (const auto& vertex : obj.vertices) {
if (vertex.x < box.minX) box.minX = vertex.x;
if (vertex.x > box.maxX) box.maxX = vertex.x;
if (vertex.y < box.minY) box.minY = vertex.y;
if (vertex.y > box.maxY) box.maxY = vertex.y;
if (vertex.z < box.minZ) box.minZ = vertex.z;
if (vertex.z > box.maxZ) box.maxZ = vertex.z;
}
return box;
}
bool isColliding(const AABB& a, const AABB& b) {
return (a.minX = b.minX) &&
(a.minY = b.minY) &&
(a.minZ = b.minZ);
}
void movePlayerIfNoCollision(Player& player, float dx, float dy, float dz, const std::vector& gameObjects) { /// TO-FIX(auto, instead of std::vector)
float newX = player.posX + dx;
float newY = player.posY + dy;
float newZ = player.posZ + dz;
// Create AABB for the player at the new position
float halfSize = 0.5f; // Adjust based on your player size
AABB playerBox;
playerBox.minX = newX - halfSize;
playerBox.maxX = newX + halfSize;
playerBox.maxY = newY - halfSize;
playerBox.maxY = newY + halfSize;
playerBox.maxZ = newZ - halfSize;
playerBox.maxZ = newZ + halfSize;
// Check against all objects
for (const auto& obj : gameObjects) {
AABB objBox = computeAABB(obj);
if (isColliding(playerBox, objBox)) {
return; // Collision detected, cancel move
}
}
// No collision, apply new position
player.posX = newX;
player.posY = newY;
player.posZ = newZ;
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... not-workig