mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-22 18:32:44 +01:00
Rewrite of camera and character look code
Make states responsible for interpolating camera transformation Apply look direction to character orientation
This commit is contained in:
parent
04e79a5117
commit
ddb62ed3cd
@ -319,7 +319,9 @@ void CharacterObject::updateCharacter(float dt) {
|
|||||||
glm::vec3 walkDir = updateMovementAnimation(dt);
|
glm::vec3 walkDir = updateMovementAnimation(dt);
|
||||||
|
|
||||||
if (canTurn()) {
|
if (canTurn()) {
|
||||||
rotation = glm::angleAxis(m_look.x, glm::vec3{0.f, 0.f, 1.f});
|
// Rotation is based on look angles and movement
|
||||||
|
float yaw = m_look.x + std::atan2(movement.z, movement.x);
|
||||||
|
rotation = glm::quat(glm::vec3(0.f, 0.f, yaw));
|
||||||
}
|
}
|
||||||
|
|
||||||
walkDir = rotation * walkDir;
|
walkDir = rotation * walkDir;
|
||||||
|
@ -522,17 +522,13 @@ void RWGame::tick(float dt) {
|
|||||||
|
|
||||||
/// @todo this doesn't make sense as the condition
|
/// @todo this doesn't make sense as the condition
|
||||||
if (state.playerObject) {
|
if (state.playerObject) {
|
||||||
nextCam.frustum.update(nextCam.frustum.projection() *
|
currentCam.frustum.update(currentCam.frustum.projection() *
|
||||||
nextCam.getView());
|
currentCam.getView());
|
||||||
// Use the current camera position to spawn pedestrians.
|
// Use the current camera position to spawn pedestrians.
|
||||||
world->cleanupTraffic(nextCam);
|
world->cleanupTraffic(currentCam);
|
||||||
world->createTraffic(nextCam);
|
world->createTraffic(currentCam);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// render() needs two cameras to smoothly interpolate between ticks.
|
|
||||||
lastCam = nextCam;
|
|
||||||
nextCam = currState->getCamera();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RWGame::render(float alpha, float time) {
|
void RWGame::render(float alpha, float time) {
|
||||||
@ -540,6 +536,11 @@ void RWGame::render(float alpha, float time) {
|
|||||||
|
|
||||||
getRenderer().getRenderer()->swap();
|
getRenderer().getRenderer()->swap();
|
||||||
|
|
||||||
|
// Update the camera
|
||||||
|
if (!StateManager::get().states.empty()) {
|
||||||
|
currentCam = StateManager::get().states.back()->getCamera(alpha);
|
||||||
|
}
|
||||||
|
|
||||||
glm::ivec2 windowSize = getWindow().getSize();
|
glm::ivec2 windowSize = getWindow().getSize();
|
||||||
renderer.setViewport(windowSize.x, windowSize.y);
|
renderer.setViewport(windowSize.x, windowSize.y);
|
||||||
|
|
||||||
@ -587,9 +588,7 @@ void RWGame::render(float alpha, float time) {
|
|||||||
viewCam.rotation = state.cameraRotation;
|
viewCam.rotation = state.cameraRotation;
|
||||||
} else {
|
} else {
|
||||||
// There's no cutscene playing - use the camera returned by the State.
|
// There's no cutscene playing - use the camera returned by the State.
|
||||||
viewCam.position = glm::mix(lastCam.position, nextCam.position, alpha);
|
viewCam = currentCam;
|
||||||
viewCam.rotation =
|
|
||||||
glm::slerp(lastCam.rotation, nextCam.rotation, alpha);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
viewCam.frustum.aspectRatio =
|
viewCam.frustum.aspectRatio =
|
||||||
|
@ -31,7 +31,7 @@ class RWGame : public GameBase {
|
|||||||
std::chrono::steady_clock::time_point last_clock_time;
|
std::chrono::steady_clock::time_point last_clock_time;
|
||||||
|
|
||||||
bool inFocus = true;
|
bool inFocus = true;
|
||||||
ViewCamera lastCam, nextCam;
|
ViewCamera currentCam;
|
||||||
|
|
||||||
enum class DebugViewMode {
|
enum class DebugViewMode {
|
||||||
Disabled,
|
Disabled,
|
||||||
@ -82,7 +82,7 @@ public:
|
|||||||
|
|
||||||
bool hitWorldRay(glm::vec3& hit, glm::vec3& normal,
|
bool hitWorldRay(glm::vec3& hit, glm::vec3& normal,
|
||||||
GameObject** object = nullptr) {
|
GameObject** object = nullptr) {
|
||||||
auto vc = nextCam;
|
auto vc = currentCam;
|
||||||
glm::vec3 from(vc.position.x, vc.position.y, vc.position.z);
|
glm::vec3 from(vc.position.x, vc.position.y, vc.position.z);
|
||||||
glm::vec3 tmp = vc.rotation * glm::vec3(1000.f, 0.f, 0.f);
|
glm::vec3 tmp = vc.rotation * glm::vec3(1000.f, 0.f, 0.f);
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ void State::handleEvent(const SDL_Event& e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ViewCamera& State::getCamera() {
|
const ViewCamera& State::getCamera(float alpha) {
|
||||||
return defaultView;
|
return defaultView;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ bool State::shouldWorldUpdate() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
GameWorld* State::getWorld() {
|
GameWorld* State::getWorld() const {
|
||||||
return game->getWorld();
|
return game->getWorld();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ public:
|
|||||||
|
|
||||||
virtual void handleEvent(const SDL_Event& e);
|
virtual void handleEvent(const SDL_Event& e);
|
||||||
|
|
||||||
virtual const ViewCamera& getCamera();
|
virtual const ViewCamera& getCamera(float alpha);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns false if the game world should not should
|
* Returns false if the game world should not should
|
||||||
@ -56,7 +56,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
virtual bool shouldWorldUpdate();
|
virtual bool shouldWorldUpdate();
|
||||||
|
|
||||||
GameWorld* getWorld();
|
GameWorld* getWorld() const;
|
||||||
GameWindow& getWindow();
|
GameWindow& getWindow();
|
||||||
|
|
||||||
bool hasExited() const {
|
bool hasExited() const {
|
||||||
|
@ -371,6 +371,6 @@ void DebugState::giveItem(int slot) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ViewCamera& DebugState::getCamera() {
|
const ViewCamera& DebugState::getCamera(float alpha) {
|
||||||
return _debugCam;
|
return _debugCam;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ public:
|
|||||||
void spawnFollower(unsigned int id);
|
void spawnFollower(unsigned int id);
|
||||||
void giveItem(int slot);
|
void giveItem(int slot);
|
||||||
|
|
||||||
const ViewCamera& getCamera();
|
const ViewCamera& getCamera(float alpha);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DEBUGSTATE_HPP
|
#endif // DEBUGSTATE_HPP
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
constexpr float kAutoLookTime = 2.f;
|
constexpr float kAutoLookTime = 2.f;
|
||||||
constexpr float kAutolookMinVelocity = 0.2f;
|
constexpr float kAutolookMinVelocity = 0.2f;
|
||||||
|
const float kInGameFOV = glm::half_pi<float>();
|
||||||
const float kMaxRotationRate = glm::half_pi<float>();
|
const float kMaxRotationRate = glm::half_pi<float>();
|
||||||
const float kCameraPitchLimit = glm::quarter_pi<float>() * 0.5f;
|
const float kCameraPitchLimit = glm::quarter_pi<float>() * 0.5f;
|
||||||
const float kVehicleCameraPitch =
|
const float kVehicleCameraPitch =
|
||||||
@ -35,6 +36,7 @@ IngameState::IngameState(RWGame* game, bool newgame, const std::string& save)
|
|||||||
, m_cameraAngles{0.f, glm::half_pi<float>()}
|
, m_cameraAngles{0.f, glm::half_pi<float>()}
|
||||||
, m_invertedY(game->getConfig().getInputInvertY())
|
, m_invertedY(game->getConfig().getInputInvertY())
|
||||||
, m_vehicleFreeLook(true) {
|
, m_vehicleFreeLook(true) {
|
||||||
|
_look.frustum.fov = kInGameFOV;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IngameState::startTest() {
|
void IngameState::startTest() {
|
||||||
@ -43,7 +45,7 @@ void IngameState::startTest() {
|
|||||||
getWorld()->state->playerObject = playerChar->getGameObjectID();
|
getWorld()->state->playerObject = playerChar->getGameObjectID();
|
||||||
|
|
||||||
glm::vec3 itemspawn(276.5f, -609.f, 36.5f);
|
glm::vec3 itemspawn(276.5f, -609.f, 36.5f);
|
||||||
for (int i = 1; i < getWorld()->data->weaponData.size(); ++i) {
|
for (unsigned int i = 1; i < getWorld()->data->weaponData.size(); ++i) {
|
||||||
auto& item = getWorld()->data->weaponData[i];
|
auto& item = getWorld()->data->weaponData[i];
|
||||||
getWorld()->createPickup(itemspawn, item->modelID,
|
getWorld()->createPickup(itemspawn, item->modelID,
|
||||||
PickupObject::OnStreet);
|
PickupObject::OnStreet);
|
||||||
@ -141,24 +143,6 @@ void IngameState::tick(float dt) {
|
|||||||
return inputEnabled && world->state->input[0].pressed(c);
|
return inputEnabled && world->state->input[0].pressed(c);
|
||||||
};
|
};
|
||||||
|
|
||||||
float viewDistance = 4.f;
|
|
||||||
switch (camMode) {
|
|
||||||
case IngameState::CAMERA_CLOSE:
|
|
||||||
viewDistance = 2.f;
|
|
||||||
break;
|
|
||||||
case IngameState::CAMERA_NORMAL:
|
|
||||||
viewDistance = 4.0f;
|
|
||||||
break;
|
|
||||||
case IngameState::CAMERA_FAR:
|
|
||||||
viewDistance = 6.f;
|
|
||||||
break;
|
|
||||||
case IngameState::CAMERA_TOPDOWN:
|
|
||||||
viewDistance = 15.f;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
viewDistance = 4.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto target = world->pedestrianPool.find(world->state->cameraTarget);
|
auto target = world->pedestrianPool.find(world->state->cameraTarget);
|
||||||
|
|
||||||
if (target == nullptr) {
|
if (target == nullptr) {
|
||||||
@ -170,39 +154,17 @@ void IngameState::tick(float dt) {
|
|||||||
targetPosition += glm::vec3(0.f, 0.f, 1.f);
|
targetPosition += glm::vec3(0.f, 0.f, 1.f);
|
||||||
lookTargetPosition += glm::vec3(0.f, 0.f, 0.5f);
|
lookTargetPosition += glm::vec3(0.f, 0.f, 0.5f);
|
||||||
|
|
||||||
btCollisionObject* physTarget = player->getCharacter()->physObject;
|
|
||||||
|
|
||||||
auto vehicle =
|
auto vehicle =
|
||||||
(target->type() == GameObject::Character)
|
(target->type() == GameObject::Character)
|
||||||
? static_cast<CharacterObject*>(target)->getCurrentVehicle()
|
? static_cast<CharacterObject*>(target)->getCurrentVehicle()
|
||||||
: nullptr;
|
: nullptr;
|
||||||
if (vehicle) {
|
if (vehicle) {
|
||||||
auto model = vehicle->getModel();
|
|
||||||
float maxDist = 0.f;
|
|
||||||
for (auto& g : model->geometries) {
|
|
||||||
float partSize = glm::length(g->geometryBounds.center) +
|
|
||||||
g->geometryBounds.radius;
|
|
||||||
maxDist = std::max(partSize, maxDist);
|
|
||||||
}
|
|
||||||
viewDistance = viewDistance + maxDist;
|
|
||||||
targetPosition = vehicle->getPosition();
|
|
||||||
lookTargetPosition = targetPosition;
|
|
||||||
lookTargetPosition.z +=
|
|
||||||
(vehicle->info->handling.dimensions.z * 0.5f);
|
|
||||||
targetPosition.z += (vehicle->info->handling.dimensions.z * 0.5f);
|
|
||||||
physTarget = vehicle->collision->getBulletBody();
|
|
||||||
|
|
||||||
if (!m_vehicleFreeLook) {
|
|
||||||
m_cameraAngles.y = kVehicleCameraPitch;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rotate the camera to the ideal angle if the player isn't moving
|
// Rotate the camera to the ideal angle if the player isn't moving
|
||||||
// it
|
// it
|
||||||
float velocity = vehicle->getVelocity();
|
float velocity = vehicle->getVelocity();
|
||||||
if (autolookTimer <= 0.f &&
|
if (autolookTimer <= 0.f &&
|
||||||
glm::abs(velocity) > kAutolookMinVelocity) {
|
glm::abs(velocity) > kAutolookMinVelocity) {
|
||||||
auto idealYaw =
|
auto idealYaw = glm::roll(vehicle->getRotation());
|
||||||
-glm::roll(vehicle->getRotation()) + glm::half_pi<float>();
|
|
||||||
const auto idealPitch = kVehicleCameraPitch;
|
const auto idealPitch = kVehicleCameraPitch;
|
||||||
if (velocity < 0.f) {
|
if (velocity < 0.f) {
|
||||||
idealYaw = glm::mod(idealYaw - glm::pi<float>(),
|
idealYaw = glm::mod(idealYaw - glm::pi<float>(),
|
||||||
@ -225,53 +187,7 @@ void IngameState::tick(float dt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Non-topdown camera can orbit
|
|
||||||
if (camMode != IngameState::CAMERA_TOPDOWN) {
|
|
||||||
bool lookleft = held(GameInputState::LookLeft);
|
|
||||||
bool lookright = held(GameInputState::LookRight);
|
|
||||||
if ((lookleft || lookright) && vehicle != nullptr) {
|
|
||||||
auto rotation = vehicle->getRotation();
|
|
||||||
if (!lookright) {
|
|
||||||
rotation *= glm::angleAxis(glm::half_pi<float>(),
|
|
||||||
glm::vec3(0.f, 0.f, -1.f));
|
|
||||||
} else if (!lookleft) {
|
|
||||||
rotation *= glm::angleAxis(glm::half_pi<float>(),
|
|
||||||
glm::vec3(0.f, 0.f, 1.f));
|
|
||||||
}
|
|
||||||
cameraPosition = targetPosition +
|
|
||||||
rotation * glm::vec3(0.f, viewDistance, 0.f);
|
|
||||||
} else {
|
|
||||||
// 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 cameraOffset =
|
|
||||||
yaw * pitch * glm::vec3(0.f, 0.f, viewDistance);
|
|
||||||
cameraPosition = targetPosition + cameraOffset;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
cameraPosition = targetPosition + glm::vec3(0.f, 0.f, viewDistance);
|
|
||||||
}
|
|
||||||
|
|
||||||
glm::quat angle;
|
|
||||||
|
|
||||||
auto camtotarget = targetPosition - cameraPosition;
|
|
||||||
auto dir = glm::normalize(camtotarget);
|
|
||||||
float correction = glm::length(camtotarget) - viewDistance;
|
|
||||||
if (correction < 0.f) {
|
|
||||||
float innerDist = viewDistance * 0.1f;
|
|
||||||
correction = glm::min(0.f, correction + innerDist);
|
|
||||||
}
|
|
||||||
cameraPosition += dir * 10.f * correction * dt;
|
|
||||||
|
|
||||||
auto lookdir = glm::normalize(lookTargetPosition - cameraPosition);
|
|
||||||
// Calculate the yaw to look at the target.
|
|
||||||
float angleYaw = glm::atan(lookdir.y, lookdir.x);
|
|
||||||
angle = glm::quat(glm::vec3(0.f, 0.f, angleYaw));
|
|
||||||
glm::vec3 movement;
|
glm::vec3 movement;
|
||||||
|
|
||||||
movement.x = input(GameInputState::GoForward) -
|
movement.x = input(GameInputState::GoForward) -
|
||||||
input(GameInputState::GoBackwards);
|
input(GameInputState::GoBackwards);
|
||||||
movement.y =
|
movement.y =
|
||||||
@ -307,41 +223,14 @@ void IngameState::tick(float dt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float length = glm::length(movement);
|
float length = glm::length(movement);
|
||||||
float movementAngle = angleYaw - glm::half_pi<float>();
|
|
||||||
if (length > 0.1f) {
|
if (length > 0.1f) {
|
||||||
glm::vec3 direction = glm::normalize(movement);
|
auto move = speed * glm::normalize(movement);
|
||||||
movementAngle += atan2(direction.y, direction.x);
|
player->setMoveDirection(glm::vec3(move.x, 0.f, move.y));
|
||||||
player->setMoveDirection(glm::vec3(speed, 0.f, 0.f));
|
|
||||||
} else {
|
} else {
|
||||||
player->setMoveDirection(glm::vec3(0.f));
|
player->setMoveDirection(glm::vec3(0.f));
|
||||||
}
|
}
|
||||||
player->setLookDirection({movementAngle, 0.f});
|
player->setLookDirection(m_cameraAngles);
|
||||||
}
|
}
|
||||||
|
|
||||||
float len2d = glm::length(glm::vec2(lookdir));
|
|
||||||
float anglePitch = glm::atan(lookdir.z, len2d);
|
|
||||||
angle *= glm::quat(glm::vec3(0.f, -anglePitch, 0.f));
|
|
||||||
|
|
||||||
// Use rays to ensure target is visible from cameraPosition
|
|
||||||
auto rayEnd = cameraPosition;
|
|
||||||
auto rayStart = targetPosition;
|
|
||||||
auto to = btVector3(rayEnd.x, rayEnd.y, rayEnd.z);
|
|
||||||
auto from = btVector3(rayStart.x, rayStart.y, rayStart.z);
|
|
||||||
ClosestNotMeRayResultCallback ray(physTarget, from, to);
|
|
||||||
|
|
||||||
world->dynamicsWorld->rayTest(from, to, ray);
|
|
||||||
if (ray.hasHit() && ray.m_closestHitFraction < 1.f) {
|
|
||||||
cameraPosition =
|
|
||||||
glm::vec3(ray.m_hitPointWorld.x(), ray.m_hitPointWorld.y(),
|
|
||||||
ray.m_hitPointWorld.z());
|
|
||||||
cameraPosition +=
|
|
||||||
glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
|
|
||||||
ray.m_hitNormalWorld.z()) *
|
|
||||||
0.1f;
|
|
||||||
}
|
|
||||||
|
|
||||||
_look.position = cameraPosition;
|
|
||||||
_look.rotation = angle;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -424,7 +313,7 @@ void IngameState::handlePlayerInput(const SDL_Event& event) {
|
|||||||
if (!m_invertedY) {
|
if (!m_invertedY) {
|
||||||
mouseMove.y = -mouseMove.y;
|
mouseMove.y = -mouseMove.y;
|
||||||
}
|
}
|
||||||
m_cameraAngles += glm::vec2(mouseMove.x, mouseMove.y);
|
m_cameraAngles += glm::vec2(-mouseMove.x, mouseMove.y);
|
||||||
m_cameraAngles.y =
|
m_cameraAngles.y =
|
||||||
glm::clamp(m_cameraAngles.y, kCameraPitchLimit,
|
glm::clamp(m_cameraAngles.y, kCameraPitchLimit,
|
||||||
glm::pi<float>() - kCameraPitchLimit);
|
glm::pi<float>() - kCameraPitchLimit);
|
||||||
@ -439,6 +328,140 @@ bool IngameState::shouldWorldUpdate() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ViewCamera& IngameState::getCamera() {
|
const ViewCamera& IngameState::getCamera(float alpha) {
|
||||||
|
auto player = game->getPlayer();
|
||||||
|
auto world = getWorld();
|
||||||
|
|
||||||
|
if (!player) {
|
||||||
|
return _look;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Force all input to 0 if player input is disabled
|
||||||
|
/// @todo verify 0ing input is the correct behaviour
|
||||||
|
const auto inputEnabled = player->isInputEnabled();
|
||||||
|
|
||||||
|
auto held = [&](GameInputState::Control c) {
|
||||||
|
return inputEnabled && world->state->input[0].pressed(c);
|
||||||
|
};
|
||||||
|
|
||||||
|
float viewDistance = getViewDistance();
|
||||||
|
auto target = getCameraTarget();
|
||||||
|
bool lookleft = held(GameInputState::LookLeft);
|
||||||
|
bool lookright = held(GameInputState::LookRight);
|
||||||
|
btCollisionObject* physTarget = player->getCharacter()->physObject;
|
||||||
|
|
||||||
|
auto targetTransform = target->getTimeAdjustedTransform(alpha);
|
||||||
|
|
||||||
|
glm::vec3 targetPosition(targetTransform[3]);
|
||||||
|
glm::vec3 lookTargetPosition(targetPosition);
|
||||||
|
targetPosition += glm::vec3(0.f, 0.f, 1.f);
|
||||||
|
lookTargetPosition += glm::vec3(0.f, 0.f, 0.5f);
|
||||||
|
|
||||||
|
if (target->type() == GameObject::Vehicle) {
|
||||||
|
auto vehicle = (VehicleObject*)target;
|
||||||
|
auto model = vehicle->getModel();
|
||||||
|
auto maxDist = model->getBoundingRadius() * 2.f;
|
||||||
|
viewDistance = viewDistance + maxDist;
|
||||||
|
lookTargetPosition.z += (vehicle->info->handling.dimensions.z * 0.5f);
|
||||||
|
targetPosition.z += (vehicle->info->handling.dimensions.z * 0.5f);
|
||||||
|
physTarget = vehicle->collision->getBulletBody();
|
||||||
|
|
||||||
|
if (!m_vehicleFreeLook) {
|
||||||
|
m_cameraAngles.y = kVehicleCameraPitch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle top-down camera
|
||||||
|
if (camMode == CAMERA_TOPDOWN) {
|
||||||
|
cameraPosition = targetPosition + glm::vec3(0.f, 0.f, viewDistance);
|
||||||
|
_look.rotation =
|
||||||
|
glm::angleAxis(glm::half_pi<float>(), glm::vec3(0.f, 1.f, 0.f));
|
||||||
|
} else if ((lookleft || lookright) &&
|
||||||
|
target->type() == GameObject::Vehicle) {
|
||||||
|
auto rotation = target->getRotation();
|
||||||
|
if (!lookright) {
|
||||||
|
rotation *= glm::angleAxis(glm::half_pi<float>(),
|
||||||
|
glm::vec3(0.f, 0.f, -1.f));
|
||||||
|
} else if (!lookleft) {
|
||||||
|
rotation *=
|
||||||
|
glm::angleAxis(glm::half_pi<float>(), glm::vec3(0.f, 0.f, 1.f));
|
||||||
|
}
|
||||||
|
cameraPosition =
|
||||||
|
targetPosition + rotation * glm::vec3(0.f, viewDistance, 0.f);
|
||||||
|
} else {
|
||||||
|
// Determine the "ideal" camera position for the current view angles
|
||||||
|
auto yaw = glm::angleAxis(m_cameraAngles.x - glm::half_pi<float>(),
|
||||||
|
glm::vec3(0.f, 0.f, 1.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto lookdir = glm::normalize(lookTargetPosition - cameraPosition);
|
||||||
|
// Calculate the angles to look at the target position
|
||||||
|
float len2d = glm::length(glm::vec2(lookdir));
|
||||||
|
float anglePitch = glm::atan(lookdir.z, len2d);
|
||||||
|
float angleYaw = glm::atan(lookdir.y, lookdir.x);
|
||||||
|
glm::quat angle(glm::vec3(0.f, -anglePitch, angleYaw));
|
||||||
|
|
||||||
|
// Ensure the target position is actually visible
|
||||||
|
auto rayEnd = cameraPosition;
|
||||||
|
auto rayStart = targetPosition;
|
||||||
|
auto to = btVector3(rayEnd.x, rayEnd.y, rayEnd.z);
|
||||||
|
auto from = btVector3(rayStart.x, rayStart.y, rayStart.z);
|
||||||
|
ClosestNotMeRayResultCallback ray(physTarget, from, to);
|
||||||
|
|
||||||
|
world->dynamicsWorld->rayTest(from, to, ray);
|
||||||
|
if (ray.hasHit() && ray.m_closestHitFraction < 1.f) {
|
||||||
|
cameraPosition =
|
||||||
|
glm::vec3(ray.m_hitPointWorld.x(), ray.m_hitPointWorld.y(),
|
||||||
|
ray.m_hitPointWorld.z());
|
||||||
|
cameraPosition +=
|
||||||
|
glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
|
||||||
|
ray.m_hitNormalWorld.z()) *
|
||||||
|
0.1f;
|
||||||
|
}
|
||||||
|
_look.position = cameraPosition;
|
||||||
|
_look.rotation = angle;
|
||||||
return _look;
|
return _look;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GameObject* IngameState::getCameraTarget() const {
|
||||||
|
auto target =
|
||||||
|
getWorld()->pedestrianPool.find(game->getState()->cameraTarget);
|
||||||
|
|
||||||
|
if (target == nullptr && game->getPlayer()) {
|
||||||
|
target = game->getPlayer()->getCharacter();
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the target is a character in a vehicle, make the vehicle the target
|
||||||
|
if (target && target->type() == GameObject::Character) {
|
||||||
|
auto vehicle = ((CharacterObject*)target)->getCurrentVehicle();
|
||||||
|
if (vehicle) {
|
||||||
|
target = vehicle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
float IngameState::getViewDistance() const {
|
||||||
|
float viewDistance = 4.f;
|
||||||
|
switch (camMode) {
|
||||||
|
case IngameState::CAMERA_CLOSE:
|
||||||
|
viewDistance = 2.f;
|
||||||
|
break;
|
||||||
|
case IngameState::CAMERA_NORMAL:
|
||||||
|
viewDistance = 4.0f;
|
||||||
|
break;
|
||||||
|
case IngameState::CAMERA_FAR:
|
||||||
|
viewDistance = 6.f;
|
||||||
|
break;
|
||||||
|
case IngameState::CAMERA_TOPDOWN:
|
||||||
|
viewDistance = 15.f;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
viewDistance = 4.f;
|
||||||
|
}
|
||||||
|
return viewDistance;
|
||||||
|
}
|
||||||
|
@ -32,6 +32,7 @@ class IngameState : public State {
|
|||||||
bool m_vehicleFreeLook;
|
bool m_vehicleFreeLook;
|
||||||
|
|
||||||
float moneyTimer = 0.f; // Timer used to updated displayed money value
|
float moneyTimer = 0.f; // Timer used to updated displayed money value
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief IngameState
|
* @brief IngameState
|
||||||
@ -56,7 +57,10 @@ public:
|
|||||||
|
|
||||||
virtual bool shouldWorldUpdate();
|
virtual bool shouldWorldUpdate();
|
||||||
|
|
||||||
const ViewCamera& getCamera();
|
const ViewCamera& getCamera(float alpha);
|
||||||
|
private:
|
||||||
|
GameObject* getCameraTarget() const;
|
||||||
|
float getViewDistance() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // INGAMESTATE_HPP
|
#endif // INGAMESTATE_HPP
|
||||||
|
Loading…
Reference in New Issue
Block a user