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

1 Commits

Author SHA1 Message Date
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