diff --git a/bones_doc.md b/bones_doc.md index 04d81f3cf..a3064511f 100644 --- a/bones_doc.md +++ b/bones_doc.md @@ -85,9 +85,11 @@ bonebend: ... [head []] [roote before. A whole cable is bent through the joint by a `rooted` `bonebend` from the socket, as the roof cables above. - A rubber collar gangway round the end door is a submodel of its own, bent the same way from the face of the body to the coupling plane: on a curve the collars of two coaches turn half the angle each and meet face to face. -- The buffers (`buffer_left0`, `buffer_right0`) of two coupled vehicles are pressed in each as its head stands - against the nearest head of the other vehicle on the track, half the overlap each, up to `DmaxB`: on a curve those - on the inside press in, those on the outside stand clear. `gfx.buffers.curve no` in `eu07.ini` presses both buffers +- The buffers (`buffer_left0`, `buffer_right0`) of two coupled vehicles are pressed in each by the deflection of + its end as the coupling has it (half the distance of the couplers, or that of the end in the physical model of the + coupling, by the springs of both ends), and by half of what its head stands nearer to the head it meets than the axis + of the vehicle does, up to `DmaxB`: on a curve those on the inside press in more, those on the outside less or not at + all, and on the straight both as before. `gfx.buffers.curve no` in `eu07.ini` presses both buffers of an end alike, by half the distance of the couplers, as before. ## Examples diff --git a/tests/unit/test_bones.cpp b/tests/unit/test_bones.cpp index 46959ada1..8bd27aae3 100644 --- a/tests/unit/test_bones.cpp +++ b/tests/unit/test_bones.cpp @@ -520,6 +520,24 @@ TEST_CASE("bones: a hose on a running vehicle is drawn with its ends, not left b CHECK(glm::distance(bones::drawn(state, 1), before + travelled) < 1.0e-9); } +TEST_CASE("bones: a joint keeps the lengths of its pieces as made, stretched or pressed by the play of the coupling") { + std::vector const rest{{0.0, 0.0, 2.0}, {0.0, 0.2, 2.0}, {0.0, 0.4, 2.0}}; + // the next car 3 cm farther, turned a little round a curve + std::vector now{{0.0, 0.0, 2.0}, {0.01, 0.215, 2.0}, {0.03, 0.43, 2.0}}; + auto const turned{glm::normalize(now[2] - now[1])}; + bones::keep_lengths(rest, now); + CHECK(now[0] == glm::dvec3{0.0, 0.0, 2.0}); + CHECK(glm::distance(now[1], now[0]) == doctest::Approx(0.2)); + CHECK(glm::distance(now[2], now[1]) == doctest::Approx(0.2)); + // each piece still pointing where it pointed + auto const kept{glm::normalize(now[2] - now[1])}; + CHECK(glm::dot(kept, turned) == doctest::Approx(1.0)); + // pressed together, the same + std::vector pressed{{0.0, 0.0, 2.0}, {0.0, 0.18, 2.0}, {0.0, 0.36, 2.0}}; + bones::keep_lengths(rest, pressed); + CHECK(pressed[2].y == doctest::Approx(0.4)); +} + TEST_CASE("bones: a joint held to the whole turn of the next car, spread evenly along it from either side") { bones::rig rig; REQUIRE(bones::add_point(rig, {"root", "fixed", "0", "0", "2"}).empty()); diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 9e60582b9..163e859af 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -848,8 +848,11 @@ void TDynamicObject::bind_flex( std::vector const &Bends ) { flex_data flex; flex.original = original; flex.bends = Bends; - // a hose is halved into small triangles to bend smoothly; a rooted part, the end of a car body, is taken as made - auto const halved { std::ranges::any_of( Bends, [ this ]( std::size_t const Bend ) { return false == m_rig.bends[ Bend ].rooted; } ) }; + // a hose is halved into small triangles to bend smoothly, one hanging from its cock too; a part of a joint of two cars + // (rooted, its head held to the whole turn of the next car), the end of a car body, is taken as made + auto const halved { std::ranges::any_of( Bends, [ this ]( std::size_t const Bend ) { + auto const &bend { m_rig.bends[ Bend ] }; + return false == bend.rooted || bend.head_up < 0; } ) }; flex.rest = halved ? subdivided( model_file_vertices( original ), max_flex_edge ) : model_file_vertices( original ); if( flex.rest.empty() ) { return; } std::vector points; @@ -981,10 +984,13 @@ void TDynamicObject::pose_flexes( std::vector const &Now ) { } // the root held to the body as made bones::hold_root( bend, turns ); - if( bend.rooted && bend.head_from >= 0 ) { + if( bend.rooted && bend.head_up >= 0 ) { // the joint of two cars: the turn spread evenly from the body to the next car, as the next car spreads it // the other way, so the parts of the two that overlap (the bellows, the roof cables) stay together bones::spread_turns( rest, turns ); + // and as long as made: the bellows and the cables turn with the cars, not stretched by the play of the + // coupling of the articulated joint as the train brakes and pulls + bones::keep_lengths( rest, now ); } nodes.emplace_back( now.begin(), now.end() ); nodeturns.emplace_back( turns.begin(), turns.end() ); @@ -8367,16 +8373,20 @@ std::optional> TDynamicObject::buffers_pressed( int const return Vehicle.GetWorldPosition( frame * glm::dvec3 { Head.x, along, Head.y } ); }; // outwards from our end auto const outwards { glm::normalize( head( *this, End, { 0.0, 0.0 } ) - GetWorldPosition( glm::dvec3 { 0.0 } ) ) }; - auto const limit { MoverParameters->Couplers[ End ].DmaxB }; + auto const &coupler { MoverParameters->Couplers[ End ] }; auto const first { head( *neighbour.vehicle, neighbour.vehicle_end, *theirs[ 0 ] ) }; auto const second { head( *neighbour.vehicle, neighbour.vehicle_end, *theirs[ 1 ] ) }; + // the gap of the two ends on their axis, as the gaps of the buffers are measured + auto const centre { glm::dot( head( *neighbour.vehicle, neighbour.vehicle_end, { 0.0, 0.0 } ) - head( *this, End, { 0.0, 0.0 } ), outwards ) }; std::array pressed {}; for( std::size_t side = 0; side < pressed.size(); ++side ) { auto const here { head( *this, End, *ours[ side ] ) }; auto const &facing { ( glm::distance( here, first ) < glm::distance( here, second ) ? first : second ) }; - // the gap to it along our end, negative where the two overlap: each buffer takes half of it + // the deflection of our end as the coupling has it (half the distance of the couplers, or ours of the physical + // model of the coupling), and half of what this buffer stands nearer to or farther from its pair than the axis + // does: on a curve the inside presses in more, the outside less or not at all auto const gap { glm::dot( facing - here, outwards ) }; - pressed[ side ] = safe_clamp( gap / 2.0, -limit, 0.0 ); + pressed[ side ] = safe_clamp( coupler.Dist / 2.0 + ( gap - centre ) / 2.0, -coupler.DmaxB, 0.0 ); } return pressed; } diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index 05eccf567..6ee9860da 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -922,8 +922,9 @@ private: int DettachStatus(int dir); int Dettach(int dir); TDynamicObject * Neighbour(int &dir); - // how far each buffer of the end (left, right) is pressed in, as its head stands against the nearest one of the next - // vehicle on the track: half the overlap each; none without a coupled vehicle with buffers known on both + // how far each buffer of the end (left, right) is pressed in: the deflection of the end as the coupling has it, and + // half of what the head stands nearer to the one it meets than the axis does; none without a coupled vehicle with + // buffers known on both std::optional> buffers_pressed( int const End ) const; // whether both vehicles at the end have their control cables modelled whole (cctrl_xon), drawn by the owner only bool control_cables_shared( int const End ) const; diff --git a/vehicle/bones.cpp b/vehicle/bones.cpp index f675b06b9..d7a37d52f 100644 --- a/vehicle/bones.cpp +++ b/vehicle/bones.cpp @@ -425,6 +425,24 @@ void spread_turns(std::vector const &Rest, std::vector & } } +void keep_lengths(std::vector const &Rest, std::vector &Now) +{ + if (Rest.size() != Now.size()) + { + return; + } + // the pieces as they stand now, before any node is moved + std::vector const was{Now}; + for (std::size_t node{1}; node < Now.size(); ++node) + { + auto const piece{was[node] - was[node - 1]}; + auto const length{glm::length(piece)}; + auto const made{glm::distance(Rest[node], Rest[node - 1])}; + auto const direction{length > 1e-9 ? piece / length : Rest[node] - Rest[node - 1]}; + Now[node] = Now[node - 1] + (length > 1e-9 ? made : 1.0) * direction; + } +} + skin_pose pose_skin(skin_bind const &Bind, std::vector const &Now, std::vector const &Turns) { if (Bind.piece + 1 >= Now.size() || Bind.piece + 1 >= Turns.size()) diff --git a/vehicle/bones.h b/vehicle/bones.h index e818c0405..d99352f78 100644 --- a/vehicle/bones.h +++ b/vehicle/bones.h @@ -214,6 +214,10 @@ void hold_root(bend const &Bend, std::vector &Turns); // the turns of the nodes between the first and the last spread evenly along the spine as made, from the turn of the first // to that of the last: a joint bent the same seen from either car void spread_turns(std::vector const &Rest, std::vector &Turns); +// the nodes after the first moved along the spine so that each piece keeps its length as made, its direction kept: the +// joint of two cars as stiff as the bellows over it, which turns with the cars but doesn't stretch with the play of +// their coupling +void keep_lengths(std::vector const &Rest, std::vector &Now); // the move of a bone: turned about its anchor to face its aim again, its anchor moved struct motion {