From 45abee6093fdc016bbb8931f83c5f38774f7bb9d Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Sun, 22 May 2016 15:58:36 +0100 Subject: [PATCH] Improve ingame camera with invert y option in configuration. --- README.md | 3 +++ rwgame/GameConfig.cpp | 5 +++++ rwgame/GameConfig.hpp | 4 ++++ rwgame/RWGame.hpp | 5 +++++ rwgame/ingamestate.cpp | 32 +++++++++++++++++++++++--------- rwgame/ingamestate.hpp | 4 ++++ 6 files changed, 44 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 11abeace..99350fe2 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ the game will look for ``~/.config/OpenRW/openrw.ini``, which should look like: ``` [game] path=/opt/games/Grand Theft Auto 3/ ; Game data path + +[input] +invert_y=0 ; Invert camera Y ``` Eventually the game will write this for you, but currently it must be done by hand. diff --git a/rwgame/GameConfig.cpp b/rwgame/GameConfig.cpp index a51d8a62..d212ba6f 100644 --- a/rwgame/GameConfig.cpp +++ b/rwgame/GameConfig.cpp @@ -11,6 +11,7 @@ GameConfig::GameConfig(const std::string& configName, const std::string& configP : m_configName(configName) , m_configPath(configPath) , m_valid(false) + , m_inputInvertY(false) { if (m_configPath.empty()) { @@ -78,6 +79,10 @@ int GameConfig::handler(void* user, { self->m_gamePath = value; } + else if (MATCH("input", "invert_y")) + { + self->m_inputInvertY = atoi(value) > 0; + } else { RW_MESSAGE("Unhandled config entry [" << section << "] " << name << " = " << value); diff --git a/rwgame/GameConfig.hpp b/rwgame/GameConfig.hpp index 2683ec39..8b2665ec 100644 --- a/rwgame/GameConfig.hpp +++ b/rwgame/GameConfig.hpp @@ -24,6 +24,7 @@ public: bool isValid(); const std::string& getGameDataPath() const { return m_gamePath; } + const bool getInputInvertY() const { return m_inputInvertY; } private: static std::string getDefaultConfigPath(); @@ -38,6 +39,9 @@ private: /// Path to the game data std::string m_gamePath; + + /// Invert the y axis for camera control. + bool m_inputInvertY; }; #endif diff --git a/rwgame/RWGame.hpp b/rwgame/RWGame.hpp index 34e6582b..f43bfe25 100644 --- a/rwgame/RWGame.hpp +++ b/rwgame/RWGame.hpp @@ -83,6 +83,11 @@ public: return script; } + const GameConfig& getConfig() const + { + return config; + } + bool hitWorldRay(glm::vec3 &hit, glm::vec3 &normal, GameObject** object = nullptr) { auto vc = nextCam; diff --git a/rwgame/ingamestate.cpp b/rwgame/ingamestate.cpp index cb0c41fc..e0312e94 100644 --- a/rwgame/ingamestate.cpp +++ b/rwgame/ingamestate.cpp @@ -19,6 +19,7 @@ constexpr float kAutoLookTime = 2.f; constexpr float kAutolookMinVelocity = 0.2f; const float kMaxRotationRate = glm::half_pi(); const float kCameraPitchLimit = glm::quarter_pi() * 0.5f; +const float kVehicleCameraPitch = glm::half_pi() - glm::quarter_pi() * 0.25f; IngameState::IngameState(RWGame* game, bool newgame, const std::string& save) : State(game) @@ -28,6 +29,8 @@ IngameState::IngameState(RWGame* game, bool newgame, const std::string& save) , autolookTimer(0.f) , camMode(IngameState::CAMERA_NORMAL) , m_cameraAngles { 0.f, glm::half_pi() } + , m_invertedY(game->getConfig().getInputInvertY()) + , m_vehicleFreeLook(true) { } @@ -131,6 +134,9 @@ void IngameState::tick(float dt) if(deltaMouse.x != 0 || deltaMouse.y != 0) { autolookTimer = kAutoLookTime; + if (!m_invertedY) { + mouseMove.y = -mouseMove.y; + } m_cameraAngles += glm::vec2(mouseMove.x, mouseMove.y); m_cameraAngles.y = glm::clamp(m_cameraAngles.y, kCameraPitchLimit, glm::pi() - kCameraPitchLimit); } @@ -183,22 +189,30 @@ void IngameState::tick(float dt) lookTargetPosition.z += (vehicle->info->handling.dimensions.z); targetPosition.z += (vehicle->info->handling.dimensions.z * 1.f); physTarget = vehicle->physBody; - m_cameraAngles.y = glm::half_pi(); + + if (!m_vehicleFreeLook) + { + m_cameraAngles.y = kVehicleCameraPitch; + } // Rotate the camera to the ideal angle if the player isn't moving it float velocity = vehicle->getVelocity(); if (autolookTimer <= 0.f && glm::abs(velocity) > kAutolookMinVelocity) { - auto idealAngle = -glm::roll(vehicle->getRotation()) - glm::half_pi(); + auto idealYaw = -glm::roll(vehicle->getRotation()) + glm::half_pi(); + const auto idealPitch = kVehicleCameraPitch; if (velocity < 0.f) { - idealAngle = glm::mod(idealAngle - glm::pi(), glm::pi() * 2.f); + idealYaw = glm::mod(idealYaw - glm::pi(), glm::pi() * 2.f); } - float currentAngle = glm::mod(m_cameraAngles.x, glm::pi()*2); - float rotation = idealAngle - currentAngle; - if (glm::abs(rotation) > glm::pi()) { - rotation -= glm::sign(rotation) * glm::pi()*2.f; + float currentYaw = glm::mod(m_cameraAngles.x, glm::pi()*2); + float currentPitch = m_cameraAngles.y; + float deltaYaw = idealYaw - currentYaw; + float deltaPitch = idealPitch - currentPitch; + if (glm::abs(deltaYaw) > glm::pi()) { + deltaYaw -= glm::sign(deltaYaw) * glm::pi()*2.f; } - m_cameraAngles.x += glm::sign(rotation) * std::min(kMaxRotationRate * dt, glm::abs(rotation)); + m_cameraAngles.x += glm::sign(deltaYaw) * std::min(kMaxRotationRate * dt, glm::abs(deltaYaw)); + m_cameraAngles.y += glm::sign(deltaPitch) * std::min(kMaxRotationRate * dt, glm::abs(deltaPitch)); } } @@ -207,7 +221,7 @@ void IngameState::tick(float dt) { // Determine the "ideal" camera position for the current view angles auto yaw = glm::angleAxis(m_cameraAngles.x, glm::vec3(0.f, 0.f,-1.f)); - auto pitch = glm::angleAxis(m_cameraAngles.y, glm::vec3(0.f,-1.f, 0.f)); + auto pitch = glm::angleAxis(m_cameraAngles.y, glm::vec3(0.f, 1.f, 0.f)); auto cameraOffset = yaw * pitch * glm::vec3(0.f, 0.f, viewDistance); cameraPosition = targetPosition + cameraOffset; diff --git a/rwgame/ingamestate.hpp b/rwgame/ingamestate.hpp index d1ec03b4..938fbd51 100644 --- a/rwgame/ingamestate.hpp +++ b/rwgame/ingamestate.hpp @@ -30,6 +30,10 @@ class IngameState : public State /// Current camera yaw and pitch glm::vec2 m_cameraAngles; + /// Invert Y axis movement + bool m_invertedY; + /// Free look in vehicles. + bool m_vehicleFreeLook; public: /** * @brief IngameState