18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/vehicle/derailmotion.cpp
jerrrrycho bc2d1323a3 feat(physics): a derailed vehicle leaves the track and moves on the ground (physics.derailment new) (#451)
* 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().
2026-11-26 22:26:37 +01:00

908 lines
34 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/.
*/
#include "stdafx.h"
#include "vehicle/derailmotion.h"
#include "McZapkie/derailment.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <random>
namespace derail_motion {
namespace {
glm::dvec3 const world_up{0.0, 1.0, 0.0};
// the vector level, of unit length; the fallback for one standing up
glm::dvec3 level(glm::dvec3 const &Vector, glm::dvec3 const &Fallback = {0.0, 0.0, 1.0}) {
glm::dvec3 const flat{Vector.x, 0.0, Vector.z};
auto const length{glm::length(flat)};
return length > 1e-9 ? flat / length : Fallback;
}
// to the left of a level heading, as TDynamicObject has it: up x front
glm::dvec3 left_of(glm::dvec3 const &Front) {
return glm::dvec3{Front.z, 0.0, -Front.x};
}
// the vector turned about the axis by the angle, right-handed
glm::dvec3 turned(glm::dvec3 const &Vector, glm::dvec3 const &Axis, double const Angle) {
auto const c{std::cos(Angle)};
auto const s{std::sin(Angle)};
return Vector * c + glm::cross(Axis, Vector) * s + Axis * (glm::dot(Axis, Vector) * (1.0 - c));
}
double sign_of(double const Value) {
if (Value > 0.0) {
return 1.0;
}
return Value < 0.0 ? -1.0 : 0.0;
}
// the height of the triangle over the point, if it is over it (in the plan)
std::optional<double> height_in(mesh_ground::triangle const &Triangle, double const X, double const Z) {
auto const &a{Triangle[0]};
auto const &b{Triangle[1]};
auto const &c{Triangle[2]};
auto const determinant{(b.z - c.z) * (a.x - c.x) + (c.x - b.x) * (a.z - c.z)};
if (std::abs(determinant) < 1e-12) {
// standing on its edge, a wall
return std::nullopt;
}
auto const u{((b.z - c.z) * (X - c.x) + (c.x - b.x) * (Z - c.z)) / determinant};
auto const v{((c.z - a.z) * (X - c.x) + (a.x - c.x) * (Z - c.z)) / determinant};
auto const w{1.0 - u - v};
constexpr double edge{-1e-9};
if (u < edge || v < edge || w < edge) {
return std::nullopt;
}
return u * a.y + v * b.y + w * c.y;
}
} // namespace
namespace {
// the angle [rad] from the first level direction to the second, to the left positive
double angle_between(glm::dvec3 const &From, glm::dvec3 const &To) {
return std::atan2(glm::dot(To, left_of(From)), glm::dot(To, From));
}
// the beams of two ends further apart across than this have gone past each other [m]
constexpr double beam_reach{2.0};
// nor do they meet pressed into each other deeper than this: the buffers and the ends behind them crushed [m]
constexpr double beam_depth{1.5};
// the buffers of two ends this far apart along the line between them are apart, the coupling straight between [m]
constexpr double buffer_reach{0.1};
} // namespace
double sliding_friction(double const Tipping, bool const Guided) {
if (Guided) {
return guided_friction;
}
auto const share{std::clamp(std::abs(Tipping) / body::half_turn, 0.0, 1.0)};
return std::lerp(derailment::off_rails_friction, derailment::overturned_friction, share);
}
double departure_angle(cause const Cause, double const Speed, double const Acceleration) {
if (Cause == cause::end_of_track || Cause == cause::collision) {
return 0.0;
}
auto const sideways{std::sqrt(2.0 * std::abs(Acceleration) * climb_width)};
return std::min(most_departure, std::atan2(sideways, std::max(std::abs(Speed), 1.0)));
}
scatter draw_scatter(std::uint32_t const Seed) {
// the raw numbers of the engine are the same with every library, the distributions of <random> aren't
std::mt19937 engine{Seed}; // NOSONAR: seeded on purpose, so that a derailment can be repeated; nothing secret is drawn
auto const uniform = [&engine](double const Low, double const High) { return Low + (High - Low) * ((static_cast<double>(engine()) + 0.5) / 4294967296.0); };
scatter result;
result.departure = uniform(-0.01, 0.01);
result.yaw = uniform(-0.02, 0.02);
result.yaw_rate = uniform(-0.02, 0.02);
result.friction_front = uniform(0.85, 1.15);
result.friction_rear = uniform(0.85, 1.15);
auto const bites{uniform(0.0, 1.0) < 2.0 / 3.0};
auto const when{uniform(0.2, 1.5)};
auto const howlong{uniform(0.3, 0.8)};
auto const how{uniform(2.0, 4.0)};
auto const side{uniform(0.0, 1.0) < 0.5 ? -1 : 1};
if (bites) {
result.dig_in = when;
result.dig_in_time = howlong;
result.dig_in_friction = how;
result.dig_in_side = side;
}
return result;
}
std::uint32_t seed_of(std::uint32_t const Run, std::string_view const Name) {
// FNV-1a of the name, mixed with the seed of the run
std::uint32_t hash{2166136261u};
for (auto const character : Name) {
hash = (hash ^ static_cast<std::uint8_t>(character)) * 16777619u;
}
hash ^= Run + 0x9e3779b9u + (hash << 6) + (hash >> 2);
return hash;
}
meeting meet(end_point const &First, end_point const &Second) {
meeting result;
auto const outward{level(First.outward)};
auto const otherward{level(Second.outward, -outward)};
result.point = First.point;
result.push = level(outward - otherward, outward);
result.bend = angle_between(outward, -otherward);
glm::dvec3 const apart{Second.point.x - First.point.x, 0.0, Second.point.z - First.point.z};
auto const along{glm::dot(apart, result.push)};
auto const across{glm::dot(apart, left_of(result.push))};
auto const distance{glm::length(apart)};
result.pull = distance > 0.05 ? apart / distance : result.push;
// the ends face each other (bent by less than a right angle) and meet within the depth of the buffers and the ends
// crushed behind them
auto const facing{glm::dot(outward, otherward) < 0.0};
if (along <= 0.0 && facing && along > -beam_depth && std::abs(across) < beam_reach) {
// the buffers press into each other
result.gap = along;
}
else if (along <= 0.0) {
// the ends went past each other, or one body turned about: only the coupling holds them, stretched
result.gap = distance;
}
else {
// apart, the coupling between the middles of the beams; as they come together, the buffers in line
result.gap = std::hypot(along, across) - std::abs(across) * std::max(0.0, 1.0 - along / buffer_reach);
}
// the buffers of the outer side of a body rolled against the other one lift or drop by its roll
auto const apartinheight{std::abs(Second.point.y - First.point.y) + buffer_half_spacing * std::abs(std::sin(Second.roll - First.roll))};
result.overlap = std::clamp(1.0 - apartinheight / buffer_head, 0.0, 1.0);
return result;
}
end_force force_on(meeting const &Meeting, double const Compression) {
end_force result;
result.point = Meeting.point;
if (Compression > 0.0) {
// the inner buffer of the bend takes more of the push, all of it past the play of the buffers; as far as the
// buffers meet in height, and as much of it as a buffer takes, the rest at the middle of the beam
auto const holding{buffer_half_spacing * Meeting.overlap * std::min(1.0, buffer_limit / Compression)};
result.point += left_of(Meeting.push) * (holding * std::clamp(Meeting.bend / buffer_lock, -1.0, 1.0));
result.force = -Meeting.push * Compression;
}
else {
result.force = Meeting.pull * (-Compression);
}
return result;
}
footprint footprint_of(pose const &Pose, double const Length, double const Width, double const Height) {
footprint result;
result.axis = level(Pose.front);
auto const left{left_of(result.axis)};
result.half_length = std::max(0.5 * Length - buffer_length, 0.5) * std::max(std::hypot(Pose.front.x, Pose.front.z), 0.1);
// the corners of its cross section, from the rails to the roof, as it is rolled
auto low{std::numeric_limits<double>::max()};
auto high{std::numeric_limits<double>::lowest()};
auto mostleft{std::numeric_limits<double>::lowest()};
auto mostright{std::numeric_limits<double>::max()};
for (auto const across : {-0.5 * Width, 0.5 * Width}) {
for (auto const up : {0.0, Height}) {
auto const corner{Pose.position + Pose.left * across + Pose.up * up};
auto const aside{glm::dot(corner - Pose.position, left)};
mostleft = std::max(mostleft, aside);
mostright = std::min(mostright, aside);
low = std::min(low, corner.y);
high = std::max(high, corner.y);
}
}
result.half_width = 0.5 * (mostleft - mostright);
result.centre = glm::dvec3{Pose.position.x, 0.0, Pose.position.z} + left * (0.5 * (mostleft + mostright));
// pitched, the ends rise and drop by the slope over half its length
auto const rise{std::abs(Pose.front.y) / std::max(std::hypot(Pose.front.x, Pose.front.z), 0.1) * result.half_length};
result.low = low - rise;
result.high = high + rise;
return result;
}
namespace {
// the corners of the footprint, in turn round it
std::array<glm::dvec3, 4> corners_of(footprint const &Print) {
auto const along{Print.axis * Print.half_length};
auto const across{left_of(Print.axis) * Print.half_width};
return {Print.centre + along + across, Print.centre + along - across, Print.centre - along - across, Print.centre - along + across};
}
// whether the point is inside the footprint (in the plan)
bool inside(footprint const &Print, glm::dvec3 const &Point) {
glm::dvec3 const offset{Point.x - Print.centre.x, 0.0, Point.z - Print.centre.z};
return std::abs(glm::dot(offset, Print.axis)) <= Print.half_length && std::abs(glm::dot(offset, left_of(Print.axis))) <= Print.half_width;
}
double cross_up(glm::dvec3 const &Arm, glm::dvec3 const &Force) {
// about the vertical, to the left positive
return Arm.z * Force.x - Arm.x * Force.z;
}
// the inverse of the mass the body moves with at the point the way of the direction
double inverse_mass(contact_body const &Body, glm::dvec3 const &Point, glm::dvec3 const &Direction) {
if (Body.mass <= 0.0) {
return 0.0;
}
if (Body.guided) {
auto const along{glm::dot(Direction, Body.guide)};
return along * along / Body.mass;
}
glm::dvec3 const arm{Point.x - Body.base.x, 0.0, Point.z - Body.base.z};
auto const turning{cross_up(arm, Direction)};
return (1.0 + turning * turning / std::max(Body.gyration, 1e-3)) / Body.mass;
}
} // namespace
std::optional<contact> collide(footprint const &First, footprint const &Second) {
if (std::min(First.high, Second.high) - std::max(First.low, Second.low) < height_overlap) {
// one goes over the other
return std::nullopt;
}
// the separating axes: the sides of both rectangles; the one they overlap least across is the way out
auto const first{corners_of(First)};
auto const second{corners_of(Second)};
glm::dvec3 const apart{Second.centre.x - First.centre.x, 0.0, Second.centre.z - First.centre.z};
contact result;
result.depth = std::numeric_limits<double>::max();
for (auto const &axis : {First.axis, left_of(First.axis), Second.axis, left_of(Second.axis)}) {
auto spread = [&axis](std::array<glm::dvec3, 4> const &Corners) {
auto low{std::numeric_limits<double>::max()};
auto high{std::numeric_limits<double>::lowest()};
for (auto const &corner : Corners) {
auto const along{glm::dot(corner, axis)};
low = std::min(low, along);
high = std::max(high, along);
}
return std::pair{low, high};
};
auto const [low1, high1]{spread(first)};
auto const [low2, high2]{spread(second)};
auto const overlap{std::min(high1, high2) - std::max(low1, low2)};
if (overlap <= 0.0) {
return std::nullopt;
}
if (overlap < result.depth) {
result.depth = overlap;
result.normal = glm::dot(apart, axis) >= 0.0 ? axis : -axis;
}
}
// the point: the middle of the corners of each inside the other, or of where the overlap is
glm::dvec3 sum{0.0};
auto count{0};
for (auto const &corner : first) {
if (inside(Second, corner)) {
sum += corner;
++count;
}
}
for (auto const &corner : second) {
if (inside(First, corner)) {
sum += corner;
++count;
}
}
if (count > 0) {
result.point = sum / static_cast<double>(count);
}
else {
// crossed without a corner inside the other (two long sides across each other): between the middles
result.point = 0.5 * (First.centre + Second.centre);
}
result.point.y = 0.0;
return result;
}
glm::dvec3 velocity_at(contact_body const &Body, glm::dvec3 const &Point) {
if (Body.guided) {
return Body.velocity;
}
// turning about the vertical at the yaw rate: up x arm
glm::dvec3 const arm{Point.x - Body.base.x, 0.0, Point.z - Body.base.z};
return Body.velocity + Body.yaw_rate * glm::dvec3{arm.z, 0.0, -arm.x};
}
glm::dvec3 contact_impulse(contact const &Contact, contact_body const &First, contact_body const &Second) {
auto const &normal{Contact.normal};
auto const relative{velocity_at(Second, Contact.point) - velocity_at(First, Contact.point)};
auto const closing{glm::dot(relative, normal)};
if (closing >= 0.0) {
// parting already
return glm::dvec3{0.0};
}
auto const inverse{inverse_mass(First, Contact.point, normal) + inverse_mass(Second, Contact.point, normal)};
if (inverse <= 0.0) {
return glm::dvec3{0.0};
}
auto const push{-(1.0 + contact_restitution) * closing / inverse};
glm::dvec3 result{normal * push};
// the friction of the sides sliding along each other, no more than stops the sliding
auto const sliding{relative - normal * closing};
auto const slide{glm::length(sliding)};
if (slide > 1e-6) {
auto const along{sliding / slide};
auto const alonginverse{inverse_mass(First, Contact.point, along) + inverse_mass(Second, Contact.point, along)};
if (alonginverse > 0.0) {
result -= along * std::min(contact_friction * push, slide / alonginverse);
}
}
return result;
}
std::pair<glm::dvec3, double> impulse_change(contact_body const &Body, glm::dvec3 const &Impulse, glm::dvec3 const &Point) {
if (Body.mass <= 0.0) {
return {glm::dvec3{0.0}, 0.0};
}
if (Body.guided) {
return {Body.guide * (glm::dot(Impulse, Body.guide) / Body.mass), 0.0};
}
glm::dvec3 const arm{Point.x - Body.base.x, 0.0, Point.z - Body.base.z};
return {glm::dvec3{Impulse.x, 0.0, Impulse.z} / Body.mass, cross_up(arm, Impulse) / (Body.mass * std::max(Body.gyration, 1e-3))};
}
std::optional<double> stopping_distance(double const Speed, double const Friction, double const Grade) {
auto const angle{std::atan(Grade)};
auto const deceleration{gravity * (Friction * std::cos(angle) + std::sin(angle))};
if (deceleration <= 0.0) {
return std::nullopt;
}
return Speed * Speed / (2.0 * deceleration);
}
double pull_ratio(double const Force, double const Angle, double const Mass, int const Axles) {
auto const axles{std::max(Axles, 2)};
auto const load{Mass * gravity / (2.0 * axles)};
if (load <= 0.0) {
return 0.0;
}
auto const across{std::abs(Force * std::sin(Angle))};
return across / (0.5 * axles) / load;
}
// ---- the ground
std::optional<double> flat_ground::height(double, double, double) const {
return m_level;
}
mesh_ground::mesh_ground(std::vector<triangle> const &Triangles, glm::dvec2 const &Min, glm::dvec2 const &Max, double const Cell) : m_min{Min}, m_max{glm::max(Max, Min + glm::dvec2{1.0})}, m_cell{std::max(Cell, 1.0)} {
auto const size{m_max - m_min};
// a million cells at most
m_cell = std::max(m_cell, std::sqrt(size.x * size.y / 1e6));
m_columns = static_cast<std::size_t>(std::ceil(size.x / m_cell));
m_rows = static_cast<std::size_t>(std::ceil(size.y / m_cell));
m_cells.resize(m_columns * m_rows);
auto const column = [this](double const X) { return static_cast<std::size_t>(std::clamp(std::floor((X - m_min.x) / m_cell), 0.0, static_cast<double>(m_columns - 1))); };
auto const row = [this](double const Z) { return static_cast<std::size_t>(std::clamp(std::floor((Z - m_min.y) / m_cell), 0.0, static_cast<double>(m_rows - 1))); };
for (auto const &triangle : Triangles) {
auto const low{glm::min(glm::min(triangle[0], triangle[1]), triangle[2])};
auto const high{glm::max(glm::max(triangle[0], triangle[1]), triangle[2])};
if (high.x < m_min.x || low.x > m_max.x || high.z < m_min.y || low.z > m_max.y) {
continue;
}
auto const index{m_triangles.size()};
m_triangles.push_back(triangle);
for (auto r = row(low.z); r <= row(high.z); ++r) {
for (auto c = column(low.x); c <= column(high.x); ++c) {
m_cells[r * m_columns + c].push_back(index);
}
}
}
}
bool mesh_ground::covers(double const X, double const Z) const {
return X >= m_min.x && X <= m_max.x && Z >= m_min.y && Z <= m_max.y;
}
bool mesh_ground::covers(glm::dvec2 const &Min, glm::dvec2 const &Max) const {
return Min.x >= m_min.x && Min.y >= m_min.y && Max.x <= m_max.x && Max.y <= m_max.y;
}
mesh_ground mesh_ground::cropped(glm::dvec2 const &Min, glm::dvec2 const &Max) const {
return mesh_ground{m_triangles, Min, Max, m_cell};
}
std::optional<std::size_t> mesh_ground::cell_of(double const X, double const Z) const {
if (false == covers(X, Z) || m_cells.empty()) {
return std::nullopt;
}
auto const column{std::min(static_cast<std::size_t>((X - m_min.x) / m_cell), m_columns - 1)};
auto const row{std::min(static_cast<std::size_t>((Z - m_min.y) / m_cell), m_rows - 1)};
return row * m_columns + column;
}
std::optional<double> mesh_ground::height(double const X, double const Z, double const Ceiling) const {
auto const cell{cell_of(X, Z)};
if (false == cell.has_value()) {
return std::nullopt;
}
std::optional<double> highest;
for (auto const index : m_cells[*cell]) {
auto const found{height_in(m_triangles[index], X, Z)};
if (found.has_value() && *found <= Ceiling && (false == highest.has_value() || *found > *highest)) {
highest = found;
}
}
return highest;
}
double formation::height(double const X, double const Z) const {
auto const across{std::abs(glm::dot(glm::dvec3{X, 0.0, Z} - glm::dvec3{point.x, 0.0, point.z}, left_of(level(direction))))};
auto const top{point.y - depth};
if (across <= half_width) {
return top;
}
return top - (across - half_width) / std::max(slope, 0.1);
}
std::optional<double> site::height(double const X, double const Z, double const Ceiling) const {
auto const bed{m_bed.height(X, Z)};
std::optional<double> result;
if (m_terrain == nullptr) {
// no terrain: level with the top of the ballast
result = m_bed.point.y - m_bed.depth;
}
else {
result = m_terrain->height(X, Z, Ceiling);
}
if (bed <= Ceiling && (false == result.has_value() || bed > *result)) {
result = bed;
}
return result;
}
// ---- the vehicle
void body::begin(start const &Start, vehicle_shape const &Shape, ground const &Ground, scatter const &Scatter) {
*this = body{};
m_active = true;
m_reason = Start.reason;
m_shape = Shape;
m_scatter = Scatter;
// the body a box turning about its middle
m_yawgyration = std::max((Shape.length * Shape.length + Shape.width * Shape.width) / 12.0, 1.0);
m_pivot = 0.5 * std::max(Shape.contact_spacing, 0.5);
auto const centre{std::max(Shape.centre_of_gravity, 0.3)};
m_lever = centre + Shape.flexibility * std::max(0.0, centre - Shape.roll_centre);
m_tipangle = std::atan2(m_pivot, m_lever);
m_reach = std::hypot(m_pivot, m_lever);
// the body a box rolling about its middle, carried over to the wheels it tips about
m_gyration = m_reach * m_reach + (Shape.width * Shape.width + Shape.height * Shape.height) / 12.0;
m_position = Start.position;
m_origin = Start.position;
m_front = level(Start.front);
m_line = level(Start.line, m_front);
m_speed = Start.speed;
// out of the curve, the way it travels
auto const direction{Start.speed < 0.0 ? -1.0 : 1.0};
auto turn{Start.outward * direction * departure_angle(Start.reason, Start.speed, Start.acceleration)};
if (Start.reason != cause::end_of_track && Start.reason != cause::collision) {
turn += Scatter.departure;
}
m_front = level(turned(m_front, world_up, turn), m_front);
m_axis = level(turned(m_front, world_up, Scatter.yaw), m_front);
if (Start.reason == cause::overturned) {
m_guided = true;
m_outward = Start.outward != 0 ? Start.outward : 1;
m_guideacceleration = std::abs(Start.acceleration);
m_guidespeed = std::max(std::abs(Start.speed), 0.1);
}
else {
m_yawrate = Scatter.yaw_rate;
}
auto const foot{find_footing(Ground)};
m_pitch = foot.grade;
m_grade = foot.grade;
m_cross = foot.cross;
place();
}
void body::step(double const Dt, double const Distance, double const Speed, ground const &Ground, std::array<end_force, 2> const &Ends) {
if (false == m_active || Dt <= 0.0) {
return;
}
// where the forces of the couplings act on the body, which turns under them through the step
for (std::size_t end = 0; end < Ends.size(); ++end) {
glm::dvec3 const arm{Ends[end].point.x - m_position.x, 0.0, Ends[end].point.z - m_position.z};
m_ends[end].force = glm::dvec3{Ends[end].force.x, 0.0, Ends[end].force.z};
m_ends[end].along = glm::dot(arm, m_axis);
m_ends[end].across = glm::dot(arm, left_of_heading());
}
auto const steps{std::max(1, static_cast<int>(std::ceil(Dt / substep - 1e-9)))};
auto const dt{Dt / steps};
auto const startspeed{m_speed};
// the heading turned about within the step goes the other way along with the rest of the step
auto direction{1.0};
for (int index = 1; index <= steps; ++index) {
auto const speed{std::lerp(startspeed, direction * Speed, static_cast<double>(index) / steps)};
auto const reversals{m_reversals};
advance(dt, direction * Distance / steps, speed, Ground);
if (m_reversals != reversals) {
direction = -direction;
}
}
m_speed = direction * Speed;
place();
}
void body::advance(double const Dt, double const Distance, double const Speed, ground const &Ground) {
m_position += m_front * Distance + left_of_heading() * (m_lateral * Dt);
m_travelled += std::hypot(Distance, m_lateral * Dt);
m_time += Dt;
auto const foot{find_footing(Ground)};
m_pitch = foot.grade;
// along the way it goes: of the slope along the body and across it
m_grade = foot.grade * glm::dot(m_front, m_axis) + foot.cross * glm::dot(m_front, left_of_heading());
m_cross = foot.cross;
fall(Dt, foot.level, std::hypot(Distance, m_lateral * Dt));
slide(Dt, foot.cross);
tip(Dt, foot.cross, Speed);
turn(Dt, Speed);
if (m_guided) {
m_guidetime += Dt;
if (m_tipping >= m_tipangle || std::abs(Speed) < 0.5 || m_guidetime > guidance_time) {
m_guided = false;
}
}
}
body::footing body::find_footing(ground const &Ground) const {
auto const left{left_of_heading()};
// a step up under the middle, and as much more under a support away from it as a slope of 2 in 1 rises: what is
// higher is over the vehicle, not under it (a bridge, a roof), and the ground under the slope of a bank isn't the
// ground the support stands on
constexpr double steepest{2.0};
auto const sample = [&Ground, this](glm::dvec3 const &Point) {
auto const reach{std::hypot(Point.x - m_position.x, Point.z - m_position.z)};
return Ground.height(Point.x, Point.z, m_position.y + step_up + steepest * reach).value_or(m_position.y);
};
auto const ahead{m_axis * (0.5 * std::max(m_shape.base, 1.0))};
// upright it stands on its wheels; tipping over, it comes to rest on the side of its body, which reaches out
// from the wheels it tips about as far as the body is high
auto const lean{std::sin(m_tipping)};
auto const middle{left * (m_side * lean * 0.5 * m_shape.height)};
auto const span{m_pivot + lean * std::max(0.0, 0.5 * m_shape.height - m_pivot)};
auto const aside{left * span};
auto const frontleft{sample(m_position + middle + ahead + aside)};
auto const frontright{sample(m_position + middle + ahead - aside)};
auto const rearleft{sample(m_position + middle - ahead + aside)};
auto const rearright{sample(m_position + middle - ahead - aside)};
footing result;
result.level = std::max(0.25 * (frontleft + frontright + rearleft + rearright), sample(m_position));
// a step under one support is no slope steeper than 2 in 1
result.grade = std::clamp((frontleft + frontright - rearleft - rearright) / (2.0 * std::max(m_shape.base, 1.0)), -steepest, steepest);
result.cross = std::clamp((frontleft + rearleft - frontright - rearright) / (4.0 * span), -steepest, steepest);
return result;
}
void body::fall(double const Dt, double const Level, double const Travel) {
// on the ground it follows it down a slope as steep as 1:1.5; off a steeper drop it falls
constexpr double follows{1.0 / 1.5};
if (m_grounded && m_position.y - Level <= std::max(0.02, follows * Travel)) {
m_position.y = Level;
return;
}
m_fallspeed -= gravity * Dt;
auto const height{m_position.y + m_fallspeed * Dt};
m_grounded = (height <= Level);
if (m_grounded) {
m_position.y = Level;
m_fallspeed = 0.0;
}
else {
m_position.y = height;
}
}
void body::slide(double const Dt, double const Cross) {
if (false == m_grounded) {
// in the air nothing holds it, nor pushes it
return;
}
auto const angle{std::atan(Cross)};
// down the slope, to the left positive; held by the friction of its weight on the slope
m_lateral -= gravity * std::sin(angle) * Dt;
auto const held{lateral_friction * gravity * std::cos(angle) * Dt};
if (std::abs(m_lateral) <= held) {
m_lateral = 0.0;
}
else {
m_lateral -= sign_of(m_lateral) * held;
}
}
double body::lateral_load(double const Cross, double const Speed) const {
auto const angle{std::atan(Cross)};
auto load{0.0};
if (m_grounded) {
if (m_lateral != 0.0) {
// sliding across, the friction under its wheels or its side trips it the way it slides
load = sign_of(m_lateral) * lateral_friction * gravity * std::cos(angle);
}
else {
// standing on the slope, the weight pulls it down the slope, the friction holds its feet
load = -gravity * std::sin(angle);
}
// going askew, the friction across it trips it too
load += m_trip;
}
if (m_guided) {
auto const share{Speed / m_guidespeed};
load += m_outward * m_guideacceleration * share * share;
}
return load;
}
void body::tip(double const Dt, double const Cross, double const Speed) {
if (lying()) {
return;
}
auto const normal{gravity * std::cos(std::atan(Cross))};
auto const load{lateral_load(Cross, Speed)};
if (m_side == 0) {
// upright: it starts to tip once the load lifts the wheels of the other side
if (std::abs(load) * std::cos(m_tipangle) - normal * std::sin(m_tipangle) <= 0.0) {
return;
}
m_side = load > 0.0 ? 1 : -1;
}
auto const angle{m_tipping - m_tipangle};
auto const acceleration{m_reach / m_gyration * (m_side * load * std::cos(angle) + normal * std::sin(angle))};
m_tiprate += acceleration * Dt;
m_tipping += m_tiprate * Dt;
if (m_tipping <= 0.0) {
// back on its wheels
m_tipping = 0.0;
m_tiprate = 0.0;
m_side = 0;
}
else if (m_tipping >= half_turn) {
// on its side, where it stays
m_tipping = half_turn;
m_tiprate = 0.0;
}
}
glm::dvec3 body::left_of_heading() const {
return left_of(m_axis);
}
double body::friction_along() const {
return sliding_friction(m_tipping, m_guided);
}
double body::friction_across() const {
return m_guided ? guided_friction : std::max(lateral_friction, friction_along());
}
double body::friction_share(bool const Front) const {
return Front ? m_scatter.friction_front : m_scatter.friction_rear;
}
int body::biting() const {
if (m_scatter.dig_in < 0.0 || m_guided || false == m_grounded || m_time < m_scatter.dig_in || m_time >= m_scatter.dig_in + m_scatter.dig_in_time) {
return 0;
}
// the end it goes towards
auto const going{m_speed < 0.0 ? -m_front : m_front};
return glm::dot(going, m_axis) >= 0.0 ? 1 : -1;
}
double body::friction() const {
auto const along{glm::dot(m_front, m_axis)};
auto const across{1.0 - along * along};
auto result{0.5 * (friction_share(true) + friction_share(false)) * (friction_along() * along * along + friction_across() * across)};
if (biting() != 0) {
// under half of its weight
result += 0.5 * (m_scatter.dig_in_friction - 1.0) * friction_along();
}
return result;
}
glm::dvec3 body::velocity() const {
return m_front * m_speed + left_of_heading() * m_lateral;
}
void body::kick(glm::dvec3 const &Velocity, double const YawRate) {
if (false == m_active) {
return;
}
auto const moving{m_front * m_speed + glm::dvec3{Velocity.x, 0.0, Velocity.z}};
auto const speed{glm::length(moving)};
if (speed > 1e-6) {
m_front = moving / speed;
m_speed = speed;
if (glm::dot(m_front, m_axis) < 0.0) {
// it goes rear first
m_front = -m_front;
m_speed = -m_speed;
++m_reversals;
}
}
else {
m_speed = 0.0;
}
m_yawrate = std::clamp(m_yawrate + YawRate, -most_yaw_rate, most_yaw_rate);
}
void body::nudge(glm::dvec3 const &Offset) {
if (false == m_active) {
return;
}
m_position += glm::dvec3{Offset.x, 0.0, Offset.z};
place();
}
double body::yaw() const {
return angle_between(m_line, m_axis);
}
body::drag body::friction_under(double const Speed, double const Yawrate) const {
drag result;
if (false == m_grounded) {
// nothing holds it in the air
return result;
}
auto const left{left_of_heading()};
auto const along{friction_along()};
auto const across{friction_across()};
// upright it stands on its bogies (the ends of its base), on its side it lies along all of its length
auto const share{std::clamp(m_tipping / half_turn, 0.0, 1.0)};
auto const base{std::max(m_shape.base, 1.0)};
auto const span{std::max(m_shape.length, base)};
auto const velocity{m_front * Speed};
auto const point = [&](double const Along, double const Across, double const Weight) {
// the friction under a point of the body: against the way it slides there, along the body and across it as each
// holds; slower than the creep, as much as it slides
auto const motion{velocity + left * (Yawrate * Along) - m_axis * (Yawrate * Across)};
auto const speed{std::max(glm::length(motion), creep_speed)};
auto const force{-(gravity * Weight) * (along * glm::dot(motion, m_axis) * m_axis + across * glm::dot(motion, left) * left) / speed};
result.force += force;
// about the vertical, to the left positive
result.moment += Along * glm::dot(force, left) - Across * glm::dot(force, m_axis);
};
if (share < 1.0) {
for (auto const end : {1.0, -1.0}) {
point(end * 0.5 * base, 0.0, (1.0 - share) * 0.5 * friction_share(end > 0.0));
}
}
if (share > 0.0) {
for (int index = 0; index < friction_points; ++index) {
auto const along_body{span * ((index + 0.5) / friction_points - 0.5)};
point(along_body, 0.0, share * friction_share(along_body > 0.0) / friction_points);
}
}
if (auto const end{biting()}; end != 0) {
// the leading end bites into the ballast at one of its wheels, under half of its weight; on its side, at a corner
// of its body
point(end * 0.5 * std::lerp(base, 0.5 * span, share), m_scatter.dig_in_side * std::lerp(m_pivot, 0.5 * m_shape.height, share), 0.5 * (m_scatter.dig_in_friction - 1.0));
}
// the way it slides across, the friction trips it
result.trip = -glm::dot(result.force, left);
return result;
}
void body::turn(double const Dt, double const Speed) {
// the forces of the couplings, per the mass
auto const left{left_of_heading()};
auto moment{0.0};
glm::dvec3 push{0.0};
if (m_shape.mass > 0.0) {
for (auto const &end : m_ends) {
auto const arm{m_axis * end.along + left * end.across};
moment += (arm.z * end.force.x - arm.x * end.force.z) / m_shape.mass;
push += end.force / m_shape.mass;
}
}
if (m_guided) {
// on its outer wheels on the rail it goes along the rail
m_yawrate = 0.0;
m_trip = 0.0;
return;
}
// the body: the couplings turn it, and the friction under it where it differs between the ends of a body going
// askew; the friction holds it against turning, never turning it back the other way
auto const driving{friction_under(Speed, 0.0).moment};
auto rate{m_yawrate + (moment + driving) / m_yawgyration * Dt};
auto const change{(friction_under(Speed, rate).moment - driving) / m_yawgyration * Dt};
rate = ((rate + change) * rate <= 0.0 ? 0.0 : rate + change);
if (std::abs(Speed) < creep_speed && m_lateral == 0.0 && std::abs(rate) < 0.01 && m_grounded) {
// at a standstill it doesn't turn as long as the friction under it holds against the couplings
// on its bogies at half the base from the middle, lying along the length a quarter of it on average
auto const share{std::clamp(m_tipping / half_turn, 0.0, 1.0)};
auto const arm{std::lerp(0.5 * std::max(m_shape.base, 1.0), 0.25 * std::max(m_shape.length, m_shape.base), share)};
if (std::abs(moment) <= gravity * friction_across() * arm) {
rate = 0.0;
}
}
m_yawrate = std::clamp(rate, -most_yaw_rate, most_yaw_rate);
m_axis = level(turned(m_axis, world_up, m_yawrate * Dt), m_axis);
// the way it travels: the friction of the body going askew turns it towards the body (no further), the couplings
// pushing across it turn it their way; the faster it goes, the less
auto const drag{friction_under(Speed, m_yawrate)};
m_trip = drag.trip;
if (m_grounded && std::abs(Speed) >= turning_speed) {
auto const across{left_of(m_front)};
auto const off{angle_between(m_front, glm::dot(m_front, m_axis) >= 0.0 ? m_axis : -m_axis)};
auto byfriction{glm::dot(drag.force, across) * Dt / Speed};
byfriction = (byfriction * off <= 0.0 ? 0.0 : sign_of(off) * std::min(std::abs(byfriction), std::abs(off)));
auto const bypush{glm::dot(push, across) * Dt / Speed};
auto const turning{std::clamp(byfriction + bypush, -most_yaw_rate * Dt, most_yaw_rate * Dt)};
m_front = level(turned(m_front, world_up, turning), m_front);
}
if (glm::dot(m_front, m_axis) < 0.0) {
// the body turned past a right angle off the way it goes: it goes rear first, the heading the other way
m_front = -m_front;
m_speed = -m_speed;
++m_reversals;
}
}
double body::roll() const {
// the ground rising to the left leans it to the right
return tipping() - std::atan(m_cross);
}
double body::along() const {
return glm::dot(m_position - m_origin, m_line);
}
double body::across() const {
return glm::dot(m_position - m_origin, left_of(m_line));
}
bool body::at_rest() const {
return m_active && m_grounded && std::abs(m_speed) < 0.01 && m_lateral == 0.0 && m_tiprate == 0.0 && m_yawrate == 0.0 && false == m_guided;
}
void body::place() {
// the plane of the ground under the supports
auto const front{glm::normalize(m_axis + world_up * m_pitch)};
auto const flatleft{glm::normalize(left_of_heading() + world_up * m_cross)};
auto const up{glm::normalize(glm::cross(front, flatleft))};
auto const left{glm::cross(up, front)};
// tipped about the wheels of its side: turned about the front, the up going over to that side
auto const angle{-m_side * m_tipping};
auto const pivot{m_position + left * (m_side * m_pivot)};
auto origin{pivot + turned(m_position - pivot, front, angle)};
auto const tippedleft{turned(left, front, angle)};
auto const tippedup{turned(up, front, angle)};
// the body doesn't go into the ground: the lowest of the edges of its box on it, from the rails to its roof
auto lowest{std::numeric_limits<double>::max()};
for (auto const across : {-0.5 * m_shape.width, 0.5 * m_shape.width}) {
for (auto const height : {0.0, m_shape.height}) {
lowest = std::min(lowest, glm::dot(origin + tippedleft * across + tippedup * height - m_position, up));
}
}
if (lowest < 0.0) {
origin -= up * lowest;
}
m_pose.position = origin;
m_pose.front = front;
m_pose.left = tippedleft;
m_pose.up = tippedup;
}
} // namespace derail_motion