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

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