mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
* feat(physics): the motion of a derailed vehicle on the ground With physics.derailment new a vehicle off the rails gets a motion of its own on the ground (vehicle/derailmotion.h, in eu07_core): it runs on along its heading, turned out of the curve by the sideways speed it left the rail with, slides across it down slopes steeper than the friction holds, follows the height and the slope of the ground under its supports, and tips over about the wheels of one side (while still guided by the curve, or on a slope, or sliding sideways) until it lies on its side. The vehicle holds the motion and what derailed it (the cause, the lateral acceleration out of the curve, the side a coupling pulled it to). While the motion runs, its friction (0.1 tipping on the outer wheels, 0.3 upright, up to 0.5 on its side) and the grade under it go into the longitudinal force. Off the end of the track the vehicle now slides on with the friction of the ballast instead of stopping within g, and a coupling can pull a vehicle off the rails (pull_off). Nothing of it with physics.derailment monitor, old or off. * feat(physics): a derailed vehicle leaves the track in the scenery TDynamicObject begins the motion of vehicle/derailmotion.h once its vehicle derails with physics.derailment new, and from then on places the model by it: off the axis of the track, pitched and rolled by the ground, on its side once it falls over, the bogies straight under the body. The ground is the scenery's terrain around the place (its opaque shapes and the heightmap terrain), with the ballast bed of the track over it; level ground at the top of the ballast where the scenery has none. The axles keep going along the track by the projection of the move, so the vehicle stays on the track nearest to it for its neighbours, the events and the drawing. A coupled vehicle still on the rails is pulled off when the sideways part of the coupler force puts its leading wheel over the climbing limit over 2 m. An uncoupled vehicle on the track and one off it run into each other only when in line. The derail sound loops while the vehicle scrapes along, and the log tells where it is every second and where it came to rest. Move() is split into move_axles() and place_by_axles(), the code of both unchanged. * feat(blackbox): a vehicle leaving the track and coming to rest off it A new record of the black box, 0x38 derailment, written when a vehicle leaves the track (physics.derailment new) and when it comes to rest off it: what derailed it, its position, heading, speed, offset from the axis of the track, roll, the distance it went and whether it lies on its side (blackbox_doc.md). The scenario runner reads it with the leaves_track check, and seven cases run it on td.scn: an EU07 with coaches into the buckled curve at 46 km/h (the AI) and at 133 km/h, over the overturning speed of the R 600 m curve at 190 km/h, off the end of a track, and overturning at 150 km/h on a curve of R 300 m on level ground, on an embankment and in a cutting. The runner also drops the cached geometry of a case's scenery (.sbt) once the case's fragment is newer than it, as it did for the base scenery: the curve cases carry their own terrain. * feat(physics): the yaw of a derailed body, and what differs between vehicles The body off the track (physics.derailment new) now turns about its vertical axis by itself, apart from the way it travels. Its heading is the way the vehicle goes, along which the Mover's speed runs; its axis is the way its front faces, with a yaw rate and the inertia of a box of its length and width. The friction of the ground holds it: under the two ends of its base while upright, along all of its length on its side, 0.3 to 0.5 along the body and 0.5 across it, each point against the way it slides there. Turning resists the yaw; a body going askew slides harder (mu along cos^2 + mu across sin^2 of the slip, which the Mover takes), its heading is drawn to the body and the sideways scrubbing trips it over. At a standstill it doesn't turn while the friction holds. The forces of the couplings at its ends turn it: two ends meet along the bisector of the bend between the bodies, the push goes through the inner buffer of the bend, off the middle of the beam by up to half the buffer spacing, as far as the buffers meet in height (a derailed body is lower than one on the rails, a rolled one misses them) and as much as a buffer takes (1 MN). Pushed from behind past what the buffers hold, a train of bodies bends further at each joint, the next one the other way: the zigzag of a train folding up. The gap of two ends is taken where they are, not from the distance between the middles, which would press the buffers of every bent pair. Past a right angle the heading turns about with the speed, so the Mover's front stays the body's front. Each vehicle draws its own scatter from the seed of the run and its name: the departure angle +-0.01 rad, yaw +-0.02 rad and +-0.02 rad/s, the friction under each half of it 0.85..1.15, and in two vehicles of three its leading end digging in 0.2..1.5 s after it left the rails, for 0.3..0.8 s, two to four times the friction under that half at one wheel or corner. The same seed gives the same outcome. * feat(physics): a derailed train folds up, and the derailment watches its own cost TDynamicObject gives the body off the track the forces of its couplings as vectors: the gap of a coupling with a vehicle off the track (coupled or meeting it) is that of the two ends as they are, and the force of each end goes to the body at the buffer it pushes through, or towards the other beam it is pulled to. A vehicle still on the rails coupled to one off it is taken off by the part of that force across its axis on its leading wheels (Y/Q over 1.2 over 2 m), pushed by the buffers as well as pulled by the coupling, to the side the force goes. Each vehicle off the track draws its scatter from the seed of the run and its name, and the model and the coupling points follow the yaw of the body. What a derailment costs is watched: the terrain is gathered again at most once a second per vehicle and kept to the nearest 400 000 triangles, the axles of a vehicle off the track follow it along the track by 10 cm steps (a body rocking in place moved them to and fro over the ends of tracks, firing their events every frame), and the gathering, a frame of the motion and the whole update of a vehicle off the track write a warning to the log when they take over 20 ms. The scenario runner gets the consist_folds check: the headings of a train's vehicles at rest off the track differ by a given angle at a given number of joints, optionally alternating, optionally on both sides of the track. eu07_td_off_track_buckle_fast checks the folding, eu07_td_off_track_buckle the coupled coach pulled off at an angle to the locomotive, and the bounds of the other cases are narrowed to the new outcomes. * feat(physics): derailed bodies run into each other instead of through With physics.derailment new the bodies of the vehicles off the track no longer go into each other, nor into the vehicles near them: once a frame, before the vehicles update, every vehicle off the track is checked against those around it (its own train coupled or not, a vehicle still on the rails, another train), by bounding circles and then by the footprints of their bodies: the box of each, less its buffers, rolled and pitched as it lies, seen from above, with the height it reaches. Two footprints in each other are separated along the axis they overlap least across. A hit is an impulse at the point of contact, which takes away the speed the two close in with there (they part with a fifth of it) with the mass, the vertical inertia and the arm of each, and the friction of their sides sliding along each other (0.3). It goes into the velocity and the yaw rate of a body off the track, which then goes the way it is thrown, and into the speed of a vehicle on the rails along them; across the rails it pushes on the leading wheels of that vehicle, which is pushed off when the Y/Q passes 1.2 over 2 m of sliding, or three times that at once. What the bodies still overlap by is taken out by moving them apart. The buffers of two ends keep meeting through meet() and force_on(); the bodies, shorter by the buffers, touch only once the ends are pressed past them or folded corner to corner. The log has the most any two bodies overlapped by every second, and the runner checks it with no_overlap in all off-track cases: before this the coaches went up to 1.64 m into each other at 190 km/h and 0.68 m stayed in each other at rest at 133 km/h; now at most 0.10 m on the way and none at rest. * fix(sonar): the reliability and security issues of the derailment SonarCloud failed the quality gate of the pull request on the new reliability and security ratings: - cpp:S836 (dereference of an undefined pointer value) in the timing watch of TDynamicObject::Update: the analyser lost the reference held in a std::optional. The watch is now always made, with a null vehicle when it is on the track, and does nothing then; copying it is deleted. Only the timing of the update off the track and its warning use it, the physics is the same. - cpp:S2245 (pseudorandom number generator) on the std::mt19937 of draw_scatter: it is seeded on purpose so that a derailment repeats, and nothing secret is drawn; marked NOSONAR like the seeded generators of the editor self tests. - cpp:S2193 (a for loop counter of floating type) in three loops of tests/unit/test_derail_motion.cpp: they count integer steps now. * fix(physics): the scraping of a derailed vehicle stops when it is put back on the rails The derail: sound loops while a vehicle off the track slides, and it was stopped only while the vehicle was still off the track, once it came to rest. rerail() (resetconsist, SetDamage taking dtrain_out off) ends the motion off the track at once, also while the vehicle still slides, and the scraping then went on for good. The vehicle now remembers that it plays the loop and stops it as soon as it isn't sliding off the track any more, at rest or back on the rails. The one-shot derail: sound of the other derailment models is not touched. * fix(physics): a vehicle put back on the rails forgets what derailed it rerail() reset only the side a coupling pulled the vehicle off to, and kept the cause and the lateral acceleration of the derailment. A vehicle that is put back on the rails and later leaves them in a way which doesn't set those (as the fork derailment of #496 does now) would leave the track with the cause and the acceleration of the earlier derailment. rerail() resets both now; the unit tests check it after repair_damage(). * fix(physics): the neighbour scan goes on past a derailed vehicle beside the line update_neighbours() took the nearest vehicle the scan of the track found, and when one of the two was off the track and the other not in line with it, gave up on that end. A vehicle lying beside the line so hid the vehicles beyond it: in a pile-up a wagon still on the rails had no front neighbour while running into a wagon in line further on, until the bodies were already deep in each other. The scan now passes such a vehicle and goes on to the next one (ABuFindObject() with InLine, only from update_neighbours(); the obstacle scan of the AI is as it was). * fix(physics): the moves out of other bodies go into the axles of a derailed vehicle The axles of a vehicle off the track follow it along the track by the displacement of the body, which move_off_track() measured from where the body was at the start of its own step. The contacts of the bodies move it out of the others between the frames, after that, so those moves never reached the axles, and the axles drifted along the track away from the body (the track it is registered on, the events it fires). The displacement is now measured from where the body was when its move went into the axles last. * perf(physics): derailed vehicles share the terrain and let it go at rest Each vehicle off the track gathered its own copy of the terrain, up to 600 m around it and 400 000 triangles (some 30-50 MB with the cell index), and kept it until the scenery unloaded: not let go at rest nor when put back on the rails, and gathered again for every vehicle of a pile-up, with a stall of tens of ms each. - The terrain gathered for a vehicle is kept in vehicle_table as a weak reference. A vehicle leaving the track (or gathering again near the edge) takes one which covers the way it may slide - around it and around where it stops going straight on, by its length and the margin the terrain is gathered again at - instead of gathering its own. A terrain cut down to the nearest triangles of a dense scenery is not shared. - At rest a vehicle keeps only the triangles under it (half its length and 40 m around, mesh_ground::cropped()); the big terrain goes once no vehicle holds it. Moving again, it gathers the terrain anew as before. - Back on the rails (rerail()) the vehicle lets its terrain go. The ground under a vehicle is the same as before: a shared terrain holds every triangle of the scenery reaching into its area. Unit tests: mesh_ground::covers() of an area and cropped().
155 lines
6.1 KiB
C++
155 lines
6.1 KiB
C++
/*
|
|
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 "vehicle/blackbox.h"
|
|
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <fstream>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <set>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
class TDynamicObject;
|
|
class TTrack;
|
|
class TMemCell;
|
|
class basic_event;
|
|
enum class user_command;
|
|
|
|
namespace simulation {
|
|
|
|
// the black box of the session (see blackbox_doc.md): written along with the physics logs, in driver mode,
|
|
// from the moment the scenario is loaded until the simulator closes or the editor is entered. the tapes of the user's
|
|
// vehicles, with the physics logs off, go on after the editor until the simulator closes
|
|
class blackbox_recorder {
|
|
public:
|
|
// starts the recording once the scenario is ready, if it is to be made, then records what is due.
|
|
// Deltatime: simulation time passed since the last call
|
|
void update(double Deltatime);
|
|
// writes the end of the recording and closes the file. a recording closed isn't started again; its tapes go on
|
|
void close();
|
|
|
|
// what happens between the looks, from wherever it happens; the cabs may run on threads of their own.
|
|
// nothing is written unless the recording runs
|
|
void command(TDynamicObject const *Vehicle, user_command Command, int Action, double Param1, double Param2);
|
|
void driver_changed(TDynamicObject const *Vehicle, bool Ai);
|
|
// Event: the one telling the aspect of the signal
|
|
void signal_passed(TDynamicObject const *Vehicle, basic_event const &Event, double Speed1, double Speed2, double Trainspeed, double Overrun, std::uint8_t Flags);
|
|
void vehicle_removed(TDynamicObject const *Vehicle);
|
|
// a train at a station of its timetable; the time and the vehicle are filled in here
|
|
void station(TDynamicObject const *Vehicle, blackbox::station_event Station);
|
|
// a train told to sound the horn (W6), at Position
|
|
void horn_requested(TDynamicObject const *Vehicle, double Duration, double Tone, glm::dvec3 const &Position);
|
|
// where the user looks from, every frame; written when it changes, at most twice a second. Owner: the vehicle the
|
|
// camera goes with; the time and its number are filled in here
|
|
void camera(blackbox::camera_state Camera, TDynamicObject const *Owner);
|
|
|
|
private:
|
|
struct coupler_entry {
|
|
TDynamicObject const *other{nullptr};
|
|
int other_side{0};
|
|
int flags{0};
|
|
};
|
|
struct vehicle_entry {
|
|
std::uint32_t id{blackbox::no_vehicle};
|
|
blackbox::vehicle_state last; // as listed last
|
|
bool listed{false};
|
|
std::array<coupler_entry, 2> couplers; // as seen last
|
|
bool couplers_known{false};
|
|
// off the track: 0 not, 1 written as leaving it, 2 written as at rest
|
|
int derailment{0};
|
|
};
|
|
struct switch_entry {
|
|
TTrack *track{nullptr};
|
|
std::uint32_t index{0};
|
|
int state{-1};
|
|
};
|
|
struct signal_entry {
|
|
TMemCell const *cell{nullptr};
|
|
glm::dvec3 location{0.0};
|
|
float speed1{0.0f};
|
|
float speed2{0.0f};
|
|
std::string command;
|
|
bool known{false};
|
|
};
|
|
|
|
void open();
|
|
void write_map();
|
|
static std::vector<std::pair<glm::dvec3, float>> stop_point_headings();
|
|
void write_cab();
|
|
// with the physics logs off, the line of the log of the driver the user is with, to its log, physicslog/<vehicle>.dat,
|
|
// as the physics logs would have it. a driver's log is begun at the first line given for that driver, and goes on
|
|
// while the user looks around outside or is in another vehicle of that driver; back with it, after the gap
|
|
void keep_tape_line();
|
|
// the clock, the user's vehicle, the vehicles, the switches and the signals
|
|
void look_around(bool Keyframe);
|
|
void look_at_vehicles(bool Keyframe);
|
|
void look_at_switches();
|
|
void look_at_signals();
|
|
// the couplers of every vehicle against the last look: those coupled at the first look, then the changes
|
|
void look_at_couplers();
|
|
// the vehicles leaving the track and coming to rest off it (physics.derailment new)
|
|
void look_at_derailments();
|
|
void write_camera();
|
|
// the timetable of the train the user is in, written again when it is another one; none isn't written
|
|
void look_at_timetable(TDynamicObject const &Vehicle, std::uint32_t Id);
|
|
// number of the vehicle, given (and the vehicle written) on first sight
|
|
std::uint32_t vehicle_id(TDynamicObject const *Vehicle);
|
|
// the number of a vehicle still on the scenery, no_vehicle for none or one taken off it
|
|
std::uint32_t known_id(TDynamicObject const *Vehicle);
|
|
// the number of the signal whose aspect the memory cell tells, no_signal for one which isn't on the map
|
|
std::uint32_t signal_of(TMemCell const *Cell) const;
|
|
|
|
std::mutex m_mutex;
|
|
std::array<char, 256 * 1024> m_filebuffer{};
|
|
std::ofstream m_file;
|
|
std::unique_ptr<blackbox::writer> m_writer;
|
|
bool m_started{false}; // tried to start, made or not
|
|
bool m_taping{false}; // the recording was to be made: its tapes are kept from then on, the file made or not, closed or not
|
|
double m_time{0.0};
|
|
double m_cabdue{0.0};
|
|
double m_tapedue{0.0};
|
|
// the names of the logs begun by the recording
|
|
std::set<std::string> m_tapes;
|
|
double m_lookdue{0.0};
|
|
double m_keyframedue{0.0};
|
|
std::unordered_map<TDynamicObject const *, vehicle_entry> m_vehicles;
|
|
std::uint32_t m_nextvehicle{1};
|
|
std::vector<switch_entry> m_switches;
|
|
std::vector<signal_entry> m_signals;
|
|
std::uint32_t m_uservehicle{blackbox::no_vehicle};
|
|
bool m_userincab{false};
|
|
std::int8_t m_usercab{0};
|
|
bool m_userknown{false};
|
|
std::string m_timetabletrain;
|
|
TDynamicObject const *m_timetablevehicle{nullptr}; // the user's vehicle at the last look at the timetable
|
|
int m_timetablestations{0};
|
|
blackbox::camera_state m_camera; // as given last
|
|
TDynamicObject const *m_cameraowner{nullptr};
|
|
blackbox::camera_state m_camerawritten; // as written last
|
|
bool m_cameraknown{false};
|
|
bool m_camerawrote{false};
|
|
double m_cameradue{0.0};
|
|
// how long the looks around take, told in the log when the recording ends
|
|
std::chrono::steady_clock::duration m_looktime{};
|
|
std::chrono::steady_clock::duration m_looklongest{};
|
|
int m_looks{0};
|
|
};
|
|
|
|
extern blackbox_recorder Blackbox;
|
|
|
|
} // namespace simulation
|