From 9a1b193be91aa603e55982e522829405719e5f49 Mon Sep 17 00:00:00 2001 From: Pedro Gimeno Date: Thu, 20 Feb 2020 22:48:14 +0100 Subject: [PATCH] Use Z for up (Part 3) Fix camera, player movement, and bounding boxes. --- client/include/world/ClientPlayer.hpp | 4 +- client/source/states/GameState.cpp | 5 +- client/source/world/ClientPlayer.cpp | 84 +++++++++++++-------------- common/source/world/Player.cpp | 2 +- mods/default/blocks.lua | 2 +- 5 files changed, 49 insertions(+), 48 deletions(-) diff --git a/client/include/world/ClientPlayer.hpp b/client/include/world/ClientPlayer.hpp index 848e7b16..751dbca0 100644 --- a/client/include/world/ClientPlayer.hpp +++ b/client/include/world/ClientPlayer.hpp @@ -56,8 +56,8 @@ class ClientPlayer : public Player { void checkCollisions(const ClientWorld &world); float pointTargetedX() const { return m_x + cos(m_angleH * RADIANS_PER_DEGREES) * cos(m_angleV * RADIANS_PER_DEGREES); } - float pointTargetedY() const { return m_y + sin(m_angleV * RADIANS_PER_DEGREES); } - float pointTargetedZ() const { return m_z + sin(m_angleH * RADIANS_PER_DEGREES) * cos(m_angleV * RADIANS_PER_DEGREES) - 0.00001; } + float pointTargetedY() const { return m_y + sin(m_angleH * RADIANS_PER_DEGREES) * cos(m_angleV * RADIANS_PER_DEGREES) - 0.00001; } + float pointTargetedZ() const { return m_z + sin(m_angleV * RADIANS_PER_DEGREES); } static ClientPlayer &getInstance() { return *s_instance; } static void setInstance(ClientPlayer *instance) { s_instance = instance; } diff --git a/client/source/states/GameState.cpp b/client/source/states/GameState.cpp index 6d2cbca1..05394a13 100644 --- a/client/source/states/GameState.cpp +++ b/client/source/states/GameState.cpp @@ -46,6 +46,7 @@ GameState::GameState(const std::string &host, int port) { m_textureAtlas = &gk::ResourceHandler::getInstance().get("atlas-blocks"); + m_camera.setUpVector(gk::Vector3d{0., 0., 1.}); m_camera.setAspectRatio((float)Config::screenWidth / Config::screenHeight); initShaders(); @@ -68,8 +69,8 @@ void GameState::onEvent(const SDL_Event &event) { if (&m_stateStack->top() == this) { if (event.type == SDL_MOUSEMOTION) { if(Config::screenWidth / 2.0f != event.motion.x || Config::screenHeight / 2.0f != event.motion.y) { - m_player.turnH(event.motion.xrel * 0.01 * Config::mouseSensitivity); - m_player.turnV(-event.motion.yrel * 0.01 * Config::mouseSensitivity); + m_player.turnH(event.motion.xrel * -0.01 * Config::mouseSensitivity); + m_player.turnV(event.motion.yrel * -0.01 * Config::mouseSensitivity); gk::Mouse::resetToWindowCenter(); } diff --git a/client/source/world/ClientPlayer.cpp b/client/source/world/ClientPlayer.cpp index 7f397ba9..da8186af 100644 --- a/client/source/world/ClientPlayer.cpp +++ b/client/source/world/ClientPlayer.cpp @@ -35,13 +35,13 @@ ClientPlayer *ClientPlayer::s_instance = nullptr; ClientPlayer::ClientPlayer(gk::Camera &camera) : m_camera(camera) { // FIXME: Warning: Duplicated in ServerCommandHandler.hpp m_x = 12; - m_y = 20; - m_z = 12; + m_y = 12; + m_z = 20; m_angleH = -90.0; m_angleV = 0.01; - m_camera.setPosition(m_x, m_y - 0.1, m_z); + m_camera.setPosition(m_x, m_y, m_z - 0.1); m_camera.setTargetPosition(pointTargetedX(), pointTargetedY(), pointTargetedZ()); } @@ -74,92 +74,92 @@ void ClientPlayer::turnV(float angle) { void ClientPlayer::move(float direction) { direction += m_angleH; - m_velocity.x = 0.04f * cos(direction * RADIANS_PER_DEGREES); - m_velocity.z = 0.04f * sin(direction * RADIANS_PER_DEGREES); + m_velocity.x = 0.04f * cosf(direction * RADIANS_PER_DEGREES); + m_velocity.y = 0.04f * sinf(direction * RADIANS_PER_DEGREES); } void ClientPlayer::processInputs() { if(gk::GamePad::isKeyPressed(GameKey::Jump) && !m_isJumping) { m_isJumping = true; - m_velocity.y = m_jumpSpeed; + m_velocity.z = m_jumpSpeed; } if(gk::GamePad::isKeyPressed(GameKey::Fly)) { - m_velocity.y = 0.1; + m_velocity.z = 0.1; } if(gk::GamePad::isKeyPressed(GameKey::Sneak)) { - m_velocity.y = -0.1; + m_velocity.z = -0.1; } if(gk::GamePad::isKeyPressed(GameKey::Up)) move(0.0f); else if(gk::GamePad::isKeyPressed(GameKey::Down)) move(180.0f); - if(gk::GamePad::isKeyPressed(GameKey::Left)) move(-90.0f); - else if(gk::GamePad::isKeyPressed(GameKey::Right)) move(90.0f); + if(gk::GamePad::isKeyPressed(GameKey::Left)) move(90.0f); + else if(gk::GamePad::isKeyPressed(GameKey::Right)) move(-90.0f); - if (gk::GamePad::isKeyPressed(GameKey::Left) && gk::GamePad::isKeyPressed(GameKey::Up)) move(-45.0f); - if (gk::GamePad::isKeyPressed(GameKey::Right) && gk::GamePad::isKeyPressed(GameKey::Up)) move(45.0f); - if (gk::GamePad::isKeyPressed(GameKey::Left) && gk::GamePad::isKeyPressed(GameKey::Down)) move(-135.0f); - if (gk::GamePad::isKeyPressed(GameKey::Right) && gk::GamePad::isKeyPressed(GameKey::Down)) move(135.0f); + if (gk::GamePad::isKeyPressed(GameKey::Left) && gk::GamePad::isKeyPressed(GameKey::Up)) move(45.0f); + if (gk::GamePad::isKeyPressed(GameKey::Right) && gk::GamePad::isKeyPressed(GameKey::Up)) move(-45.0f); + if (gk::GamePad::isKeyPressed(GameKey::Left) && gk::GamePad::isKeyPressed(GameKey::Down)) move(135.0f); + if (gk::GamePad::isKeyPressed(GameKey::Right) && gk::GamePad::isKeyPressed(GameKey::Down)) move(-135.0f); if (gk::GamePad::isKeyPressed(GameKey::Sprint)) { m_velocity.x *= 1.5; - m_velocity.z *= 1.5; + m_velocity.y *= 1.5; } } void ClientPlayer::updatePosition(const ClientWorld &world) { - ClientChunk *chunk = (ClientChunk *)world.getChunkAtBlockPos(m_x, m_y - 0.2, m_z); + ClientChunk *chunk = (ClientChunk *)world.getChunkAtBlockPos(m_x, m_y, m_z - 0.2); if (!Config::isFlyModeEnabled && chunk && chunk->isInitialized()) { - m_velocity.y -= m_gravity; // Gravity + m_velocity.z -= m_gravity; // Gravity - if (m_velocity.y < -m_jumpSpeed) // Jump max accel - m_velocity.y = -m_jumpSpeed; + if (m_velocity.z < -m_jumpSpeed) // Jump max accel + m_velocity.z = -m_jumpSpeed; } if (!Config::isNoClipEnabled) checkCollisions(world); - if (!Config::isFlyModeEnabled && m_velocity.y != 0) { + if (!Config::isFlyModeEnabled && m_velocity.z != 0) { m_velocity.x *= 0.75f; - m_velocity.z *= 0.75f; + m_velocity.y *= 0.75f; } m_x += m_velocity.x; m_y += m_velocity.y; m_z += m_velocity.z; - m_camera.setPosition(m_x, m_y - 0.1, m_z); + m_camera.setPosition(m_x, m_y, m_z - 0.1); Player::setPosition(m_x, m_y, m_z); if (m_velocity.x != 0 || m_velocity.y != 0 || m_velocity.z != 0) m_camera.setTargetPosition(pointTargetedX(), pointTargetedY(), pointTargetedZ()); m_velocity.x = 0; - m_velocity.z = 0; + m_velocity.y = 0; if (Config::isFlyModeEnabled) - m_velocity.y = 0; + m_velocity.z = 0; } void ClientPlayer::checkCollisions(const ClientWorld &world) { #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || defined(__ANDROID__) const float PLAYER_HEIGHT = 1.8; - float eyeheight = m_y + PLAYER_HEIGHT - 1.4; + float eyeheight = m_z + PLAYER_HEIGHT - 1.4; // testPoint(world, glm::vec3(m_x, m_y, m_z), m_velocity); - testPoint(world, m_x - 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_z - 0.2, m_velocity); - testPoint(world, m_x + 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_z - 0.2, m_velocity); - testPoint(world, m_x - 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_z + 0.2, m_velocity); - testPoint(world, m_x + 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_z + 0.2, m_velocity); - testPoint(world, m_x - 0.2, eyeheight - 0.4, m_z - 0.2, m_velocity); - testPoint(world, m_x + 0.2, eyeheight - 0.4, m_z - 0.2, m_velocity); - testPoint(world, m_x - 0.2, eyeheight - 0.4, m_z + 0.2, m_velocity); - testPoint(world, m_x + 0.2, eyeheight - 0.4, m_z + 0.2, m_velocity); + testPoint(world, m_x - 0.2, m_y - 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_velocity); + testPoint(world, m_x + 0.2, m_y - 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_velocity); + testPoint(world, m_x - 0.2, m_y + 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_velocity); + testPoint(world, m_x + 0.2, m_y + 0.2, eyeheight - PLAYER_HEIGHT - 0.4, m_velocity); + testPoint(world, m_x - 0.2, m_y - 0.2, eyeheight - 0.4, m_velocity); + testPoint(world, m_x + 0.2, m_y - 0.2, eyeheight - 0.4, m_velocity); + testPoint(world, m_x - 0.2, m_y + 0.2, eyeheight - 0.4, m_velocity); + testPoint(world, m_x + 0.2, m_y + 0.2, eyeheight - 0.4, m_velocity); #else - for (float x = m_x + m_hitbox.x ; x <= m_x + m_hitbox.x + m_hitbox.sizeX + 0.1f ; x += 0.2f) { - for (float y = m_y + m_hitbox.y ; y <= m_y + m_hitbox.y + m_hitbox.sizeY + 0.1f ; y += 0.9f) { - for (float z = m_z + m_hitbox.z ; z <= m_z + m_hitbox.z + m_hitbox.sizeZ + 0.1f ; z += 0.2f) { + for (float z = m_z + m_hitbox.z ; z <= m_z + m_hitbox.z + m_hitbox.sizeZ + 0.1f ; z += 0.9f) { + for (float y = m_y + m_hitbox.y ; y <= m_y + m_hitbox.y + m_hitbox.sizeY + 0.1f ; y += 0.2f) { + for (float x = m_x + m_hitbox.x ; x <= m_x + m_hitbox.x + m_hitbox.sizeX + 0.1f ; x += 0.2f) { if (x == m_x + m_hitbox.x || x == m_x + m_hitbox.x + m_hitbox.sizeX || y == m_y + m_hitbox.y || y == m_y + m_hitbox.y + m_hitbox.sizeY || z == m_z + m_hitbox.z || z == m_z + m_hitbox.z + m_hitbox.sizeZ) @@ -178,15 +178,15 @@ bool passable(const ClientWorld &world, float x, float y, float z) { void ClientPlayer::testPoint(const ClientWorld &world, float x, float y, float z, glm::vec3 &speed) { // FIXME: Temporary fix, find the real problem!!! - if (x < 0) --x; + if (x < 1) --x; if (y < 1) --y; - if (z < 0) --z; + if (z < 1) --z; if(!passable(world, x + speed.x, y, z)) speed.x = 0; - if(!passable(world, x, y, z + speed.z)) speed.z = 0; - if(!passable(world, x, y + speed.y, z)) { - if(speed.y < 0 && m_isJumping) m_isJumping = false; - speed.y = 0; + if(!passable(world, x, y + speed.y, z)) speed.y = 0; + if(!passable(world, x, y, z + speed.z)) { + if(speed.z < 0 && m_isJumping) m_isJumping = false; + speed.z = 0; } } diff --git a/common/source/world/Player.cpp b/common/source/world/Player.cpp index 761cc8eb..23474663 100644 --- a/common/source/world/Player.cpp +++ b/common/source/world/Player.cpp @@ -24,7 +24,7 @@ #include "Player.hpp" Player::Player() { - m_hitbox = gk::FloatBox{-0.2, -1.8, -0.2, 0.4, 1.8, 0.4}; + m_hitbox = gk::FloatBox{-0.2, -0.2, -1.8, 0.4, 0.4, 1.8}; } void Player::serialize(sf::Packet &packet) const { diff --git a/mods/default/blocks.lua b/mods/default/blocks.lua index aff46d4a..b538bb69 100644 --- a/mods/default/blocks.lua +++ b/mods/default/blocks.lua @@ -141,7 +141,7 @@ mod:block { tiles = "dandelion.png", hardness = 0.05, draw_type = 1, -- FIXME: Use string instead - bounding_box = {0.25, 0.0, 0.25, 0.5, 0.5, 0.5}, + bounding_box = {0.25, 0.25, 0.0, 0.5, 0.5, 0.5}, } mod:block {