1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-10-06 09:07:19 +02:00

Improve ingame camera with invert y option in configuration.

This commit is contained in:
Daniel Evans 2016-05-22 15:58:36 +01:00
parent 4a2141605d
commit 45abee6093
6 changed files with 44 additions and 9 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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

View File

@ -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;

View File

@ -19,6 +19,7 @@ constexpr float kAutoLookTime = 2.f;
constexpr float kAutolookMinVelocity = 0.2f;
const float kMaxRotationRate = glm::half_pi<float>();
const float kCameraPitchLimit = glm::quarter_pi<float>() * 0.5f;
const float kVehicleCameraPitch = glm::half_pi<float>() - glm::quarter_pi<float>() * 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<float>() }
, 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<float>() - 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<float>();
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<float>();
auto idealYaw = -glm::roll(vehicle->getRotation()) + glm::half_pi<float>();
const auto idealPitch = kVehicleCameraPitch;
if (velocity < 0.f) {
idealAngle = glm::mod(idealAngle - glm::pi<float>(), glm::pi<float>() * 2.f);
idealYaw = glm::mod(idealYaw - glm::pi<float>(), glm::pi<float>() * 2.f);
}
float currentAngle = glm::mod(m_cameraAngles.x, glm::pi<float>()*2);
float rotation = idealAngle - currentAngle;
if (glm::abs(rotation) > glm::pi<float>()) {
rotation -= glm::sign(rotation) * glm::pi<float>()*2.f;
float currentYaw = glm::mod(m_cameraAngles.x, glm::pi<float>()*2);
float currentPitch = m_cameraAngles.y;
float deltaYaw = idealYaw - currentYaw;
float deltaPitch = idealPitch - currentPitch;
if (glm::abs(deltaYaw) > glm::pi<float>()) {
deltaYaw -= glm::sign(deltaYaw) * glm::pi<float>()*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;

View File

@ -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