From 13e9191427fbe7e951bb876609dfd4848c103123 Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Wed, 2 Dec 2026 20:46:55 +0100 Subject: [PATCH] fix(doors): folding doors move as the coach ones do, with an optional hold at half way on closing Folding doors (DoorOpenMethod=Fold) turned at a constant speed. Their speed now changes along the way, in the same time as before (range / OpenSpeed, range / CloseSpeed): - opening is fastest at the closed door, slows down around half way and creeps to a stop at the fully open one; - closing swings to half way and then shuts, each part speeding up from its start and slowing down to its end. The new optional key DoorFoldHold= in the Doors: line of the .fiz holds a closing door at half way for that many seconds, with the wings rocking a little towards the closed door, a bit differently each time. Without it there is no hold, and the random numbers are drawn only for a hold, so vehicles without the key keep theirs. The position of a folding door now follows its wings, so such a door counts as open or closed only when its wings are; with a hold, the AI departs after it. Other kinds of doors move as before. The motion is in vehicle/doorwings.h (fold_times_of, fold_begin, fold_advance) with unit tests; the rocking is applied to the animation only. Game data (SVN pctga), changed in the working copy, to be committed there with this change: the Doors: line of the 45 coach .fiz files with DoorOpenMethod=Fold in dynamic/pkp (11xa_v2, 14xa_v1, 110a_v2, 111arow_v1, 11xa-techniczne_v1, bdhinu_v1) goes from OpenSpeed=70 CloseSpeed=400 to OpenSpeed=32 CloseSpeed=53 and gets DoorFoldHold=3.3 (2.5 s opening, 1.5 s closing and a 3.3 s hold, as in a recording of an IC coach). The road vehicles with folding doors are left as they are, without a hold. Either part works without the other: this code on the old data opens and closes in the old times without a hold; the new data on an older build only makes the doors slower, the older build ignoring DoorFoldHold. --- McZapkie/MOVER.h | 4 + McZapkie/Mover.cpp | 30 +++++- tests/unit/test_door_wings.cpp | 149 +++++++++++++++++++++++++++ vehicle/DynObj.cpp | 14 ++- vehicle/doorwings.h | 177 +++++++++++++++++++++++++++++++++ 5 files changed, 370 insertions(+), 4 deletions(-) diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index d5847cbc1..81dfe05ac 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -20,6 +20,7 @@ http://mozilla.org/MPL/2.0/. #include "collision.h" #include "scripting/ladderlogic.h" #include "vehicle/derailmotion.h" +#include "vehicle/doorwings.h" #include "vehicle/odometerreading.h" class cParser; @@ -1544,6 +1545,7 @@ class TMoverParameters float close_delay{0.f}; // delay between activation of closing state and actual closing float open_delay{0.f}; // delay between activation of opening state and actual opening float position{0.f}; // current shift of the door from the closed position + door_wings::fold_state fold; // the motion of a folding door (type 3), which holds on the way; its rocking is for the animation only float step_position{0.f}; // current shift of the movable step from the retracted position // ld outputs bool is_closed{true}; // the door is fully closed @@ -1567,6 +1569,7 @@ class TMoverParameters int type{2}; float range{0.f}; // extent of primary move/rotation float range_out{0.f}; // extent of shift outward, applicable for plug doors + float fold_hold{0.f}; // folding doors: the hold at half way on closing [s] (DoorFoldHold) int step_type{2}; float step_rate{0.5f}; float step_range{0.f}; @@ -3155,6 +3158,7 @@ class TMoverParameters // not const: the door they change is one of Doors.instances void update_door_requests(basic_door &door, bool localopencontrol, bool remoteopencontrol, bool localclosecontrol, bool remoteclosecontrol); void move_door(basic_door &door, double Deltatime); + float fold_door_move(basic_door &door, bool Opening, double Deltatime) const; bool door_open_request(basic_door const &door, bool localopencontrol, bool remoteopencontrol) const; bool door_close_request(basic_door const &door, bool localclosecontrol, bool remoteclosecontrol) const; bool doors_powered() const; diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 7ee85203a..0c980ae63 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -11,6 +11,7 @@ http://mozilla.org/MPL/2.0/. #include "MOVER.h" #include "vehicle/DynObj.h" +#include "vehicle/doorwings.h" #include "Oerlikon_ESt.h" #include "utilities/Globals.h" #include "utilities/Logs.h" @@ -11219,6 +11220,27 @@ float TMoverParameters::door_open_duration(basic_door const &door, bool const lo return -1.f; } +// the position of a folding door after Deltatime of its move, in the times of the .fiz. closing, it swings to 50% and, +// with DoorFoldHold, holds there with the wings rocking a little towards the closed door, a bit differently each time, +// then shuts; opening, it slows down there +float TMoverParameters::fold_door_move(basic_door &door, bool const Opening, double const Deltatime) const +{ + if (false == door.fold.begun || door.fold.opening != Opening) + { + auto const times{door_wings::fold_times_of(Doors.range, Doors.open_rate, Doors.close_rate, Doors.fold_hold)}; + door_wings::fold_rocking rocking; + if (false == Opening && times.hold > 0.f) + { + // drawn only for a hold, so the random numbers of the vehicles without one go as they did + rocking.amplitude = static_cast(Random(1.0, 2.5)); + rocking.frequency = static_cast(Random(0.25, 0.5)); + rocking.phase = static_cast(Random(0.0, 6.283)); + } + door_wings::fold_begin(door.fold, door.position, Doors.range, Opening, times, rocking); + } + return door_wings::fold_advance(door.fold, door.position, Doors.range, static_cast(Deltatime)); +} + // a door and its step moved as they open or close void TMoverParameters::move_door(basic_door &door, double const Deltatime) { @@ -11232,7 +11254,8 @@ void TMoverParameters::move_door(basic_door &door, double const Deltatime) door.open_delay += static_cast(Deltatime); if (door.open_delay > Doors.open_delay) { - door.position = std::min(Doors.range, static_cast(door.position + Doors.open_rate * Deltatime)); + door.position = Doors.type == door_wings::fold_type ? fold_door_move(door, true, Deltatime) : + std::min(Doors.range, static_cast(door.position + Doors.open_rate * Deltatime)); } } door.close_delay = 0.f; @@ -11243,7 +11266,8 @@ void TMoverParameters::move_door(basic_door &door, double const Deltatime) door.close_delay += static_cast(Deltatime); if (door.close_delay > Doors.close_delay) { - door.position = std::max(0.f, static_cast(door.position - Doors.close_rate * Deltatime)); + door.position = Doors.type == door_wings::fold_type ? fold_door_move(door, false, Deltatime) : + std::max(0.f, static_cast(door.position - Doors.close_rate * Deltatime)); } door.open_delay = 0.f; } @@ -13202,6 +13226,8 @@ void TMoverParameters::LoadFIZ_Doors(std::string const &line) extract_value(Doors.range, "DoorMaxShiftL", line, ""); extract_value(Doors.range, "DoorMaxShiftR", line, ""); extract_value(Doors.range_out, "DoorMaxShiftPlug", line, ""); + // folding doors: the hold at half way on closing [s], none without it + extract_value(Doors.fold_hold, "DoorFoldHold", line, ""); std::map> doortypes{ {"Shift", 1}, diff --git a/tests/unit/test_door_wings.cpp b/tests/unit/test_door_wings.cpp index 9eff62441..86d0e8993 100644 --- a/tests/unit/test_door_wings.cpp +++ b/tests/unit/test_door_wings.cpp @@ -4,6 +4,10 @@ #include "vehicle/doorwings.h" #include "utilities/parser.h" +#include +#include +#include + // the poses of the animated door wings (eu07/maszyna#512): a wing of the main model and its namesake in the low // poly interior get the same pose, worked out once from the door position of the mover @@ -205,3 +209,148 @@ TEST_CASE("door wings: a wing with its own range is as far along it as the door CHECK(open.x == doctest::Approx(0.06f)); CHECK(open.z == doctest::Approx(0.87f)); } + +namespace { + +// a folding door of 80 degrees, its hold at 40 degrees +constexpr float fold_range{80.f}; +constexpr float fold_hold{40.f}; +// the coaches: OpenSpeed=32 CloseSpeed=53.3 DoorFoldHold=3.3 over 80 degrees +door_wings::fold_times const fold_coach{door_wings::fold_times_of(fold_range, 32.f, 80.f / 1.5f, 3.3f)}; + +// the time a folding door takes from Position to the end of its move, in steps of Deltatime +float fold_move_time(float const Position, bool const Opening, float const Deltatime) { + door_wings::fold_state state; + door_wings::fold_begin(state, Position, fold_range, Opening, fold_coach, {}); + auto position{Position}; + auto const end{Opening ? fold_range : 0.f}; + auto time{0.f}; + while (std::abs(position - end) > 0.f && time < 100.f) { + position = door_wings::fold_advance(state, position, fold_range, Deltatime); + time += Deltatime; + } + return time; +} + +// the speed of the folding door at the position, in degrees a second +float fold_speed(float const Position, bool const Opening) { + return door_wings::fold_speed(Position, fold_range, Opening, fold_coach); +} + +// a folding door moved in steps of 1/60 s, which records where it was at each step and how much its wings rocked +struct fold_run { + std::vector positions; + std::vector rocks; + fold_run(float const Position, bool const Opening, door_wings::fold_rocking const &Rocking, float const Seconds) { + door_wings::fold_state state; + door_wings::fold_begin(state, Position, fold_range, Opening, fold_coach, Rocking); + auto position{Position}; + for (auto step = 0; step < static_cast(Seconds * 60.f); ++step) { + position = door_wings::fold_advance(state, position, fold_range, 1.f / 60.f); + positions.emplace_back(position); + rocks.emplace_back(state.rock); + } + } + // the number of steps the door stood at the hold + int held() const { + return static_cast(std::ranges::count_if(positions, [](float const Position) { return std::abs(Position - fold_hold) < 1e-4f; })); + } +}; + +} // namespace + +TEST_CASE("door wings: a folding door opens in 2.5 s and closes in 1.5 s and its hold, whatever the step") { + for (auto const step : {1.f / 60.f, 1.f / 15.f}) { + CHECK(fold_move_time(0.f, true, step) == doctest::Approx(2.5f).epsilon(0.03)); + CHECK(fold_move_time(fold_range, false, step) == doctest::Approx(4.8f).epsilon(0.02)); + } + CHECK(fold_move_time(0.f, true, 0.5f) == doctest::Approx(2.5f).epsilon(0.2)); + CHECK(fold_move_time(fold_range, false, 0.5f) == doctest::Approx(4.8f).epsilon(0.1)); +} + +TEST_CASE("door wings: a closing folding door holds at 50% with its wings rocking, an opening one slows down there") { + door_wings::fold_rocking const rocking{2.f, 0.4f, 0.5f}; + // closing: 3.3 s at 40 degrees, the wings rocking by up to the amplitude and still again after it + fold_run const closing{fold_range, false, rocking, 7.f}; + CHECK(closing.held() == doctest::Approx(198).epsilon(0.02)); + // towards the closed door only, so it never looks like opening again + CHECK(std::ranges::all_of(closing.rocks, [](float const Rock) { return Rock <= 0.f; })); + CHECK(std::ranges::min(closing.rocks) < -1.f); + CHECK(std::ranges::min(closing.rocks) >= -2.f); + CHECK(closing.rocks.back() == 0.f); + CHECK(closing.positions.back() == 0.f); + // opening: no hold, the wings still, but at 50% at a quarter of the speed a little before and after it + fold_run const opening{0.f, true, rocking, 4.f}; + CHECK(opening.held() < 3); + CHECK(std::ranges::all_of(opening.rocks, [](float const Rock) { return Rock == 0.f; })); + CHECK(opening.positions.back() == fold_range); + CHECK(fold_speed(40.f, true) < 0.4f * fold_speed(28.f, true)); + CHECK(fold_speed(40.f, true) < 0.4f * fold_speed(52.f, true)); +} + +TEST_CASE("door wings: a closing folding door holds only when it passes the place of the hold") { + // from 30 degrees: below the hold, so no hold + fold_run const below{30.f, false, {}, 2.f}; + CHECK(below.held() == 0); + CHECK(below.positions.back() == 0.f); + // from 60 degrees: it holds at 40 on the way + fold_run const above{60.f, false, {}, 5.f}; + CHECK(above.held() > 190); + CHECK(above.positions.back() == 0.f); +} + +TEST_CASE("door wings: an opening folding door is fastest at the closed door and slows down towards the open one") { + CHECK(fold_speed(1.f, true) > fold_speed(20.f, true)); + CHECK(fold_speed(60.f, true) > fold_speed(72.f, true)); + CHECK(fold_speed(79.f, true) < 0.2f * fold_speed(1.f, true)); +} + +TEST_CASE("door wings: a closing folding door swings and shuts at once, each speeding up and slowing down to its end") { + // it moves from the start: no creeping at the open door + CHECK(fold_speed(fold_range, false) > 0.2f * fold_speed(60.f, false)); + // the swing fastest in its middle, slower at its start and before the hold + CHECK(fold_speed(60.f, false) > fold_speed(78.f, false)); + CHECK(fold_speed(60.f, false) > fold_speed(42.f, false)); + // the shutting fastest in its middle too + CHECK(fold_speed(20.f, false) > fold_speed(38.f, false)); + CHECK(fold_speed(20.f, false) > fold_speed(2.f, false)); + // the swing over in 0.8 s, then the hold and the shutting + fold_run const closing{fold_range, false, {}, 1.f}; + CHECK(std::abs(closing.positions[49] - fold_hold) < 1e-4f); + CHECK(closing.positions[43] > fold_hold); +} + +TEST_CASE("door wings: a folding door stops at the closed and at the open position, and no further") { + door_wings::fold_state state; + door_wings::fold_begin(state, 40.f, fold_range, true, fold_coach, {}); + CHECK(door_wings::fold_advance(state, 40.f, fold_range, 10.f) == fold_range); + door_wings::fold_begin(state, 20.f, fold_range, false, fold_coach, {}); + CHECK(door_wings::fold_advance(state, 20.f, fold_range, 10.f) == 0.f); + // no door range: it stays where it is + door_wings::fold_begin(state, 1.f, 0.f, false, fold_coach, {}); + CHECK(door_wings::fold_advance(state, 1.f, 0.f, 0.25f) == 1.f); +} + +TEST_CASE("door wings: a folding door takes the times of the .fiz, and holds only with DoorFoldHold") { + // the times of the speeds of the .fiz + auto const coach{door_wings::fold_times_of(80.f, 32.f, 80.f / 1.5f, 3.3f)}; + CHECK(coach.open == doctest::Approx(2.5f)); + CHECK(coach.close == doctest::Approx(1.5f)); + CHECK(coach.hold == doctest::Approx(3.3f)); + // a bus without DoorFoldHold (88 degrees, OpenSpeed=30 CloseSpeed=38): its own times, and no hold + auto const bus{door_wings::fold_times_of(88.f, 30.f, 38.f, 0.f)}; + door_wings::fold_state state; + door_wings::fold_begin(state, 88.f, 88.f, false, bus, {2.f, 0.4f, 0.f}); + CHECK(state.hold_done); + auto position{88.f}; + auto time{0.f}; + while (position > 0.f && time < 10.f) { + position = door_wings::fold_advance(state, position, 88.f, 1.f / 60.f); + CHECK(state.rock == 0.f); + time += 1.f / 60.f; + } + CHECK(time == doctest::Approx(88.f / 38.f).epsilon(0.02)); + // no speed in the .fiz: the door stays where it is, as it did at the constant speed of zero + door_wings::fold_begin(state, 0.f, 80.f, true, door_wings::fold_times_of(80.f, 0.f, 0.f, 0.f), {}); + CHECK(door_wings::fold_advance(state, 0.f, 80.f, 1.f) == 0.f); +} diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index 7eb6f19a9..9b5700b91 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -642,6 +642,16 @@ float door_position( TMoverParameters const &Mover, TAnim const *Animation ) { side::left ) ].position; } +// the same for a folding wing, with the rocking of the wings while they hold on closing +float fold_door_position( TMoverParameters const &Mover, TAnim const *Animation ) { + + auto const &door { Mover.Doors.instances[ ( + door_wings::on_right_side( Animation->iNumber ) ? + side::right : + side::left ) ] }; + return door.position + door.fold.rock; +} + // the wing in the main model, and its namesake in the low poly interior; either can be missing std::array wing_parts( TAnim const *Animation ) { @@ -697,7 +707,7 @@ void TDynamicObject::UpdateDoorFold(TAnim *pAnim) { if( pAnim->smAnimated == nullptr ) { return; } - auto const angles { door_wings::fold( door_position( *MoverParameters, pAnim ) ) }; + auto const angles { door_wings::fold( fold_door_position( *MoverParameters, pAnim ) ) }; for( auto *wing : wing_parts( pAnim ) ) { fold_wing( wing, angles ); } @@ -734,7 +744,7 @@ void TDynamicObject::UpdateDoorWing( TAnim *pAnim, door_wings::wing_spec const & break; } case door_wings::fold_type: { - fold_wing( wing, door_wings::fold( position ) ); + fold_wing( wing, door_wings::fold( door_wings::wing_position( fold_door_position( *MoverParameters, pAnim ), MoverParameters->Doors.range, Spec.range ) ) ); break; } case door_wings::plug_type: { diff --git a/vehicle/doorwings.h b/vehicle/doorwings.h index db05b03fc..53b7a72e8 100644 --- a/vehicle/doorwings.h +++ b/vehicle/doorwings.h @@ -13,6 +13,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include #include #include @@ -62,6 +63,182 @@ inline fold_angles fold(float const Position) { return {Position, -Position - Position, Position}; } +// a folding door moves as the ones of the coaches do. its moves take as long as at the constant speeds of the .fiz: +// opening Range / OpenSpeed, closing Range / CloseSpeed, but their speed changes along the way. +// opening, the speed depends on how far open the door is: it is fastest at the closed door and slows down towards the +// open one, harder and harder just before it is fully open, and slows down around fold_hold_at on the way. as a part +// of the full speed, with Open the part of the range the door is open: +// (fold_slowest + (1 - fold_slowest) * (1 - Open)^fold_slowdown) * the slowdown +// closing, it swings to fold_hold_at in fold_swing_part of the closing time and shuts in the rest. with DoorFoldHold +// in the .fiz it holds at fold_hold_at for that many seconds before it shuts, the wings rocking a little towards the +// closed door, a bit differently each time; the hold comes on top of the closing time. the swing and the shutting each +// speed up from a start and slow down to their end, as fold_close_slowest + (1 - fold_close_slowest) * sin(pi * u) +// of their full speed, u being how far along they are +inline constexpr float fold_slowest{0.08f}; // opening: the speed at the fully open door, as a part of the full speed +inline constexpr float fold_slowdown{0.6f}; // below 1: fast most of the way, then slowing down steeply near the open door +inline constexpr float fold_hold_at{0.5f}; // where the wings hold on closing and slow down on opening, as a part of the range +inline constexpr float fold_slowdown_depth{0.75f}; // how much of the speed the slowdown on opening takes away at its middle +inline constexpr float fold_slowdown_width{0.08f}; // the slowdown on opening, as a part of the range either way +inline constexpr float fold_close_slowest{0.25f}; // closing: the speed at the start and the end of the swing and the shutting +inline constexpr float fold_swing_part{0.8f / 1.5f}; // closing: the part of the closing time the swing to the hold takes + +// how long the moves of a folding door take [s]: opening, closing (without the hold), and the hold on closing +struct fold_times { + float open{0.f}; + float close{0.f}; + float hold{0.f}; +}; + +// the times of a folding door over Range at the speeds of the .fiz, with Hold seconds of hold on closing; a speed of +// zero leaves the door where it is, as it did at the constant speed +inline fold_times fold_times_of(float const Range, float const Openspeed, float const Closespeed, float const Hold) { + fold_times times; + times.open = Openspeed > 0.f ? Range / Openspeed : 0.f; + times.close = Closespeed > 0.f ? Range / Closespeed : 0.f; + times.hold = std::max(0.f, Hold); + return times; +} + +// opening: the speed at the position, as a part of the full speed +inline float fold_open_speed_part(float const Open) { + auto const open{std::clamp(Open, 0.f, 1.f)}; + auto const slowdown{(open - fold_hold_at) / fold_slowdown_width}; + return (fold_slowest + (1.f - fold_slowest) * std::pow(1.f - open, fold_slowdown)) * (1.f - fold_slowdown_depth * std::exp(-slowdown * slowdown)); +} + +// closing: the speed of the swing or the shutting, Along the way of it, as a part of its full speed +inline float fold_close_speed_part(float const Along) { + return fold_close_slowest + (1.f - fold_close_slowest) * std::sin(std::numbers::pi_v * std::clamp(Along, 0.f, 1.f)); +} + +// the integral of 1 / Part over 0..1: the full speed, in times the average one, at which a move with the speeds of +// Part takes as long as at the constant average speed +template +float fold_peak_of(Part_ const &Part) { + constexpr int steps{4096}; + auto sum{0.0}; + for (auto step = 0; step < steps; ++step) { + sum += 1.0 / Part((static_cast(step) + 0.5f) / steps); + } + return static_cast(sum / steps); +} + +inline float fold_open_peak() { + static float const peak{fold_peak_of(fold_open_speed_part)}; + return peak; +} + +inline float fold_close_peak() { + static float const peak{fold_peak_of(fold_close_speed_part)}; + return peak; +} + +// the speed of a folding door at Position over Range, opening or closing in the Times, in units of the range a second +inline float fold_speed(float const Position, float const Range, bool const Opening, fold_times const &Times) { + if (Opening) { + return fold_open_peak() * Range / Times.open * fold_open_speed_part(Position / Range); + } + auto const hold{Range * fold_hold_at}; + if (Position > hold) { + // the swing, from fully open to the hold + auto const length{Range - hold}; + return fold_close_peak() * length / (Times.close * fold_swing_part) * fold_close_speed_part((Range - Position) / length); + } + // the shutting, from the hold to closed + return fold_close_peak() * hold / (Times.close * (1.f - fold_swing_part)) * fold_close_speed_part((hold - Position) / hold); +} + +// how the wings rock while they hold on closing: up to Amplitude degrees towards the closed door, Frequency times a second +struct fold_rocking { + float amplitude{0.f}; + float frequency{0.f}; + float phase{0.f}; +}; + +// the motion of a folding door between the frames +struct fold_state { + bool begun{false}; // a move was begun + bool opening{false}; // the way of the last move + bool hold_done{true}; // the move had its hold, or has none: opening, no hold time, or closing from below the place of it + float hold_left{0.f}; // the time left of the hold under way + float rock{0.f}; // the angle the wings rock by while they hold, for the animation only + fold_times times; // of this move + fold_rocking rocking; // of the hold of this move +}; + +// starts a move of the door from Position over Range, opening or closing in the Times; Rocking is the rocking of the +// hold on closing +inline void fold_begin(fold_state &State, float const Position, float const Range, bool const Opening, fold_times const &Times, fold_rocking const &Rocking) { + State.begun = true; + State.opening = Opening; + State.times = Times; + State.hold_done = Opening || Times.hold <= 0.f || Position <= Range * fold_hold_at; + State.hold_left = 0.f; + State.rock = 0.f; + State.rocking = Opening ? fold_rocking{} : Rocking; +} + +// moves the door from Position towards Target over Range, opening or closing in the Times, for at most Time; returns +// whether it got there, with the time it didn't need left in Time. short steps, so the result hardly depends on the +// frame time +inline bool fold_move_towards(float &Position, float const Target, float const Range, bool const Opening, fold_times const &Times, float &Time) { + auto const direction{Opening ? 1.f : -1.f}; + while (Time > 0.f) { + if ((Position - Target) * direction >= 0.f) { + Position = Target; + return true; + } + auto const step{std::min(Time, 0.002f)}; + // midpoint of the step, for the speed changing along it + auto const middle{Position + 0.5f * step * direction * fold_speed(Position, Range, Opening, Times)}; + auto const next{Position + step * direction * fold_speed(middle, Range, Opening, Times)}; + if ((next - Target) * direction >= 0.f) { + // there: only the time the rest of the way took, at the speed of this step + Time -= step * (Target - Position) / (next - Position); + Position = Target; + return true; + } + Position = next; + Time -= step; + } + return false; +} + +// the rocking of the wings at the time Held since they began to hold for Hold seconds: towards the closed door (a +// negative angle) and back, swelling and fading over the hold +inline float fold_rock(fold_rocking const &Rocking, float const Held, float const Hold) { + constexpr auto pi{std::numbers::pi_v}; + auto const swell{std::sin(pi * std::clamp(Held / Hold, 0.f, 1.f))}; + return -Rocking.amplitude * swell * (0.5f - 0.5f * std::cos(2.f * pi * Rocking.frequency * Held + Rocking.phase)); +} + +// the position of a folding door after Deltatime of the move begun by fold_begin over Range. closing, it holds on the +// way when it has a hold; it stops at the end. without a range or a time of the move it stays where it is +inline float fold_advance(fold_state &State, float const Position, float const Range, float const Deltatime) { + if (Range <= 0.f || (State.opening ? State.times.open : State.times.close) <= 0.f) { + return Position; + } + auto position{std::clamp(Position, 0.f, Range)}; + auto time{Deltatime}; + while (time > 0.f) { + if (State.hold_left > 0.f) { + auto const held{std::min(time, State.hold_left)}; + State.hold_left -= held; + time -= held; + State.hold_done = State.hold_left <= 0.f; + State.rock = State.hold_done ? 0.f : fold_rock(State.rocking, State.times.hold - State.hold_left, State.times.hold); + continue; + } + auto const end{State.opening ? Range : 0.f}; + auto const holding{false == State.hold_done}; + if (false == fold_move_towards(position, holding ? Range * fold_hold_at : end, Range, State.opening, State.times, time) || false == holding) { + break; // the time ran out on the way, or the door is at the end + } + State.hold_left = State.times.hold; + } + return position; +} + // the animation of one wing given in the .mmd, in place of the one the .fiz sets for all door wings of the vehicle // (eu07/maszyna#70). the wing still follows the door of its side, over its own range: // animdoor: { type: range: rangeout: }