diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index d5847cbc1..9c5f71533 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -1364,6 +1364,12 @@ struct neighbour_data TDynamicObject *vehicle{nullptr}; // detected obstacle int vehicle_end{-1}; // facing end of the obstacle float distance{10000.f}; // distance to the obstacle // NOTE: legacy value. TBD, TODO: use standard -1 instead? + // physics.derailment new, either vehicle off the track: the distance is between the ends where they are, and it + // changes with the moves of the vehicles (along their own axes, as V and dMoveLen go) by these shares of them, + // rather than as along a track. see TMoverParameters::coupler_gap_change() + bool off_track{false}; + double own_share{0.0}; + double other_share{0.0}; }; struct speed_control @@ -2885,6 +2891,10 @@ class TMoverParameters bool coupler_physics_at(int const End) const; // the force of the coupling at the end by the physical model (couplerphysics in eu07.ini), see couplerphysics.h double coupler_force_physical(int const End, double const dt); + // how much the distance to the vehicle at the End grows [m, or m/s] when this vehicle moves (Own) and the other one + // moves (Other) along their axes: along a track the difference of the two moves; with either vehicle off the + // track (Neighbours[End].off_track) by the shares of them the ends have in the direction they are apart in + double coupler_gap_change(int const End, double const Own, double const Other) const; // with the physical model: whether the vehicles at the end hit the stop of their springs at the last update and // still close in on it, so that they collide; a hit hard enough breaks the coupling or crushes the buffers bool coupler_stop_hit(int const End, TMoverParameters &Other, int const OtherEnd); diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 7ee85203a..8245810cb 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -6658,7 +6658,10 @@ void TMoverParameters::ComputeTotalForce(double dt) // the velocities before the step, the forces of all the vehicles come before their movement Couplers[end].closing = -CouplerVelocityDifference(end, *Neighbours[end].vehicle->MoverParameters, Neighbours[end].vehicle_end) * DirF(end); Couplers[end].CForce = CouplerForce(end, dt); - FTrain += Couplers[end].CForce; + // off the track (physics.derailment new) the vehicles are apart in another direction than along the axis of + // this one: of the force between them, along it the share the gap goes by, which for ends gone past each + // other is the other way. the force as a whole stays in CForce, for the motion off the track + FTrain += Couplers[end].CForce * (Neighbours[end].off_track ? coupler_gap_change(end, 1.0, 0.0) * DirF(end) : 1.0); // what the buffers of the pair take in, for the budget of the crush (physics.collisions new and monitor) auto &coupler{Couplers[end]}; coupler.contact.update(coupler.Dist < 0.0, coupler.CheckCollision, coupler.closing, compression_at(coupler, end), std::max(0.0, -coupler.Dist), dt); @@ -7095,6 +7098,19 @@ int coupler_sounds(coupler_sound_input const &Input) } // namespace +double TMoverParameters::coupler_gap_change(int const End, double const Own, double const Other) const +{ + auto const &neighbour{Neighbours[End]}; + if (neighbour.off_track) + { + return neighbour.own_share * Own + neighbour.other_share * Other; + } + // the gap opens up when the front end falls behind the vehicle ahead of it, or the rear end runs ahead of the one + // behind; the other vehicle's move turned around when the vehicles face each other + auto const other{Other * DirPatch(End, neighbour.vehicle_end)}; + return End == end::front ? other - Own : Own - other; +} + double TMoverParameters::coupler_force_physical(int const End, double const dt) { auto &thiscoupler{Couplers[End]}; @@ -7103,11 +7119,12 @@ double TMoverParameters::coupler_force_physical(int const End, double const dt) auto &othercoupler{othervehicle->Couplers[otherend]}; auto &state{thiscoupler.physics_state}; - // the gap as it is now: the distance found at the start of the frame, and the movement of both vehicles since - auto const othervehiclemove{othervehicle->dMoveLen * DirPatch(End, otherend)}; - auto const gap{Neighbours[End].distance + (End == end::front ? othervehiclemove - dMoveLen : dMoveLen - othervehiclemove)}; - // the gap opens up when the front end falls behind the vehicle ahead of it, or the rear end runs ahead of the one behind - auto const gaprate{CouplerVelocityDifference(End, *othervehicle, otherend) * DirF(End)}; + // the gap as it is now: the distance found at the start of the frame, and the movement of both vehicles since. + // off the track the ends of a pair may have gone past each other, where a move along the track that would close + // them on it opens the gap; taken as along the track, the draw gear unloaded while the gap grew far past its travel, + // and the coupling neither held nor broke + auto const gap{Neighbours[End].distance + coupler_gap_change(End, dMoveLen, othervehicle->dMoveLen)}; + auto const gaprate{coupler_gap_change(End, V, othervehicle->V)}; auto const coupled{thiscoupler.CouplingFlag != coupling::faux}; auto const adapter{thiscoupler.has_adapter() || othercoupler.has_adapter()}; @@ -7159,7 +7176,7 @@ bool TMoverParameters::coupler_stop_hit(int const End, TMoverParameters &Other, auto &state{thiscoupler.physics_state}; // the draw gear went solid with the gap opening up, the buffers with it closing - auto const gaprate{CouplerVelocityDifference(End, Other, OtherEnd) * DirF(End)}; + auto const gaprate{coupler_gap_change(End, V, Other.V)}; auto const closing{state.stop > 0 ? gaprate : -gaprate}; if (state.stop == 0 || closing <= 0.0) { diff --git a/tests/unit/test_coupler_physics.cpp b/tests/unit/test_coupler_physics.cpp index 6785a2cb3..9e4799464 100644 --- a/tests/unit/test_coupler_physics.cpp +++ b/tests/unit/test_coupler_physics.cpp @@ -7,6 +7,7 @@ #include #include +#include // the physical model of the couplings (McZapkie/couplerphysics.h, couplerphysics in eu07.ini): progressive buffers and // draw gear with friction hysteresis, pressed in series, the play of the coupling, its yield and its breaking @@ -336,6 +337,63 @@ TEST_CASE("coupler physics: a coupling with play pulls only once the play is tak CHECK(front.speed() < 2.5); } +TEST_CASE("coupler physics: off the track, ends gone past each other pull the coupling taut, it holds or breaks") { + // eu07_td_off_track_buckle: the locomotive lay at rest beside the track, the coach coupled to it ran on past it along + // the rails. the gap between their ends grew with the run of the coach, but the model took the run along the track, + // as closing the gap: the draw gear unloaded, the coupling carried 0 N while the gap grew to 30 m, and it never broke + flag_setting const physics{Global.coupler_physics, true}; + flag_setting const everywhere{Global.coupler_physics_all, true}; + auto running{loose_wagon(0.05, 30.0, "running")}; + auto lying{loose_wagon(0.05, 0.0, "lying")}; + join(running, end::front, lying, end::rear); + test::pair pair{running, end::front, lying, end::rear, 0.0}; + running.hold_brake_pipe(5.0); + lying.hold_brake_pipe(5.0); + // as TDynamicObject::update_neighbours() measures the ends where they are: gone past each other, the run of the + // running wagon forwards opens the gap, the other one running after it closes it. from where the pair stands at + // the first step, the ends at one place + std::optional start; + auto const gap = [&]() { return (running->Loc.X - lying->Loc.X) - start.value_or(running->Loc.X - lying->Loc.X); }; + pair.neighbours_found = [&]() { + if (false == start.has_value()) { + start = running->Loc.X - lying->Loc.X; + } + auto const gap{(running->Loc.X - lying->Loc.X) - *start}; + auto &fromrunning{running->Neighbours[end::front]}; + fromrunning.distance = static_cast(gap); + fromrunning.off_track = true; + fromrunning.own_share = 1.0; + fromrunning.other_share = -1.0; + auto &fromlying{lying->Neighbours[end::rear]}; + fromlying.distance = static_cast(gap); + fromlying.off_track = true; + fromlying.own_share = -1.0; + fromlying.other_share = 1.0; + }; + + SUBCASE("with crash damage, the jerk breaks it") { + flag_setting const crashdamage{Global.crash_damage, true}; + pair.run(1.0); + CHECK_FALSE(pair.coupled()); + CHECK((coupler_damaged(running) || coupler_damaged(lying))); + } + SUBCASE("without it, the coupling holds: the wagon running past is held back, the gap stays within the draw gear") { + flag_setting const crashdamage{Global.crash_damage, false}; + auto pulled{false}; + auto widest{0.0}; + for (int i = 0; i < 300; ++i) { + pair.step(); + pulled = pulled || running->Couplers[end::front].CForce != 0.0; + widest = std::max(widest, gap()); + } + CHECK(pulled); + CHECK(pair.coupled()); + // it ran 25 m on past the other one, the coupling carrying nothing + CHECK(widest < 0.5); + CHECK(running.speed() == doctest::Approx(lying.speed()).epsilon(0.2)); + } +} + TEST_CASE("coupler physics: buffers hit at a walking pace give back less than they took") { flag_setting const physics{Global.coupler_physics, true}; flag_setting const everywhere{Global.coupler_physics_all, true}; diff --git a/tests/unit/vehicle.cpp b/tests/unit/vehicle.cpp index afbac8680..f3ef758bd 100644 --- a/tests/unit/vehicle.cpp +++ b/tests/unit/vehicle.cpp @@ -325,6 +325,9 @@ void pair::find_neighbours() { othermover.Couplers[other.end].adapter_length); mover.Neighbours[1 - self.end] = neighbour_data(); } + if (neighbours_found) { + neighbours_found(); + } } void pair::step(track const &Track, double Dt) { diff --git a/tests/unit/vehicle.h b/tests/unit/vehicle.h index 05c03dbf6..98d1ee5c7 100644 --- a/tests/unit/vehicle.h +++ b/tests/unit/vehicle.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -152,6 +153,10 @@ public: int coupling() const; bool coupled() const { return coupling() != coupling::faux; } + // called after each finding of the neighbours, which it may change: stands in for the ends of vehicles off the track + // meeting where they are (TDynamicObject::update_neighbours() with physics.derailment new), not along the track + std::function neighbours_found; + private: struct placement { vehicle *owner{nullptr}; diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index fb28557a0..1cf9b71bd 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -3094,6 +3094,27 @@ std::string_view side_name( int const Side ) { return Side < 0 ? "right" : "none"; } +// the level unit vector the vehicle moves along with a positive V, its front +glm::dvec3 level_front_of( TDynamicObject const &Vehicle ) { + glm::dvec3 const front { Vehicle.VectorFront().x, 0.0, Vehicle.VectorFront().z }; + auto const length { glm::length( front ) }; + return length > 1e-6 ? front / length : glm::dvec3 { 0.0, 0.0, 1.0 }; +} + +// physics.derailment new, either vehicle of the pair off the track: the distance between their ends where they are, and +// the shares the moves of both vehicles along their own axes have in it (TMoverParameters::coupler_gap_change()): along +// the direction they are apart in, which for ends gone past each other is the other way than along the track +void measure_off_track( neighbour_data &Neighbour, TDynamicObject const &Self, int const End ) { + auto const &other { *Neighbour.vehicle }; + auto const meeting { derail_motion::meet( end_point_of( Self, End ), end_point_of( other, Neighbour.vehicle_end ) ) }; + // pressed into each other the buffers give along the bisector of the bend, apart the coupling between the beams + auto const direction { meeting.gap < 0.0 ? meeting.push : meeting.pull }; + Neighbour.distance = static_cast( meeting.gap ); + Neighbour.off_track = true; + Neighbour.own_share = -glm::dot( direction, level_front_of( Self ) ); + Neighbour.other_share = glm::dot( direction, level_front_of( other ) ); +} + } // namespace double TDynamicObject::off_track_gap( int const End ) const { @@ -3332,7 +3353,7 @@ void TDynamicObject::move_off_track( double const Distance ) { crop_off_track_terrain(); } - pull_neighbours( Distance ); + pull_neighbours( time ); // what it cost: a frame of the vehicle off the track should take well under a millisecond auto const took { seconds_since( started ) }; @@ -3358,7 +3379,7 @@ void TDynamicObject::place_off_track() { m_offtrackplaced = true; } -void TDynamicObject::pull_neighbours( double const Distance ) { +void TDynamicObject::pull_neighbours( double const Time ) { for( int end = end::front; end <= end::rear; ++end ) { auto const &coupler { MoverParameters->Couplers[ end ] }; @@ -3384,7 +3405,10 @@ void TDynamicObject::pull_neighbours( double const Distance ) { auto const magnitude { glm::length( glm::dvec3 { force.x, 0.0, force.z } ) }; auto const mass { other.TotalMass - other.Mred }; auto const ratio { derail_motion::pull_ratio( magnitude, angle, mass, other.NAxles ) }; - if( false == m_pulledoff[ end ].update( ratio >= derailment::climbing_limit, Distance ) ) { + // the flange climbs the rail over the distance the wheels of the vehicle pulled run along it, whatever this one + // does: lying at rest beside the track, it went on pulling the vehicle running past it, which then never got + // the distance the climb takes + if( false == m_pulledoff[ end ].update( ratio >= derailment::climbing_limit, std::abs( other.V ) * Time ) ) { continue; } // the way the force across it goes: towards this one pulled, away from it pushed @@ -8443,10 +8467,11 @@ TDynamicObject::update_neighbours() { // neighbour.vehicle = coupler.Connected; // neighbour.vehicle_end = coupler.ConnectedNr; neighbour.distance = TMoverParameters::CouplerDist( MoverParameters, coupler.Connected ); + neighbour.off_track = false; if( MoverParameters->OffTrack.active() || neighbour.vehicle->MoverParameters->OffTrack.active() ) { // physics.derailment new: off the track the bodies turn against each other, the ends meet where they are // (the distance between the middles less the half lengths would press the buffers of a bent pair) - neighbour.distance = off_track_gap( end ); + measure_off_track( neighbour, *this, end ); } // take into account potential adapters attached to the couplers auto const &othercoupler { neighbour.vehicle->MoverParameters->Couplers[ neighbour.vehicle_end ] }; @@ -8475,7 +8500,7 @@ TDynamicObject::update_neighbours() { neighbour.distance = TMoverParameters::CouplerDist( MoverParameters, neighbour.vehicle->MoverParameters ); if( MoverParameters->OffTrack.active() || neighbour.vehicle->MoverParameters->OffTrack.active() ) { // physics.derailment new: the ends where they are, as for a coupled pair - neighbour.distance = off_track_gap( end ); + measure_off_track( neighbour, *this, end ); } // take into account potential adapters attached to the couplers auto const &othercoupler { neighbour.vehicle->MoverParameters->Couplers[ neighbour.vehicle_end ] }; diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index ca82a0950..7fd1f113f 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -780,9 +780,9 @@ private: derail_motion::formation track_bed() const; glm::dvec3 track_front() const; int outward_side() const; - // the couplings of this vehicle off the track pull the vehicles coupled to it off the rails sideways, the step - // having travelled the distance - void pull_neighbours( double Distance ); + // the couplings of this vehicle off the track pull the vehicles coupled to it off the rails sideways, over the step + // of the time [s] + void pull_neighbours( double Time ); // the scraping of the vehicle off the track, until it stops, and its state to the log every second void update_off_track( double Deltatime );