18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00

Merge pull request #402 from MaSzyna-EU07/feat/camera-orbit-and-shunting-views

feat: orbit, shunting, coupling and trackside views of the train (GE#551)
This commit is contained in:
maj00r
2026-11-18 01:19:58 +01:00
committed by GitHub
12 changed files with 1305 additions and 4 deletions

View File

@@ -182,6 +182,7 @@ set(SOURCES
"rendering/screenshot.cpp"
"input/driverkeyboardinput.cpp"
"application/drivermode.cpp"
"application/consistviews.cpp"
"application/driveruilayer.cpp"
"application/driveruipanels.cpp"
"input/editorkeyboardinput.cpp"
@@ -550,6 +551,7 @@ set(CORE_SOURCES
"vehicle/brakesetting.cpp"
"vehicle/handover.cpp"
"vehicle/horn.cpp"
"vehicle/cameraviews.cpp"
"vehicle/linebreaker.cpp"
"vehicle/smoke.cpp"
"vehicle/odometer.cpp"

View File

@@ -0,0 +1,316 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "application/consistviews.h"
#include <algorithm>
#include <cmath>
#include "vehicle/Camera.h"
#include "vehicle/Driver.h"
#include "vehicle/DynObj.h"
namespace {
// more vehicles than any train has: a guard against a loop in the couplings
std::size_t constexpr train_limit{1000};
// metres the vehicle picked moves before the trackside view takes the way it went for the way of the train
double constexpr direction_step{0.5};
cameraviews::vehicle_pose pose_of(TDynamicObject const *Vehicle) {
cameraviews::vehicle_pose pose;
pose.position = Vehicle->GetPosition();
pose.front = Vehicle->VectorFront();
pose.length = Vehicle->MoverParameters->Dim.L;
pose.width = Vehicle->MoverParameters->Dim.W;
pose.height = Vehicle->MoverParameters->Dim.H;
return pose;
}
// the end of the Vehicle the Other vehicle is beyond, -1 when it's beyond neither
int end_towards(TDynamicObject const *Vehicle, TDynamicObject const *Other) {
for (auto const side : {end::front, end::rear}) {
if (Vehicle->MoverParameters->Neighbours[side].vehicle == Other) {
return side;
}
}
return -1;
}
// the end of the Vehicle at an end of the train, away from the Inner vehicle next to it (nullptr when it's alone)
int outer_end(TDynamicObject const *Vehicle, TDynamicObject const *Inner, int const Alone) {
if (Inner == nullptr) {
return Alone;
}
auto const inner{end_towards(Vehicle, Inner)};
return inner < 0 ? Alone : 1 - inner;
}
cameraviews::coupler coupler_of(TDynamicObject const *Vehicle, int const End) {
cameraviews::coupler coupler;
coupler.position = Vehicle->CouplerPosition(static_cast<end>(End));
coupler.outward = Vehicle->VectorFront() * (End == end::front ? 1.0 : -1.0);
coupler.width = Vehicle->MoverParameters->Dim.W;
return coupler;
}
// the driver's way: the front (1) or the rear (-1) of the Driven vehicle, as the user drives it
int driven_way(TDynamicObject const *Driven) {
auto const cab{Driven->MoverParameters->CabOccupied < 0 ? -1 : 1};
auto const direction{Driven->MoverParameters->DirActive < 0 ? -1 : 1};
return cab * direction;
}
// the coupler of the Vehicle furthest along the Way (or furthest back, with a negative Way)
glm::dvec3 end_along(TDynamicObject const *Vehicle, glm::dvec3 const &Way) {
auto const front{Vehicle->CouplerPosition(end::front)};
auto const rear{Vehicle->CouplerPosition(end::rear)};
return glm::dot(front - rear, Way) >= 0.0 ? front : rear;
}
// the front of the Vehicle, or its rear when that faces the Way
glm::dvec3 facing_along(TDynamicObject const *Vehicle, glm::dvec3 const &Way) {
auto const front{cameraviews::flat_front(Vehicle->VectorFront())};
return glm::dot(front, Way) < 0.0 ? -front : front;
}
bool usable(cameraviews::placement const &Place) {
auto const finite{[](glm::dvec3 const &Value) { return std::isfinite(Value.x) && std::isfinite(Value.y) && std::isfinite(Value.z); }};
if (false == (finite(Place.eye) && finite(Place.target) && finite(Place.up))) {
return false;
}
auto const look{Place.target - Place.eye};
if (glm::length(look) < 0.01 || glm::length(Place.up) < 0.5) {
return false;
}
// a camera looking along its own up has no way to tell left from right
return std::abs(glm::dot(glm::normalize(look), glm::normalize(Place.up))) < 0.999;
}
} // namespace
std::vector<TDynamicObject *> consist_views::train_of(TDynamicObject *Driven) {
std::vector<TDynamicObject *> train;
if (Driven == nullptr) {
return train;
}
auto const *controller{Driven->Mechanik != nullptr ? Driven->Mechanik : Driven->ctOwner};
auto *first{controller != nullptr ? controller->Vehicle(end::front) : nullptr};
if (first == nullptr) {
first = Driven;
}
for (auto *vehicle{first}; vehicle != nullptr && train.size() < train_limit; vehicle = vehicle->Next()) {
if (std::ranges::find(train, vehicle) != train.end()) {
break;
}
train.push_back(vehicle);
}
if (std::ranges::find(train, Driven) == train.end()) {
// the controller's list is behind the couplings: the driven vehicle alone, until it catches up
train.assign(1, Driven);
}
return train;
}
TDynamicObject *consist_views::pick(std::vector<TDynamicObject *> const &Train, TDynamicObject *Driven) {
auto picked{std::ranges::find(Train, m_vehicle)};
if (m_vehicle == nullptr || picked == Train.end()) {
picked = std::ranges::find(Train, Driven);
}
if (picked == Train.end()) {
picked = Train.begin();
}
auto index{static_cast<int>(picked - Train.begin())};
if (m_view != kind::coupling) {
index = cameraviews::step_index(index, static_cast<int>(Train.size()), m_step);
m_step = 0;
}
if (Train[index] != m_vehicle) {
// m_lastposition is of the vehicle before: the way the train goes is set again from the way the user drives,
// or the step between the two vehicles would be taken for a move
m_moved = false;
}
m_vehicle = Train[index];
return m_vehicle;
}
double consist_views::gap_beyond(TDynamicObject const *Vehicle, int const End) {
auto const &neighbour{Vehicle->MoverParameters->Neighbours[End]};
if (neighbour.vehicle == nullptr || Vehicle->MoverParameters->Couplers[End].CouplingFlag != 0) {
return -1.0;
}
if (neighbour.distance > cameraviews::coupler_watch_range) {
return -1.0;
}
return std::max(0.0, static_cast<double>(neighbour.distance));
}
consist_views::coupling_place consist_views::coupling_at(std::vector<TDynamicObject *> const &Train, int const Index) {
auto const count{static_cast<int>(Train.size())};
coupling_place place;
if (Index > 0 && Index < count) {
// between two vehicles of the train
auto *ahead{Train[Index - 1]};
auto const *behind{Train[Index]};
auto const aheadend{end_towards(ahead, behind)};
auto const behindend{end_towards(behind, ahead)};
place.vehicle = ahead;
place.coupler = coupler_of(ahead, aheadend < 0 ? end::rear : aheadend);
place.other = true;
place.otherposition = behind->CouplerPosition(static_cast<end>(behindend < 0 ? end::front : behindend));
return place;
}
// an end of the train, perhaps with another vehicle coming closer to it
auto const front{Index <= 0};
auto *vehicle{front ? Train.front() : Train.back()};
auto const *inner{count > 1 ? Train[front ? 1 : count - 2] : nullptr};
auto const outer{outer_end(vehicle, inner, front ? end::front : end::rear)};
place.vehicle = vehicle;
place.coupler = coupler_of(vehicle, outer);
auto const &neighbour{vehicle->MoverParameters->Neighbours[outer]};
place.gap = gap_beyond(vehicle, outer);
if (place.gap >= 0.0 && (neighbour.vehicle_end == end::front || neighbour.vehicle_end == end::rear)) {
place.other = true;
place.otherposition = neighbour.vehicle->CouplerPosition(static_cast<end>(neighbour.vehicle_end));
}
return place;
}
cameraviews::train_motion consist_views::motion_of(std::vector<TDynamicObject *> const &Train, TDynamicObject const *Driven) {
auto const position{m_vehicle->GetPosition()};
if (false == m_moved) {
// standing, the train is to go the way the user drives
m_direction = cameraviews::flat_front(m_vehicle->VectorFront()) *
static_cast<double>(cameraviews::driving_facing(m_vehicle->VectorFront(), Driven->VectorFront(), driven_way(Driven)));
m_lastposition = position;
m_moved = true;
}
else if (std::hypot(position.x - m_lastposition.x, position.z - m_lastposition.z) > direction_step) {
m_direction = cameraviews::flat_front(position - m_lastposition);
m_lastposition = position;
}
std::vector<glm::dvec3> positions;
positions.reserve(Train.size());
for (auto const *vehicle : Train) {
positions.emplace_back(vehicle->GetPosition());
}
auto const [head, tail]{cameraviews::train_ends(positions, m_direction)};
cameraviews::train_motion motion;
motion.direction = m_direction;
motion.head = end_along(Train[head], m_direction);
motion.tail = end_along(Train[tail], -m_direction);
motion.speed = std::abs(m_vehicle->GetVelocity()) / 3.6;
motion.width = m_vehicle->MoverParameters->Dim.W;
if (Train.size() > 1) {
auto const *behind{Train[head == 0 ? 1 : head - 1]};
motion.curvature = cameraviews::line_curvature(Train[head]->GetPosition(), facing_along(Train[head], m_direction), behind->GetPosition(), facing_along(behind, m_direction));
}
return motion;
}
cameraviews::placement consist_views::place_orbit(TCamera &Camera, double const Seconds) {
auto const pose{pose_of(m_vehicle)};
if (m_orbit.radius <= 0.0) {
m_orbit = cameraviews::orbit_start(pose);
}
// the mouse turns the camera around the vehicle rather than the view away from it
glm::dvec2 const turn{Camera.Angle.x, Camera.Angle.y};
Camera.Angle = {};
m_orbit = cameraviews::orbit_moved(m_orbit, Camera.Velocity, turn, Seconds, pose);
return cameraviews::orbit_placement(m_orbit, pose);
}
cameraviews::placement consist_views::place_shunting(TCamera const &Camera, std::vector<TDynamicObject *> const &Train, TDynamicObject const *Driven, double const Seconds) {
auto const pose{pose_of(m_vehicle)};
auto length{0.0};
for (auto const *vehicle : Train) {
length += vehicle->MoverParameters->Dim.L;
}
m_topdown = cameraviews::topdown_moved(m_topdown, Camera.Velocity, Seconds, pose, 0.5 * length + 50.0);
auto const facing{cameraviews::driving_facing(m_vehicle->VectorFront(), Driven->VectorFront(), Driven->MoverParameters->CabOccupied)};
return cameraviews::topdown_placement(m_topdown, pose, facing);
}
cameraviews::placement consist_views::place_coupling(TCamera const &Camera, std::vector<TDynamicObject *> const &Train, double const Seconds) {
auto const count{static_cast<int>(Train.size())};
if (m_coupling < 0) {
auto const front{coupling_at(Train, 0)};
auto const rear{coupling_at(Train, count)};
m_coupling = cameraviews::first_coupling(front.gap, rear.gap, count);
}
m_coupling = cameraviews::step_index(std::min(m_coupling, count), count + 1, m_step);
m_step = 0;
auto const place{coupling_at(Train, m_coupling)};
m_vehicle = place.vehicle;
m_coupler = cameraviews::coupler_moved(m_coupler, Camera.Velocity, Seconds);
return cameraviews::coupler_placement(m_coupler, place.coupler, place.other ? &place.otherposition : nullptr);
}
cameraviews::placement consist_views::place_trackside(std::vector<TDynamicObject *> const &Train, TDynamicObject const *Driven) {
auto const motion{motion_of(Train, Driven)};
if (cameraviews::trackside_expired(m_trackside, motion)) {
m_trackside = cameraviews::trackside_next(m_trackside, motion);
}
return cameraviews::trackside_placement(m_trackside, cameraviews::orbit_centre(pose_of(m_vehicle)));
}
TDynamicObject *consist_views::enter(kind const View, TDynamicObject *Driven) {
m_view = View;
m_step = 0;
if (View == kind::coupling) {
// each time the view starts at the end of the train something comes closer to, or at its front
m_coupling = -1;
}
if (View == kind::trackside) {
// and the railfan finds a new spot ahead of the train
m_trackside.placed = false;
m_moved = false;
}
auto const train{train_of(Driven)};
if (train.empty()) {
return nullptr;
}
return pick(train, Driven);
}
void consist_views::select(int const Step) {
m_step += Step;
}
TDynamicObject *consist_views::update(TCamera &Camera, TDynamicObject *Driven, double const Seconds) {
auto const train{train_of(Driven)};
if (train.empty()) {
return nullptr;
}
pick(train, Driven);
cameraviews::placement place;
switch (m_view) {
case kind::orbit:
place = place_orbit(Camera, Seconds);
break;
case kind::shunting:
// straight down, the mouse would only tilt it off the train
Camera.Angle = {};
place = place_shunting(Camera, train, Driven, Seconds);
break;
case kind::coupling:
place = place_coupling(Camera, train, Seconds);
break;
case kind::trackside:
place = place_trackside(train, Driven);
break;
}
if (usable(place)) {
Camera.Pos = place.eye;
Camera.LookAt = place.target;
Camera.vUp = place.up;
}
return m_vehicle;
}

View File

@@ -0,0 +1,74 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <vector>
#include "vehicle/cameraviews.h"
class TCamera;
class TDynamicObject;
// The outside views of the user's train that go with one of its vehicles (eu07/maszyna#551), after the four views
// [Shift]+[F4] had before: the orbit around a vehicle, the shunting view from above, the view beside a coupling
// and the trackside view of a railfan. [ and ] pick the vehicle (the coupling) the view goes with. The camera
// follows from cameraviews.h; this class hands over the vehicles of the train and keeps what the user set.
class consist_views {
public:
enum class kind { orbit, shunting, coupling, trackside };
// starts the View of the train of the Driven vehicle, with the vehicle (or the coupling) picked before when it's
// still in the train; returns the vehicle the camera goes with, nullptr when there's no train
TDynamicObject *enter(kind View, TDynamicObject *Driven);
// picks the vehicle Step places towards the rear of the train (towards its front for a negative Step); the
// coupling in the coupling view
void select(int Step);
// places the Camera for this frame of Seconds (real time): the movement keys come from its Velocity, the mouse of
// the orbit from its Angle. returns the vehicle the camera goes with, nullptr when the train is gone
TDynamicObject *update(TCamera &Camera, TDynamicObject *Driven, double Seconds);
private:
struct coupling_place {
TDynamicObject *vehicle{nullptr}; // the vehicle the coupler is on
cameraviews::coupler coupler;
bool other{false};
glm::dvec3 otherposition{}; // the facing coupler of the vehicle beyond, coupled or coming closer
double gap{-1.0}; // at an end of the train: the distance to a vehicle coming closer, negative for none
};
// the vehicles of the train of the Driven vehicle, from its front to its rear
static std::vector<TDynamicObject *> train_of(TDynamicObject *Driven);
// the vehicle the camera goes with in the Train: the one picked before, or the Driven one
TDynamicObject *pick(std::vector<TDynamicObject *> const &Train, TDynamicObject *Driven);
// the Index coupling of the Train, 0 its front end, Train.size() its rear end
static coupling_place coupling_at(std::vector<TDynamicObject *> const &Train, int Index);
// the distance to a vehicle not coupled beyond the End of the Vehicle, negative for none
static double gap_beyond(TDynamicObject const *Vehicle, int End);
// keeps up the way the train goes and works out where its ends are, for the trackside view
cameraviews::train_motion motion_of(std::vector<TDynamicObject *> const &Train, TDynamicObject const *Driven);
cameraviews::placement place_orbit(TCamera &Camera, double Seconds);
cameraviews::placement place_shunting(TCamera const &Camera, std::vector<TDynamicObject *> const &Train, TDynamicObject const *Driven, double Seconds);
cameraviews::placement place_coupling(TCamera const &Camera, std::vector<TDynamicObject *> const &Train, double Seconds);
cameraviews::placement place_trackside(std::vector<TDynamicObject *> const &Train, TDynamicObject const *Driven);
kind m_view{kind::orbit};
TDynamicObject *m_vehicle{nullptr}; // the vehicle picked; only compared with the vehicles of the train, never followed
int m_step{0}; // vehicles to step over in the next update, from [ and ]
int m_coupling{-1}; // the coupling picked in the coupling view, -1 not yet
cameraviews::orbit m_orbit;
cameraviews::topdown m_topdown;
cameraviews::coupler_view m_coupler;
cameraviews::trackside m_trackside;
glm::dvec3 m_lastposition{}; // of the vehicle picked, where the trackside view last saw it move
glm::dvec3 m_direction{0.0, 0.0, -1.0}; // the way the train goes, for the trackside view
bool m_moved{false}; // m_lastposition is known
};

View File

@@ -739,7 +739,12 @@ void driver_mode::update_camera(double const Deltatime)
// reset window state, it'll be set again if applicable in a check below
Global.CabWindowOpen = false;
if (simulation::Train != nullptr && controlled != nullptr && Camera.m_owner != nullptr && false == DebugCameraFlag)
if (consist_view_active())
{
// the orbit, shunting, coupling and trackside views place the camera themselves
update_consist_view(Deltatime);
}
else if (simulation::Train != nullptr && controlled != nullptr && Camera.m_owner != nullptr && false == DebugCameraFlag)
{
// jeśli jazda w kabinie, przeliczyć trzeba parametry kamery
/*
@@ -1102,6 +1107,17 @@ void driver_mode::OnKeyDown(int cKey)
}
break;
}
case GLFW_KEY_LEFT_BRACKET:
case GLFW_KEY_RIGHT_BRACKET:
{
// in the orbit, shunting, coupling and trackside views: the vehicle (coupling) towards the front, or the rear,
// of the train. [Ctrl] with them moves the train in the debug mode, bound in the keyboard settings
if (false == Global.ctrlState && consist_view_active())
{
m_consistviews.select(cKey == GLFW_KEY_LEFT_BRACKET ? -1 : 1);
}
break;
}
default:
{
break;
@@ -1111,6 +1127,18 @@ void driver_mode::OnKeyDown(int cKey)
return; // nie są przekazywane do pojazdu wcale
}
bool driver_mode::consist_view_active() const
{
return FreeFlyModeFlag && m_externalview && m_externalviewmode >= view::orbit && Camera.m_owner != nullptr && false == DebugCameraFlag;
}
void driver_mode::update_consist_view(double const Deltatime)
{
auto *controlled{simulation::Train != nullptr ? simulation::Train->Dynamic() : nullptr};
// the camera goes with the vehicle picked, or stays where it is once the train is gone
Camera.m_owner = m_consistviews.update(Camera, controlled, Deltatime);
}
// places camera outside the controlled vehicle, or nearest if nothing is under control
// depending on provided switch the view is placed right outside, or at medium distance
void driver_mode::DistantView(bool const Near)
@@ -1159,7 +1187,8 @@ void driver_mode::ExternalView()
if (true == m_externalview)
{
// we're already in some external view mode, so select next one on the list
m_externalviewmode = clamp_circular(++m_externalviewmode, static_cast<int>(view::count_));
// the views of the train's vehicles come after the four older ones, unless they're turned off in eu07.ini
m_externalviewmode = clamp_circular(++m_externalviewmode, static_cast<int>(Global.ConsistViews ? view::count_ : view::orbit));
}
FreeFlyModeFlag = true;
@@ -1262,6 +1291,17 @@ void driver_mode::ExternalView()
DistantView(false);
break;
}
case view::orbit:
case view::shunting:
case view::coupling:
case view::trackside:
{
// the camera goes with a vehicle of the train, placed by update_consist_view() from the next frame on
auto const kind{static_cast<consist_views::kind>(m_externalviewmode - view::orbit)};
Camera.m_owner = m_consistviews.enter(kind, vehicle);
Camera.m_owneroffset = {};
break;
}
default:
{
break;

View File

@@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/.
#include "input/gamepadinput.h"
#include "Console.h"
#include "vehicle/Camera.h"
#include "application/consistviews.h"
#include "utilities/Classes.h"
#ifdef WITH_UART
#include "utilities/uart.h"
@@ -55,6 +56,11 @@ private:
consistrear,
bogie,
driveby,
// the views of consist_views (eu07/maszyna#551), left out of the cycle with consistviews off in eu07.ini
orbit,
shunting,
coupling,
trackside,
count_
};
@@ -97,6 +103,10 @@ private:
void CabView();
void ExternalView();
void DistantView( bool const Near = false );
// whether the camera shows one of the views of consist_views now
bool consist_view_active() const;
// places the camera of the view of consist_views for the frame
void update_consist_view( double const Deltatime );
void set_picking( bool const Picking );
// members
@@ -107,6 +117,7 @@ private:
int m_externalviewmode { view::consistfront }; // selected external view mode
bool m_externalview { true };
std::array<view_config, view::count_> m_externalviewconfigs;
consist_views m_consistviews; // the orbit, shunting, coupling and trackside views
TDynamicObject *pDynamicNearest { nullptr }; // vehicle nearest to the active camera. TODO: move to camera
double fTime50Hz { 0.0 }; // bufor czasu dla komunikacji z PoKeys
double const m_primaryupdaterate { 1.0 / 100.0 };

View File

@@ -388,7 +388,7 @@ moved more than 0.5 m or turned more than 0.05 rad.
|---------|------------------------------------------------------------------------------|
| `f64` | t |
| `u8` | mode: 0 from the cab, 1 an outside view of the user's train, 2 the free camera, 3 the debug camera |
| `u8` | the outside view: 0 the front of the train, 1 its rear, 2 a bogie, 3 drive-by; 0 in the other modes |
| `u8` | the outside view: 0 the front of the train, 1 its rear, 2 a bogie, 3 drive-by, 4 the orbit around a vehicle, 5 the shunting view from above, 6 beside a coupling, 7 trackside; 0 in the other modes |
| `u32` | number of the vehicle the camera goes with, 0 for none |
| `dvec3` | position |
| `f32` | heading [rad] of where it looks, as that of a vehicle: towards (sin, cos) in (x, z) |

View File

@@ -0,0 +1,386 @@
#include "stdafx.h"
#include "doctest.h"
#include <cmath>
#include <numbers>
#include "vehicle/cameraviews.h"
// the outside views of the train that go with one of its vehicles (eu07/maszyna#551): orbit, shunting from above,
// beside a coupling, trackside. The camera follows from the place of the vehicle and the keys alone.
namespace {
using cameraviews::move_keys;
// a 20 m coach standing at Position, facing Front
cameraviews::vehicle_pose coach(glm::dvec3 const &Position = {100.0, 50.0, 200.0}, glm::dvec3 const &Front = {0.0, 0.0, -1.0}) {
cameraviews::vehicle_pose pose;
pose.position = Position;
pose.front = Front;
pose.length = 20.0;
pose.width = 3.0;
pose.height = 4.0;
return pose;
}
double flat_distance(glm::dvec3 const &A, glm::dvec3 const &B) {
return std::hypot(A.x - B.x, A.z - B.z);
}
// a key held, as TCamera::Velocity has it outside the cab
double constexpr key{1.0};
} // namespace
TEST_CASE("camera views: the orbit starts outside the vehicle, ahead of it on its left, above the rails") {
auto const pose{coach()};
auto const orbit{cameraviews::orbit_start(pose)};
auto const place{cameraviews::orbit_placement(orbit, pose)};
auto const centre{cameraviews::orbit_centre(pose)};
CHECK(place.target == centre);
CHECK(glm::distance(place.eye, centre) == doctest::Approx(orbit.radius));
CHECK(orbit.radius >= cameraviews::orbit_min_radius(pose));
auto const offset{place.eye - centre};
CHECK(glm::dot(offset, pose.front) > 0.0); // ahead
CHECK(glm::dot(offset, glm::dvec3{-1.0, 0.0, 0.0}) > 0.0); // on the left of a vehicle facing -z
CHECK(place.eye.y > pose.position.y + pose.height);
}
TEST_CASE("camera views: the orbit radius stays between the body of the vehicle and its limit") {
auto const pose{coach()};
auto orbit{cameraviews::orbit_start(pose)};
// the up arrow held for a minute brings the camera as close as the body lets it
for (int frame = 0; frame < 600; ++frame) {
orbit = cameraviews::orbit_moved(orbit, move_keys{0.0, 0.0, -key}, {}, 0.1, pose);
}
CHECK(orbit.radius == doctest::Approx(cameraviews::orbit_min_radius(pose)));
// the diagonal of the body, so it's outside at any angle
CHECK(orbit.radius > 0.5 * std::hypot(pose.length, pose.width));
for (int frame = 0; frame < 600; ++frame) {
orbit = cameraviews::orbit_moved(orbit, move_keys{0.0, 0.0, key}, {}, 0.1, pose);
}
CHECK(orbit.radius == doctest::Approx(cameraviews::orbit_max_radius));
// a radius not set yet becomes the default one of the vehicle
cameraviews::orbit unset;
CHECK(cameraviews::orbit_clamped(unset, pose).radius == doctest::Approx(cameraviews::orbit_start(pose).radius));
}
TEST_CASE("camera views: the orbit keeps above the rails however low it goes") {
auto const pose{coach()};
for (auto const radius : {12.0, 25.0, 100.0, 250.0}) {
CAPTURE(radius);
auto orbit{cameraviews::orbit_start(pose)};
orbit.radius = radius;
for (int frame = 0; frame < 300; ++frame) {
orbit = cameraviews::orbit_moved(orbit, move_keys{0.0, -key, 0.0}, {}, 0.1, pose);
}
auto const place{cameraviews::orbit_placement(orbit, pose)};
CHECK(place.eye.y >= pose.position.y + cameraviews::rail_clearance - 1e-9);
CHECK(orbit.elevation >= cameraviews::orbit_min_elevation - 1e-9);
}
// up to its highest it doesn't go over the top
auto orbit{cameraviews::orbit_start(pose)};
for (int frame = 0; frame < 300; ++frame) {
orbit = cameraviews::orbit_moved(orbit, move_keys{0.0, key, 0.0}, {}, 0.1, pose);
}
CHECK(orbit.elevation == doctest::Approx(cameraviews::orbit_max_elevation));
// the mouse moved up a lot at once is held the same way
orbit = cameraviews::orbit_moved(orbit, {}, {10.0, 0.0}, 0.0, pose);
CHECK(orbit.elevation == doctest::Approx(cameraviews::orbit_lowest_elevation(pose, orbit.radius)));
}
TEST_CASE("camera views: the left key takes the orbit clockwise around the vehicle, as a step to the left") {
auto const pose{coach()};
cameraviews::orbit orbit;
orbit.radius = 30.0;
orbit = cameraviews::orbit_clamped(orbit, pose);
auto const ahead{cameraviews::orbit_placement(orbit, pose)};
CHECK(glm::dot(ahead.eye - ahead.target, pose.front) > 0.0);
// a little to the left from in front of the vehicle: the camera comes round to its right side
auto const moved{cameraviews::orbit_moved(orbit, move_keys{-key, 0.0, 0.0}, {}, 0.5, pose)};
CHECK(moved.azimuth < orbit.azimuth);
auto const place{cameraviews::orbit_placement(moved, pose)};
CHECK((place.eye - place.target).x > 0.0); // the right of a vehicle facing -z is +x
// so does the mouse moved to the right (TCamera::Angle.y goes down)
CHECK(cameraviews::orbit_moved(orbit, {}, {0.0, -0.2}, 0.0, pose).azimuth == doctest::Approx(orbit.azimuth - 0.2));
// all the way round, the azimuth stays within half a turn either way
auto round{orbit};
for (int frame = 0; frame < 200; ++frame) {
round = cameraviews::orbit_moved(round, move_keys{key, 0.0, 0.0}, {}, 0.1, pose);
REQUIRE(std::abs(round.azimuth) <= std::numbers::pi + 1e-9);
}
}
TEST_CASE("camera views: the orbit turns with the vehicle and moves with it") {
auto const north{coach({0.0, 10.0, 0.0}, {0.0, 0.0, -1.0})};
// the same vehicle further on, turned to face east (+x) on a slope
auto const east{coach({500.0, 20.0, -300.0}, glm::normalize(glm::dvec3{1.0, 0.05, 0.0}))};
auto const orbit{cameraviews::orbit_start(north)};
auto const before{cameraviews::orbit_placement(orbit, north)};
auto const after{cameraviews::orbit_placement(orbit, east)};
auto const a{before.eye - before.target};
auto const b{after.eye - after.target};
CHECK(b.y == doctest::Approx(a.y));
// a quarter turn clockwise from above: -z becomes +x, so the offset (x, z) becomes (-z, x)
CHECK(b.x == doctest::Approx(-a.z));
CHECK(b.z == doctest::Approx(a.x));
CHECK(after.target == cameraviews::orbit_centre(east));
}
TEST_CASE("camera views: a longer vehicle picked keeps the orbit outside its body") {
auto const shortvehicle{coach()};
auto longvehicle{coach()};
longvehicle.length = 40.0;
cameraviews::orbit orbit;
orbit.radius = cameraviews::orbit_min_radius(shortvehicle);
orbit = cameraviews::orbit_clamped(orbit, longvehicle);
CHECK(orbit.radius == doctest::Approx(cameraviews::orbit_min_radius(longvehicle)));
CHECK(orbit.radius > 20.0);
}
TEST_CASE("camera views: the shunting view looks straight down, the way the user drives up the screen") {
auto const pose{coach()};
cameraviews::topdown view;
view = cameraviews::topdown_clamped(view, pose, 100.0);
CHECK(view.height >= cameraviews::topdown_min_height(pose));
auto const place{cameraviews::topdown_placement(view, pose, 1)};
CHECK(place.eye.x == doctest::Approx(pose.position.x));
CHECK(place.eye.z == doctest::Approx(pose.position.z));
CHECK(place.eye.y == doctest::Approx(pose.position.y + view.height));
CHECK(place.target.x == doctest::Approx(place.eye.x));
CHECK(place.target.z == doctest::Approx(place.eye.z));
CHECK(place.up.z == doctest::Approx(-1.0)); // the front of the vehicle up the screen
// driven from the rear cab, its rear is up the screen
CHECK(cameraviews::topdown_placement(view, pose, -1).up.z == doctest::Approx(1.0));
// the vehicle turned to face east: the screen turns with it
auto const east{coach({0.0, 0.0, 0.0}, {1.0, 0.0, 0.0})};
CHECK(cameraviews::topdown_placement(view, east, 1).up.x == doctest::Approx(1.0));
}
TEST_CASE("camera views: the shunting view moves along and across the train and up, within its limits") {
auto const pose{coach()};
auto const along{60.0};
auto view{cameraviews::topdown_clamped({}, pose, along)};
auto const height{view.height};
// the up arrow moves it up the screen, the way the user drives
auto const forward{cameraviews::topdown_moved(view, move_keys{0.0, 0.0, -key}, 1.0, pose, along)};
CHECK(forward.along > 0.0);
auto const place{cameraviews::topdown_placement(forward, pose, -1)};
CHECK(place.eye.z > pose.position.z); // driven from the rear cab, up the screen is +z
// the right arrow to the right of the screen
auto const right{cameraviews::topdown_moved(view, move_keys{key, 0.0, 0.0}, 1.0, pose, along)};
CHECK(cameraviews::topdown_placement(right, pose, 1).eye.x > pose.position.x);
CHECK(cameraviews::topdown_placement(right, pose, -1).eye.x < pose.position.x);
for (int frame = 0; frame < 600; ++frame) {
view = cameraviews::topdown_moved(view, move_keys{key, -key, -key}, 0.1, pose, along);
}
CHECK(view.along == doctest::Approx(along));
CHECK(view.across == doctest::Approx(cameraviews::topdown_max_across));
CHECK(view.height == doctest::Approx(cameraviews::topdown_min_height(pose)));
CHECK(view.height < height);
for (int frame = 0; frame < 600; ++frame) {
view = cameraviews::topdown_moved(view, move_keys{0.0, key, 0.0}, 0.1, pose, along);
}
CHECK(view.height == doctest::Approx(cameraviews::topdown_max_height));
}
TEST_CASE("camera views: the way the user drives, seen from another vehicle of the train") {
glm::dvec3 const north{0.0, 0.0, -1.0};
glm::dvec3 const south{0.0, 0.0, 1.0};
CHECK(cameraviews::driving_facing(north, north, 1) == 1);
CHECK(cameraviews::driving_facing(north, north, -1) == -1);
CHECK(cameraviews::driving_facing(north, north, 0) == 1); // no cab occupied counts as the front one
// a vehicle turned round in the train
CHECK(cameraviews::driving_facing(south, north, 1) == -1);
CHECK(cameraviews::driving_facing(south, north, -1) == 1);
}
TEST_CASE("camera views: [ and ] step through the vehicles of the train and stop at its ends") {
CHECK(cameraviews::step_index(2, 5, 1) == 3);
CHECK(cameraviews::step_index(2, 5, -1) == 1);
CHECK(cameraviews::step_index(0, 5, -1) == 0);
CHECK(cameraviews::step_index(4, 5, 1) == 4);
CHECK(cameraviews::step_index(1, 5, 10) == 4);
CHECK(cameraviews::step_index(3, 1, 1) == 0); // a vehicle alone
CHECK(cameraviews::step_index(3, 0, 1) == 0); // no train
// a picked vehicle beyond the end of a train which got shorter
CHECK(cameraviews::step_index(7, 3, 0) == 2);
}
TEST_CASE("camera views: the head and the tail of a train are its ends the way it goes") {
std::vector<glm::dvec3> const positions{{0.0, 0.0, 0.0}, {0.0, 0.0, 20.0}, {0.0, 0.0, 40.0}};
CHECK(cameraviews::train_ends(positions, {0.0, 0.0, -1.0}) == std::pair{0, 2});
CHECK(cameraviews::train_ends(positions, {0.0, 0.0, 1.0}) == std::pair{2, 0});
CHECK(cameraviews::train_ends({}, {0.0, 0.0, 1.0}) == std::pair{-1, -1});
}
TEST_CASE("camera views: the coupling view starts where another vehicle comes closer") {
auto const count{5};
CHECK(cameraviews::first_coupling(-1.0, -1.0, count) == 0);
CHECK(cameraviews::first_coupling(30.0, -1.0, count) == 0);
CHECK(cameraviews::first_coupling(-1.0, 30.0, count) == count);
CHECK(cameraviews::first_coupling(50.0, 10.0, count) == count);
CHECK(cameraviews::first_coupling(10.0, 50.0, count) == 0);
// a vehicle too far away doesn't count
CHECK(cameraviews::first_coupling(-1.0, cameraviews::coupler_watch_range + 1.0, count) == 0);
}
TEST_CASE("camera views: the coupling view stands beside the coupler and steps back for the vehicle coming closer") {
cameraviews::coupler coupler;
coupler.position = {0.0, 10.0, -10.0};
coupler.outward = {0.0, 0.0, -1.0};
coupler.width = 3.0;
cameraviews::coupler_view const view;
auto const alone{cameraviews::coupler_placement(view, coupler, nullptr)};
CHECK(alone.eye.x == doctest::Approx(-(1.5 + view.distance))); // on the left of the outward way, -x
CHECK(alone.eye.z > coupler.position.z); // a little back towards its vehicle
CHECK(alone.eye.y == doctest::Approx(coupler.position.y + cameraviews::buffer_height + view.height));
CHECK(alone.target.z < coupler.position.z); // looking out past the coupler
// a vehicle 30 m away: the camera steps back from the track and looks between the two couplers
glm::dvec3 const other{0.0, 10.0, -40.0};
auto const coming{cameraviews::coupler_placement(view, coupler, &other)};
CHECK(coming.eye.x < alone.eye.x);
CHECK(coming.target.z == doctest::Approx(-25.0));
CHECK(coming.target.y == doctest::Approx(10.0 + cameraviews::buffer_height));
// closer, the camera comes back to the track with it
glm::dvec3 const closer{0.0, 10.0, -12.0};
CHECK(cameraviews::coupler_placement(view, coupler, &closer).eye.x == doctest::Approx(alone.eye.x));
// far away, it stops stepping back
glm::dvec3 const distant{0.0, 10.0, -500.0};
CHECK(cameraviews::coupler_placement(view, coupler, &distant).eye.x == doctest::Approx(-(1.5 + cameraviews::coupler_max_distance)));
}
TEST_CASE("camera views: the keys of the coupling view") {
cameraviews::coupler_view view;
view = cameraviews::coupler_moved(view, move_keys{key, 0.0, 0.0}, 0.1);
CHECK(view.side == -1.0);
view = cameraviews::coupler_moved(view, {}, 0.1);
CHECK(view.side == -1.0); // stays on its side without the key
view = cameraviews::coupler_moved(view, move_keys{-key, 0.0, 0.0}, 0.1);
CHECK(view.side == 1.0);
for (int frame = 0; frame < 600; ++frame) {
view = cameraviews::coupler_moved(view, move_keys{0.0, key, key}, 0.1);
}
CHECK(view.distance == doctest::Approx(cameraviews::coupler_max_distance));
CHECK(view.height == doctest::Approx(cameraviews::coupler_max_height));
for (int frame = 0; frame < 600; ++frame) {
view = cameraviews::coupler_moved(view, move_keys{0.0, -key, -key}, 0.1);
}
CHECK(view.distance == doctest::Approx(cameraviews::coupler_min_distance));
CHECK(view.height == doctest::Approx(cameraviews::coupler_min_height));
// the lowest still above the rails
CHECK(cameraviews::buffer_height + cameraviews::coupler_min_height > 0.0);
}
TEST_CASE("camera views: along an arc of the line, and its curvature from two vehicles") {
glm::dvec3 const start{0.0, 5.0, 0.0};
glm::dvec3 const north{0.0, 0.0, -1.0};
auto const [straight, straightway]{cameraviews::along_arc(start, north, 0.0, 100.0)};
CHECK(straight.z == doctest::Approx(-100.0));
CHECK(straight.y == doctest::Approx(5.0));
CHECK(straightway.z == doctest::Approx(-1.0));
// a quarter of a circle of 500 m to the left: from going north to going west
auto const radius{500.0};
auto const [point, way]{cameraviews::along_arc(start, north, 1.0 / radius, 0.5 * std::numbers::pi * radius)};
CHECK(point.x == doctest::Approx(-radius));
CHECK(point.z == doctest::Approx(-radius));
CHECK(way.x == doctest::Approx(-1.0));
CHECK(way.z == doctest::Approx(0.0).epsilon(1e-9));
// two vehicles 25 m apart on that circle, the one ahead turned to the left of the one behind
auto const angle{25.0 / radius};
glm::dvec3 const centre{-radius, 0.0, 0.0};
glm::dvec3 const behind{0.0, 0.0, 0.0};
glm::dvec3 const ahead{centre + glm::dvec3{radius * std::cos(angle), 0.0, -radius * std::sin(angle)}};
glm::dvec3 const aheadway{-std::sin(angle), 0.0, -std::cos(angle)};
auto const curvature{cameraviews::line_curvature(ahead, aheadway, behind, north)};
CHECK(curvature == doctest::Approx(1.0 / radius).epsilon(0.01));
// the other way round it turns to the right
CHECK(cameraviews::line_curvature(ahead, glm::dvec3{-aheadway.x, 0.0, aheadway.z}, behind, north) < 0.0);
// too close to tell
CHECK(cameraviews::line_curvature(behind, aheadway, behind, north) == 0.0);
}
TEST_CASE("camera views: the railfan waits for the train ahead of it and moves on once it's gone by") {
cameraviews::train_motion train;
train.direction = {0.0, 0.0, -1.0};
train.head = {0.0, 0.0, 0.0};
train.tail = {0.0, 0.0, 100.0};
train.speed = 20.0; // 72 km/h
train.width = 3.0;
cameraviews::trackside view;
REQUIRE(cameraviews::trackside_expired(view, train));
view = cameraviews::trackside_next(view, train);
REQUIRE(view.placed);
// ahead of the head by the 15 s it needs to get there, beside the line on the right, at the eye of a man
CHECK(view.spot.z == doctest::Approx(-20.0 * cameraviews::trackside_lead_time));
CHECK(view.spot.x == doctest::Approx(1.5 + cameraviews::trackside_distance));
CHECK(view.spot.y == doctest::Approx(cameraviews::trackside_eye_height));
// the train comes and goes by: the railfan stays until its tail is well past
auto const spot{view.spot};
for (int step = 0; step < 50; ++step) {
auto const run{10.0 * step};
auto passing{train};
passing.head.z -= run;
passing.tail.z -= run;
auto const tailpast{spot.z - passing.tail.z};
CAPTURE(run);
CHECK(cameraviews::trackside_expired(view, passing) == (tailpast > cameraviews::trackside_passed));
}
// and then walks to the other side of the line, ahead again
auto gone{train};
gone.head.z -= 500.0;
gone.tail.z -= 500.0;
auto const next{cameraviews::trackside_next(view, gone)};
CHECK(next.side == -view.side);
CHECK(next.spot.x == doctest::Approx(-(1.5 + cameraviews::trackside_distance)));
CHECK(next.spot.z < gone.head.z);
CHECK(false == cameraviews::trackside_expired(next, gone));
}
TEST_CASE("camera views: the railfan's spot ahead keeps to the line in a curve, and nearby when the train stands") {
cameraviews::train_motion train;
train.direction = {0.0, 0.0, -1.0};
train.speed = 40.0;
auto const radius{400.0};
train.curvature = 1.0 / radius;
cameraviews::trackside const view;
auto const next{cameraviews::trackside_next(view, train)};
// the centre of the curve is on the left of the head, the railfan on the right of the line: outside the circle
glm::dvec3 const centre{-radius, 0.0, 0.0};
CHECK(flat_distance(next.spot, centre) == doctest::Approx(radius + 0.5 * train.width + cameraviews::trackside_distance));
// no further round than a quarter of the circle
CHECK(next.spot.z >= -radius - 1e-6);
// a train standing still gets a railfan at the shortest lead
auto standing{train};
standing.speed = 0.0;
standing.curvature = 0.0;
auto const nearby{cameraviews::trackside_next(view, standing)};
CHECK(nearby.spot.z == doctest::Approx(-cameraviews::trackside_min_lead));
// and as long as it stands, the railfan stays
CHECK(false == cameraviews::trackside_expired(nearby, standing));
// a train gone far away (moved by the user, or a long way back) gets a new railfan
auto away{standing};
away.head = {0.0, 0.0, 2000.0};
away.tail = {0.0, 0.0, 2100.0};
CHECK(cameraviews::trackside_expired(nearby, away));
}

View File

@@ -443,6 +443,13 @@ bool global_settings::ConfigParseInput(cParser& Parser, const std::string& token
return true;
}
if (token == "consistviews")
{
// no: [Shift]+[F4] cycles through the four views of the train it had before the views of its vehicles
ParseOne(Parser, ConsistViews);
return true;
}
if (token == "input.gamepad")
{
ParseOne(Parser, InputGamepad);
@@ -1746,6 +1753,7 @@ global_settings::export_as_text( std::ostream &Output ) const {
<< fMouseXScale << " "
<< fMouseYScale << "\n";
export_as_text( Output, "mousecontrol", InputMouse );
export_as_text( Output, "consistviews", ConsistViews );
export_as_text( Output, "enabletraction", bEnableTraction );
export_as_text( Output, "loadtraction", bLoadTraction );
export_as_text( Output, "friction", fFriction );

View File

@@ -232,6 +232,7 @@ struct global_settings {
int iFeedbackPort{ 0 }; // dodatkowy adres dla informacji zwrotnych
bool InputGamepad{ true }; // whether gamepad support is enabled
bool InputMouse{ true }; // whether control pick mode can be activated
bool ConsistViews{ true }; // whether [Shift]+[F4] goes on to the orbit, shunting, coupling and trackside views of the train (eu07/maszyna#551)
double fBrakeStep { 1.0 }; // krok zmiany hamulca innych niż FV4a dla klawiszy [Num3] i [Num9]
double brake_speed { 3.0 }; // prędkość przesuwu hamulca dla FV4a
// parametry kalibracyjne wejść z pulpitu

View File

@@ -301,7 +301,7 @@ struct station_event {
struct camera_state {
double time{0.0};
std::uint8_t mode{camera_modes::free};
std::uint8_t view{0}; // of an outside view: 0 the front of the train, 1 its rear, 2 a bogie, 3 drive-by
std::uint8_t view{0}; // of an outside view: 0 the front of the train, 1 its rear, 2 a bogie, 3 drive-by, 4 orbit, 5 shunting, 6 coupling, 7 trackside
std::uint32_t vehicle{no_vehicle}; // the vehicle the camera goes with, no_vehicle for none
glm::dvec3 position{0.0};
float heading{0.0f}; // [rad] of where it looks, from +z towards +x, as the heading of a vehicle

281
vehicle/cameraviews.cpp Normal file
View File

@@ -0,0 +1,281 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "vehicle/cameraviews.h"
#include <algorithm>
#include <cmath>
#include <numbers>
namespace cameraviews {
namespace {
glm::dvec3 const world_up{0.0, 1.0, 0.0};
// radians per second of the left and right keys, and of the page keys, for a key held
double constexpr orbit_turn_rate{1.2};
double constexpr orbit_climb_rate{0.8};
// the radius grows (or shrinks) e times in a second of a key held
double constexpr orbit_zoom_rate{1.0};
// the height of the shunting view grows (or shrinks) e times in a second of a key held
double constexpr topdown_zoom_rate{0.8};
// metres per second of the shunting view moved along and across, for each metre of its height
double constexpr topdown_pan_rate{0.6};
// metres per second of the coupling view moved closer, further, up and down
double constexpr coupler_move_rate{3.0};
// metres the coupling view stands back from the coupler, towards its vehicle
double constexpr coupler_step_back{2.0};
// the share of the gap to the vehicle coming closer the coupling view keeps from the side of the train
double constexpr coupler_gap_share{0.7};
double flat_distance(glm::dvec3 const &A, glm::dvec3 const &B) {
return std::hypot(A.x - B.x, A.z - B.z);
}
} // namespace
glm::dvec3 flat_front(glm::dvec3 const &Front) {
glm::dvec3 const flat{Front.x, 0.0, Front.z};
auto const length{glm::length(flat)};
if (length < 1e-6) {
return {0.0, 0.0, -1.0};
}
return flat / length;
}
glm::dvec3 flat_left(glm::dvec3 const &Flatfront) {
return glm::cross(world_up, Flatfront);
}
int step_index(int const Index, int const Count, int const Step) {
if (Count <= 0) {
return 0;
}
return std::clamp(Index + Step, 0, Count - 1);
}
std::pair<int, int> train_ends(std::vector<glm::dvec3> const &Positions, glm::dvec3 const &Direction) {
if (Positions.empty()) {
return {-1, -1};
}
auto const way{flat_front(Direction)};
auto const along{[&way](glm::dvec3 const &Position) { return glm::dot(Position, way); }};
auto const [tail, head]{std::ranges::minmax_element(Positions, {}, along)};
return {static_cast<int>(head - Positions.begin()), static_cast<int>(tail - Positions.begin())};
}
int driving_facing(glm::dvec3 const &Front, glm::dvec3 const &Drivenfront, int const Cab) {
auto const cab{Cab < 0 ? -1 : 1};
auto const aligned{glm::dot(flat_front(Front), flat_front(Drivenfront)) < 0.0 ? -1 : 1};
return cab * aligned;
}
// orbit
glm::dvec3 orbit_centre(vehicle_pose const &Vehicle) {
return Vehicle.position + world_up * (0.4 * Vehicle.height);
}
double orbit_min_radius(vehicle_pose const &Vehicle) {
return 0.5 * std::hypot(Vehicle.length, Vehicle.width) + 1.0;
}
orbit orbit_start(vehicle_pose const &Vehicle) {
orbit start;
start.azimuth = 0.6;
start.elevation = 0.25;
start.radius = std::max(orbit_min_radius(Vehicle), Vehicle.length + 5.0);
return orbit_clamped(start, Vehicle);
}
double orbit_lowest_elevation(vehicle_pose const &Vehicle, double const Radius) {
auto const centre{0.4 * Vehicle.height};
auto const sine{std::clamp((rail_clearance - centre) / std::max(Radius, 1e-3), -1.0, 1.0)};
return std::max(orbit_min_elevation, std::asin(sine));
}
orbit orbit_clamped(orbit Orbit, vehicle_pose const &Vehicle) {
if (Orbit.radius <= 0.0) {
Orbit.radius = orbit_start(Vehicle).radius;
}
Orbit.radius = std::clamp(Orbit.radius, orbit_min_radius(Vehicle), std::max(orbit_min_radius(Vehicle), orbit_max_radius));
Orbit.elevation = std::clamp(Orbit.elevation, orbit_lowest_elevation(Vehicle, Orbit.radius), orbit_max_elevation);
Orbit.azimuth = std::remainder(Orbit.azimuth, 2.0 * std::numbers::pi);
return Orbit;
}
orbit orbit_moved(orbit Orbit, move_keys const &Keys, glm::dvec2 const &Turn, double const Seconds, vehicle_pose const &Vehicle) {
Orbit = orbit_clamped(Orbit, Vehicle);
// the left key takes the camera to the left around the vehicle, as a step to the left would while looking at it:
// clockwise seen from above. so does the mouse moved to the right, which turns the view to the right
Orbit.azimuth += Keys.x * orbit_turn_rate * Seconds + Turn.y;
// the mouse moved down looks down at the vehicle, from higher up
Orbit.elevation += Keys.y * orbit_climb_rate * Seconds - Turn.x;
// the up arrow (a negative z) brings the camera closer
Orbit.radius *= std::exp(Keys.z * orbit_zoom_rate * Seconds);
return orbit_clamped(Orbit, Vehicle);
}
placement orbit_placement(orbit const &Orbit, vehicle_pose const &Vehicle) {
auto const front{flat_front(Vehicle.front)};
auto const left{flat_left(front)};
auto const around{std::cos(Orbit.azimuth) * front + std::sin(Orbit.azimuth) * left};
auto const way{std::cos(Orbit.elevation) * around + std::sin(Orbit.elevation) * world_up};
placement place;
place.target = orbit_centre(Vehicle);
place.eye = place.target + Orbit.radius * way;
place.up = world_up;
return place;
}
// shunting view
double topdown_min_height(vehicle_pose const &Vehicle) {
return Vehicle.height + 3.0;
}
topdown topdown_clamped(topdown View, vehicle_pose const &Vehicle, double const Alonglimit) {
auto const lowest{topdown_min_height(Vehicle)};
if (View.height <= 0.0) {
View.height = 1.5 * Vehicle.length;
}
View.height = std::clamp(View.height, lowest, std::max(lowest, topdown_max_height));
auto const along{std::max(0.0, Alonglimit)};
View.along = std::clamp(View.along, -along, along);
View.across = std::clamp(View.across, -topdown_max_across, topdown_max_across);
return View;
}
topdown topdown_moved(topdown View, move_keys const &Keys, double const Seconds, vehicle_pose const &Vehicle, double const Alonglimit) {
View = topdown_clamped(View, Vehicle, Alonglimit);
auto const pan{View.height * topdown_pan_rate * Seconds};
// the up arrow (a negative z) moves the view up the screen, the way the user drives
View.along -= Keys.z * pan;
View.across += Keys.x * pan;
View.height *= std::exp(Keys.y * topdown_zoom_rate * Seconds);
return topdown_clamped(View, Vehicle, Alonglimit);
}
placement topdown_placement(topdown const &View, vehicle_pose const &Vehicle, int const Facing) {
auto const up{flat_front(Vehicle.front) * (Facing < 0 ? -1.0 : 1.0)};
// looking down with the screen up the way the user drives, the right of the screen is the right of that way
auto const right{-flat_left(up)};
placement place;
auto const ground{Vehicle.position + up * View.along + right * View.across};
place.target = ground;
place.eye = ground + world_up * View.height;
place.up = up;
return place;
}
// coupling view
coupler_view coupler_moved(coupler_view View, move_keys const &Keys, double const Seconds) {
if (Keys.x < 0.0) {
View.side = 1.0;
}
else if (Keys.x > 0.0) {
View.side = -1.0;
}
View.distance = std::clamp(View.distance + Keys.z * coupler_move_rate * Seconds, coupler_min_distance, coupler_max_distance);
View.height = std::clamp(View.height + Keys.y * coupler_move_rate * Seconds, coupler_min_height, coupler_max_height);
return View;
}
placement coupler_placement(coupler_view const &View, coupler const &Coupler, glm::dvec3 const *Other) {
auto const outward{flat_front(Coupler.outward)};
auto const aside{flat_left(outward) * (View.side < 0.0 ? -1.0 : 1.0)};
auto const buffers{Coupler.position + world_up * buffer_height};
auto distance{View.distance};
placement place;
if (Other != nullptr) {
auto const otherbuffers{*Other + world_up * buffer_height};
distance = std::max(distance, coupler_gap_share * flat_distance(Coupler.position, *Other));
place.target = 0.5 * (buffers + otherbuffers);
}
else {
place.target = buffers + outward;
}
distance = std::min(distance, coupler_max_distance);
place.eye = buffers - outward * coupler_step_back + aside * (0.5 * Coupler.width + distance) + world_up * View.height;
place.up = world_up;
return place;
}
int first_coupling(double const Frontgap, double const Reargap, int const Count) {
auto const watched{[](double const Gap) { return Gap >= 0.0 && Gap <= coupler_watch_range; }};
auto const front{watched(Frontgap)};
auto const rear{watched(Reargap)};
if (rear && (false == front || Reargap < Frontgap)) {
return std::max(Count, 0);
}
return 0;
}
// trackside view
std::pair<glm::dvec3, glm::dvec3> along_arc(glm::dvec3 const &Start, glm::dvec3 const &Direction, double const Curvature, double const Distance) {
auto const way{flat_front(Direction)};
if (std::abs(Curvature) < 1e-6) {
return {Start + way * Distance, way};
}
auto const left{flat_left(way)};
auto const angle{Curvature * Distance};
auto const point{Start + way * (std::sin(angle) / Curvature) + left * ((1.0 - std::cos(angle)) / Curvature)};
auto const heading{way * std::cos(angle) + left * std::sin(angle)};
return {point, heading};
}
double line_curvature(glm::dvec3 const &Positiona, glm::dvec3 const &Directiona, glm::dvec3 const &Positionb, glm::dvec3 const &Directionb) {
auto const distance{flat_distance(Positiona, Positionb)};
if (distance < 1.0) {
return 0.0;
}
auto const ahead{flat_front(Directiona)};
auto const behind{flat_front(Directionb)};
// the vehicle ahead turned to the left of the one behind it: a turn about the up axis, anticlockwise from above
auto const angle{std::atan2(glm::cross(behind, ahead).y, glm::dot(behind, ahead))};
return angle / distance;
}
bool trackside_expired(trackside const &View, train_motion const &Train) {
if (false == View.placed) {
return true;
}
auto const way{flat_front(Train.direction)};
auto const offset{View.spot - Train.tail};
auto const passed{glm::dot(glm::dvec3{offset.x, 0.0, offset.z}, way) < -trackside_passed};
return passed || flat_distance(View.spot, Train.head) > trackside_max_distance;
}
trackside trackside_next(trackside const &View, train_motion const &Train) {
auto lead{std::clamp(Train.speed * trackside_lead_time, trackside_min_lead, trackside_max_lead)};
// along a sharp curve the line comes round: the spot stays within a quarter of the circle
if (auto const quarter{0.5 * std::numbers::pi}; std::abs(Train.curvature) * lead > quarter) {
lead = quarter / std::abs(Train.curvature);
}
auto const [point, heading]{along_arc(Train.head, Train.direction, Train.curvature, lead)};
trackside next;
next.side = View.placed ? -View.side : View.side;
next.spot = point + flat_left(heading) * (next.side * (0.5 * Train.width + trackside_distance)) + world_up * trackside_eye_height;
next.placed = true;
return next;
}
placement trackside_placement(trackside const &View, glm::dvec3 const &Target) {
placement place;
place.eye = View.spot;
place.target = Target;
place.up = world_up;
return place;
}
} // namespace cameraviews

182
vehicle/cameraviews.h Normal file
View File

@@ -0,0 +1,182 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <utility>
#include <vector>
#include <glm/glm.hpp>
// The outside views of the user's train that go with one of its vehicles (eu07/maszyna#551): the orbit around a
// vehicle, the shunting view from above, the view beside a coupling and the trackside view of a railfan. Each one
// is worked out here from the place of the vehicle and the keys of the user alone, so it can be tested without the
// game; application/consistviews.cpp hands over the vehicles of the train and turns the results into the camera.
// The world has y up; the rails are at the height of the place of a vehicle (TDynamicObject::GetPosition).
namespace cameraviews {
// a vehicle of the train, as the views see it
struct vehicle_pose {
glm::dvec3 position{}; // its middle, at the height of the rails
glm::dvec3 front{0.0, 0.0, -1.0}; // unit, towards its front coupler
double length{20.0};
double width{3.0};
double height{4.0};
};
// where the camera stands, the point it looks at and its up
struct placement {
glm::dvec3 eye{};
glm::dvec3 target{};
glm::dvec3 up{0.0, 1.0, 0.0};
};
// the movement keys of the camera as TCamera::Velocity has them outside the cab: x to the right, y up, z backwards
// (the up arrow gives a negative z), 1 for a key held, 5 with ctrl
using move_keys = glm::dvec3;
// the Front of a vehicle flattened on the ground, unit; a vehicle standing upright (no heading) faces -z
glm::dvec3 flat_front(glm::dvec3 const &Front);
// to the left of the flat Front, unit
glm::dvec3 flat_left(glm::dvec3 const &Flatfront);
// the index after Step vehicles (or couplings) from Index, among Count of them, held at the first and the last
int step_index(int Index, int Count, int Step);
// the two ends of a train whose vehicles stand at Positions, going in the Direction: the index of the vehicle
// at its head (furthest along the Direction) and at its tail
std::pair<int, int> train_ends(std::vector<glm::dvec3> const &Positions, glm::dvec3 const &Direction);
// +1 when the front of the vehicle the view goes with points the way the user drives, -1 when it points back:
// the Front of that vehicle, the Front of the vehicle the user drives and its occupied cab (1 the front one,
// -1 the rear one, 0 none)
int driving_facing(glm::dvec3 const &Front, glm::dvec3 const &Drivenfront, int Cab);
// orbit: around the middle of the vehicle, the left and right keys (and the mouse) turn the camera around it,
// the up and down arrows bring it closer and further away, the page keys (and the mouse) raise and lower it
struct orbit {
double azimuth{0.0}; // radians around the vehicle from its front, towards its left
double elevation{0.0}; // radians above the horizon of the middle of the vehicle
double radius{0.0}; // metres from the middle of the vehicle, 0 for the default one of the vehicle
};
inline constexpr double orbit_max_radius{250.0};
inline constexpr double orbit_max_elevation{1.4}; // radians, about 80 degrees
inline constexpr double orbit_min_elevation{-0.5}; // radians, about -30 degrees, if the rails let it
inline constexpr double rail_clearance{0.5}; // metres the camera keeps above the rails
// the first orbit around a vehicle: ahead of it on its left, a little above it, as far as the vehicle is long
orbit orbit_start(vehicle_pose const &Vehicle);
// the point the orbit goes around: the middle of the vehicle, a little lower than half of its height
glm::dvec3 orbit_centre(vehicle_pose const &Vehicle);
// the nearest the camera comes to the middle of the Vehicle: outside its body whichever way it looks
double orbit_min_radius(vehicle_pose const &Vehicle);
// the lowest elevation at the Radius which keeps the camera above the rails
double orbit_lowest_elevation(vehicle_pose const &Vehicle, double Radius);
// the Orbit held within its limits around the Vehicle (radius 0 becomes the default one)
orbit orbit_clamped(orbit Orbit, vehicle_pose const &Vehicle);
// the Orbit after Seconds of the Keys held and the mouse moved by Turn (radians: x up and down, y to the left and
// right, as TCamera::Angle takes them), within its limits around the Vehicle
orbit orbit_moved(orbit Orbit, move_keys const &Keys, glm::dvec2 const &Turn, double Seconds, vehicle_pose const &Vehicle);
// the camera of the Orbit around the Vehicle, looking at its middle
placement orbit_placement(orbit const &Orbit, vehicle_pose const &Vehicle);
// shunting view: straight down from above the vehicle, the way the user drives up the screen; it goes with the
// vehicle, the up and down arrows move it along the train, left and right across, the page keys up and down
struct topdown {
double height{0.0}; // metres above the rails, 0 for the default one of the vehicle
double along{0.0}; // metres from the middle of the vehicle along it, the way the user drives
double across{0.0}; // metres from the middle of the vehicle across it, to the right of the screen
};
inline constexpr double topdown_max_height{300.0};
inline constexpr double topdown_max_across{60.0};
// the lowest the camera comes: above the roof of the Vehicle
double topdown_min_height(vehicle_pose const &Vehicle);
// the View held within its limits: Alonglimit metres along the vehicle either way
topdown topdown_clamped(topdown View, vehicle_pose const &Vehicle, double Alonglimit);
// the View after Seconds of the Keys held; the further the camera, the faster it moves
topdown topdown_moved(topdown View, move_keys const &Keys, double Seconds, vehicle_pose const &Vehicle, double Alonglimit);
// the camera of the View over the Vehicle; Facing +1 puts the front of the vehicle up the screen, -1 its rear
placement topdown_placement(topdown const &View, vehicle_pose const &Vehicle, int Facing);
// coupling view: beside a coupler, low, looking along the train at it; when another vehicle comes closer to it
// the camera steps back from the track to keep both couplers in sight, looking between them
inline constexpr double buffer_height{1.06}; // metres above the rails
struct coupler {
glm::dvec3 position{}; // the coupler at the height of the rails (TDynamicObject::CouplerPosition)
glm::dvec3 outward{0.0, 0.0, -1.0}; // unit, from its vehicle out through the coupler
double width{3.0}; // of its vehicle
};
struct coupler_view {
double side{1.0}; // 1 on the left of the outward way, -1 on the right
double height{0.4}; // metres above the buffers
double distance{2.5}; // metres from the side of the vehicle, before the vehicle coming closer moves it further
};
inline constexpr double coupler_min_distance{1.0};
inline constexpr double coupler_max_distance{40.0};
inline constexpr double coupler_min_height{-0.8}; // metres above the buffers: a little above the rails
inline constexpr double coupler_max_height{10.0};
inline constexpr double coupler_watch_range{150.0}; // metres to a vehicle that still counts as coming closer
// the View after Seconds of the Keys held: left and right move it to that side of the track, the arrows closer
// and further, the page keys up and down
coupler_view coupler_moved(coupler_view View, move_keys const &Keys, double Seconds);
// the camera beside the Coupler; Other is the facing coupler of the vehicle coming closer, when there is one
placement coupler_placement(coupler_view const &View, coupler const &Coupler, glm::dvec3 const *Other);
// the coupling of a train of Count vehicles the view starts at, numbered from 0 (its front end) to Count (its rear
// end): the end with another vehicle, not coupled, closer than coupler_watch_range (the closer one of the two),
// otherwise the front. Frontgap and Reargap are the distances to such vehicles, negative for none
int first_coupling(double Frontgap, double Reargap, int Count);
// trackside view: a railfan standing beside the line ahead of the train, looking at it as it comes and goes by;
// once the whole train has gone past (or gone far away) the railfan walks to a new spot ahead of it, on the other
// side of the line
struct trackside {
glm::dvec3 spot{}; // the eye of the railfan
double side{-1.0}; // 1 on the left of the line, -1 on the right
bool placed{false};
};
// the train as the trackside view sees it
struct train_motion {
glm::dvec3 head{}; // the vehicle at its head, the way it goes, at the height of the rails
glm::dvec3 tail{}; // the vehicle at its tail
glm::dvec3 direction{0.0, 0.0, -1.0}; // unit, flat, the way it goes
double speed{0.0}; // m/s
double curvature{0.0}; // 1/m of the line at the head, positive turning left
double width{3.0}; // of the vehicles
};
inline constexpr double trackside_eye_height{1.7};
inline constexpr double trackside_distance{8.0}; // metres from the side of the train
inline constexpr double trackside_passed{40.0}; // metres the tail goes past the spot before the railfan moves on
inline constexpr double trackside_max_distance{1000.0}; // metres to the head before the railfan moves on anyway
inline constexpr double trackside_lead_time{15.0}; // seconds the train needs to reach the new spot
inline constexpr double trackside_min_lead{80.0};
inline constexpr double trackside_max_lead{500.0};
// whether the railfan at the View has seen the Train go by and moves on
bool trackside_expired(trackside const &View, train_motion const &Train);
// the next spot of the railfan, ahead of the Train along its line, on the other side than the View had
trackside trackside_next(trackside const &View, train_motion const &Train);
// the camera of the railfan at the View, looking at the Target
placement trackside_placement(trackside const &View, glm::dvec3 const &Target);
// the point and the way Distance metres on from Start going in the (flat, unit) Direction along a line which
// turns by Curvature (1/m, positive to the left)
std::pair<glm::dvec3, glm::dvec3> along_arc(glm::dvec3 const &Start, glm::dvec3 const &Direction, double Curvature, double Distance);
// the curvature of the line under two vehicles of a train going its way: A ahead of B, each at its Position with
// its front (or rear) towards the way of the train; 0 when they stand too close to tell
double line_curvature(glm::dvec3 const &Positiona, glm::dvec3 const &Directiona, glm::dvec3 const &Positionb, glm::dvec3 const &Directionb);
} // namespace cameraviews