mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-22 10:22:52 +01:00
Overhaul ingame camera implementation, much better now.
This commit is contained in:
parent
7a987ea1b6
commit
39c639fc60
@ -81,6 +81,8 @@ public:
|
|||||||
void tickPhysics(float dt);
|
void tickPhysics(float dt);
|
||||||
|
|
||||||
bool isFlipped() const;
|
bool isFlipped() const;
|
||||||
|
|
||||||
|
float getVelocity() const;
|
||||||
|
|
||||||
void ejectAll();
|
void ejectAll();
|
||||||
|
|
||||||
|
@ -365,6 +365,14 @@ bool VehicleObject::isFlipped() const
|
|||||||
return up.z <= -0.1f;
|
return up.z <= -0.1f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float VehicleObject::getVelocity() const
|
||||||
|
{
|
||||||
|
if (physVehicle) {
|
||||||
|
return (physVehicle->getCurrentSpeedKmHour()*1000.f)/(60.f*60.f);
|
||||||
|
}
|
||||||
|
return 0.f;
|
||||||
|
}
|
||||||
|
|
||||||
void VehicleObject::setSteeringAngle(float a)
|
void VehicleObject::setSteeringAngle(float a)
|
||||||
{
|
{
|
||||||
steerAngle = a;
|
steerAngle = a;
|
||||||
|
@ -15,11 +15,19 @@
|
|||||||
#include <script/ScriptMachine.hpp>
|
#include <script/ScriptMachine.hpp>
|
||||||
#include <dynamics/RaycastCallbacks.hpp>
|
#include <dynamics/RaycastCallbacks.hpp>
|
||||||
|
|
||||||
#define AUTOLOOK_TIME 2.f
|
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;
|
||||||
|
|
||||||
IngameState::IngameState(RWGame* game, bool newgame, const std::string& save)
|
IngameState::IngameState(RWGame* game, bool newgame, const std::string& save)
|
||||||
: State(game), started(false), newgame(newgame), save(save),
|
: State(game)
|
||||||
autolookTimer(0.f), camMode(IngameState::CAMERA_NORMAL)
|
, started(false)
|
||||||
|
, save(save)
|
||||||
|
, newgame(newgame)
|
||||||
|
, autolookTimer(0.f)
|
||||||
|
, camMode(IngameState::CAMERA_NORMAL)
|
||||||
|
, m_cameraAngles { 0.f, glm::half_pi<float>() }
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,20 +116,24 @@ void IngameState::tick(float dt)
|
|||||||
autolookTimer = std::max(autolookTimer - dt, 0.f);
|
autolookTimer = std::max(autolookTimer - dt, 0.f);
|
||||||
|
|
||||||
auto player = game->getPlayer();
|
auto player = game->getPlayer();
|
||||||
if( player && player->isInputEnabled() && game->hasFocus() )
|
if( player && player->isInputEnabled() )
|
||||||
{
|
{
|
||||||
float qpi = glm::half_pi<float>();
|
|
||||||
|
|
||||||
sf::Vector2f screenSize(getWindow().getSize());
|
sf::Vector2f screenSize(getWindow().getSize());
|
||||||
sf::Vector2f screenCenter(screenSize / 2.f);
|
sf::Vector2f screenCenter(screenSize / 2.f);
|
||||||
sf::Vector2f mousePos(sf::Mouse::getPosition(getWindow()));
|
sf::Vector2f mouseMove;
|
||||||
sf::Vector2f deltaMouse = (mousePos - screenCenter);
|
if (game->hasFocus())
|
||||||
sf::Vector2f mouseMove(deltaMouse.x / screenSize.x, deltaMouse.y / screenSize.y);
|
|
||||||
sf::Mouse::setPosition(sf::Vector2i(screenCenter), getWindow());
|
|
||||||
|
|
||||||
if(deltaMouse.x != 0 || deltaMouse.y != 0)
|
|
||||||
{
|
{
|
||||||
autolookTimer = AUTOLOOK_TIME;
|
sf::Vector2f mousePos(sf::Mouse::getPosition(getWindow()));
|
||||||
|
sf::Vector2f deltaMouse = (mousePos - screenCenter);
|
||||||
|
mouseMove = sf::Vector2f(deltaMouse.x / screenSize.x, deltaMouse.y / screenSize.y);
|
||||||
|
sf::Mouse::setPosition(sf::Vector2i(screenCenter), getWindow());
|
||||||
|
|
||||||
|
if(deltaMouse.x != 0 || deltaMouse.y != 0)
|
||||||
|
{
|
||||||
|
autolookTimer = kAutoLookTime;
|
||||||
|
m_cameraAngles += glm::vec2(mouseMove.x, mouseMove.y);
|
||||||
|
m_cameraAngles.y = glm::clamp(m_cameraAngles.y, kCameraPitchLimit, glm::pi<float>() - kCameraPitchLimit);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float viewDistance = 4.f;
|
float viewDistance = 4.f;
|
||||||
@ -169,20 +181,36 @@ void IngameState::tick(float dt)
|
|||||||
targetPosition = vehicle->getPosition();
|
targetPosition = vehicle->getPosition();
|
||||||
lookTargetPosition = targetPosition;
|
lookTargetPosition = targetPosition;
|
||||||
lookTargetPosition.z += (vehicle->info->handling.dimensions.z);
|
lookTargetPosition.z += (vehicle->info->handling.dimensions.z);
|
||||||
targetPosition.z += (vehicle->info->handling.dimensions.z * 2.f);
|
targetPosition.z += (vehicle->info->handling.dimensions.z * 1.f);
|
||||||
physTarget = vehicle->physBody;
|
physTarget = vehicle->physBody;
|
||||||
|
m_cameraAngles.y = glm::half_pi<float>();
|
||||||
|
|
||||||
|
// 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>();
|
||||||
|
if (velocity < 0.f) {
|
||||||
|
idealAngle = glm::mod(idealAngle - 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;
|
||||||
|
}
|
||||||
|
m_cameraAngles.x += glm::sign(rotation) * std::min(kMaxRotationRate * dt, glm::abs(rotation));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Non-topdown camera can orbit
|
// Non-topdown camera can orbit
|
||||||
if( camMode != IngameState::CAMERA_TOPDOWN )
|
if( camMode != IngameState::CAMERA_TOPDOWN )
|
||||||
{
|
{
|
||||||
// Rotate the cameraPosition vector around targetPosition by the mouse movement
|
// Determine the "ideal" camera position for the current view angles
|
||||||
auto camtotarget = targetPosition - cameraPosition;
|
auto yaw = glm::angleAxis(m_cameraAngles.x, glm::vec3(0.f, 0.f,-1.f));
|
||||||
float camAngle = glm::atan(camtotarget.y, camtotarget.x);
|
auto pitch = glm::angleAxis(m_cameraAngles.y, glm::vec3(0.f,-1.f, 0.f));
|
||||||
glm::quat epsilon( glm::vec3( 0.f, 0.f, camAngle) );
|
auto cameraOffset =
|
||||||
glm::quat theta( glm::vec3(0.f, 0.f, -mouseMove.x) );
|
yaw * pitch * glm::vec3(0.f, 0.f, viewDistance);
|
||||||
glm::quat rho( epsilon * glm::vec3(0.f, mouseMove.y, 0.f) );
|
cameraPosition = targetPosition + cameraOffset;
|
||||||
cameraPosition = targetPosition - (theta * (rho * camtotarget));
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -27,6 +27,9 @@ class IngameState : public State
|
|||||||
/** Timer to hold user camera position */
|
/** Timer to hold user camera position */
|
||||||
float autolookTimer;
|
float autolookTimer;
|
||||||
CameraMode camMode;
|
CameraMode camMode;
|
||||||
|
|
||||||
|
/// Current camera yaw and pitch
|
||||||
|
glm::vec2 m_cameraAngles;
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief IngameState
|
* @brief IngameState
|
||||||
|
Loading…
Reference in New Issue
Block a user