From 1687abe58c314f306c750426d2a44617c50edfaf Mon Sep 17 00:00:00 2001 From: milek7 Date: Tue, 10 Sep 2019 22:51:17 +0200 Subject: [PATCH] normal derivatives graph, global crash derail/coupler damage switch --- Globals.cpp | 4 ++++ Globals.h | 1 + McZapkie/Mover.cpp | 55 +++++++++++++++++++++++----------------------- driveruipanels.cpp | 44 +++++++++++++++++++++++++++++-------- driveruipanels.h | 20 ++++++++++++++--- 5 files changed, 84 insertions(+), 40 deletions(-) diff --git a/Globals.cpp b/Globals.cpp index 463565a5..975513c3 100644 --- a/Globals.cpp +++ b/Globals.cpp @@ -928,6 +928,10 @@ global_settings::ConfigParse(cParser &Parser) { Parser >> exec_on_exit; std::replace(std::begin(exec_on_exit), std::end(exec_on_exit), '_', ' '); } + else if (token == "crashdamage") { + Parser.getTokens(1); + Parser >> crash_damage; + } } while ((token != "") && (token != "endconfig")); //(!Parser->EndOfFile) // na koniec trochę zależności if (!bLoadTraction) // wczytywanie drutów i słupów diff --git a/Globals.h b/Globals.h index 921f4dd3..c32788ee 100644 --- a/Globals.h +++ b/Globals.h @@ -188,6 +188,7 @@ struct global_settings { bool dds_upper_origin = false; bool captureonstart = true; bool render_cab = true; + bool crash_damage = true; #ifdef USE_EXTCAM_CAMERA std::string extcam_cmd; diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index e58d7b89..b6badfee 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -1144,31 +1144,33 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) } } - if( ( coupler.Dist < 0 ) - && ( FuzzyLogic( std::abs( CCF ), 5.0 * ( coupler.FmaxC + 1.0 ), p_coupldmg ) ) ) { - // small chance to smash the coupler if it's hit with excessive force - damage_coupler( End ); - } + if (Global.crash_damage) { + if( ( coupler.Dist < 0 ) + && ( FuzzyLogic( std::abs( CCF ), 5.0 * ( coupler.FmaxC + 1.0 ), p_coupldmg ) ) ) { + // small chance to smash the coupler if it's hit with excessive force + damage_coupler( End ); + } - auto const safevelocitylimit { 15.0 }; - auto const velocitydifference { - glm::length( - glm::angleAxis( Rot.Rz, glm::dvec3{ 0, 1, 0 } ) * V - - glm::angleAxis( othervehicle->Rot.Rz, glm::dvec3{ 0, 1, 0 } ) * othervehicle->V ) - * 3.6 }; // m/s -> km/h + auto const safevelocitylimit { 15.0 }; + auto const velocitydifference { + glm::length( + glm::angleAxis( Rot.Rz, glm::dvec3{ 0, 1, 0 } ) * V + - glm::angleAxis( othervehicle->Rot.Rz, glm::dvec3{ 0, 1, 0 } ) * othervehicle->V ) + * 3.6 }; // m/s -> km/h - if( velocitydifference > safevelocitylimit ) { - // HACK: crude estimation for potential derail, will take place with velocity difference > 15 km/h adjusted for vehicle mass ratio - WriteLog( "Bad driving: " + Name + " and " + othervehicle->Name + " collided with velocity " + to_string( velocitydifference, 0 ) + " km/h" ); + if( velocitydifference > safevelocitylimit ) { + // HACK: crude estimation for potential derail, will take place with velocity difference > 15 km/h adjusted for vehicle mass ratio + WriteLog( "Bad driving: " + Name + " and " + othervehicle->Name + " collided with velocity " + to_string( velocitydifference, 0 ) + " km/h" ); + + if( velocitydifference > safevelocitylimit * ( TotalMass / othervehicle->TotalMass ) ) { + derail( 5 ); + } + if( velocitydifference > safevelocitylimit * ( othervehicle->TotalMass / TotalMass ) ) { + othervehicle->derail( 5 ); + } + } + } - if( velocitydifference > safevelocitylimit * ( TotalMass / othervehicle->TotalMass ) ) { - derail( 5 ); - } - if( velocitydifference > safevelocitylimit * ( othervehicle->TotalMass / TotalMass ) ) { - othervehicle->derail( 5 ); - } - } - // adjust velocity and acceleration of affected vehicles if( false == TestFlag( DamageFlag, dtrain_out ) ) { auto const accelerationchange{ ( velocity - V ) / dt }; @@ -1191,8 +1193,8 @@ void TMoverParameters::CollisionDetect(int const End, double const dt) void TMoverParameters::damage_coupler( int const End ) { - if( SetFlag( DamageFlag, dtrain_coupling ) ) - EventFlag = true; + if( SetFlag( DamageFlag, dtrain_coupling ) ) + EventFlag = true; auto &coupler { Couplers[ End ] }; @@ -1201,8 +1203,6 @@ TMoverParameters::damage_coupler( int const End ) { AlarmChainFlag = true; } - // M7TMP: disable coupler breaking because consist cannot be teleported to starting place in one piece - /* coupler.CouplingFlag = 0; if( coupler.Connected != nullptr ) { @@ -1221,7 +1221,6 @@ TMoverParameters::damage_coupler( int const End ) { } } } - */ WriteLog( "Bad driving: " + Name + " broke a coupler" ); } @@ -4647,7 +4646,7 @@ double TMoverParameters::CouplerForce( int const End, double dt ) { coupler.stretch_duration += dt; // give coupler 1 sec of leeway to account for simulation glitches, before checking whether it breaks // (arbitrary) chance to break grows from 10-100% over 10 sec period - if( ( coupler.stretch_duration > 1.f ) + if( Global.crash_damage && ( coupler.stretch_duration > 1.f ) && ( Random() < ( coupler.stretch_duration * 0.1f * dt ) ) ) { damage_coupler( End ); } diff --git a/driveruipanels.cpp b/driveruipanels.cpp index f4d29d58..66e26ab2 100644 --- a/driveruipanels.cpp +++ b/driveruipanels.cpp @@ -533,6 +533,13 @@ debug_panel::render() { &GfxRenderer.settings.traction_debug ); } render_section( "Camera", m_cameralines ); + if (m_input.vehicle && ImGui::CollapsingHeader(STR_C("Normal forces graph"))) { + ImGui::Text("Normal acceleration: %.2f m/s^2", AccN_acc_graph.last_val); + AccN_acc_graph.render(); + + ImGui::Text("Normal jerk: %.2f m/s^3", AccN_jerk_graph.last_val); + AccN_jerk_graph.render(); + } render_section( "Gfx Renderer", m_rendererlines ); // toggles ImGui::Separator(); @@ -673,13 +680,7 @@ debug_panel::update_section_vehicle( std::vector &Output ) { textline += m_buffer.data(); } - Output.emplace_back( textline, Global.UITextColor ); - - if( tprev != simulation::Time.data().wSecond ) { - tprev = simulation::Time.data().wSecond; - Acc = ( mover.Vel - VelPrev ) / 3.6; - VelPrev = mover.Vel; - } + Output.emplace_back( textline, Global.UITextColor ); std::snprintf( m_buffer.data(), m_buffer.size(), @@ -690,8 +691,8 @@ debug_panel::update_section_vehicle( std::vector &Output ) { mover.Adhesive( mover.RunningTrack.friction ), ( mover.SlippingWheels ? " (!)" : "" ), // acceleration - Acc, - mover.AccN + 0.001f, + mover.AccSVBased, + mover.AccN, std::string( std::abs( mover.RunningShape.R ) > 10000.0 ? "~0" : to_string( mover.RunningShape.R, 0 ) ).c_str(), // velocity vehicle.GetVelocity(), @@ -703,6 +704,31 @@ debug_panel::update_section_vehicle( std::vector &Output ) { Output.emplace_back( m_buffer.data(), Global.UITextColor ); + if (!std::isnan(last_time)) { + double dt = Timer::GetTime() - last_time; + AccN_jerk_graph.update((mover.AccN - last_AccN) / dt); + AccN_acc_graph.update(mover.AccN); + } + + last_AccN = mover.AccN; + last_time = Timer::GetTime(); +} + +void debug_panel::graph_data::update(float val) { + data[pos] = val; + + pos++; + if (pos >= data.size()) + pos = 0; + + last_val = val; +} + +void debug_panel::graph_data::render() { + ImGui::PushID(this); + ImGui::SliderFloat(STR_C("##Range"), &range, 0.5f, 60.0f, "%.1f"); + ImGui::PlotLines("##plot", data.data(), data.size(), pos, nullptr, 0.0f, range, ImVec2(0, 100)); + ImGui::PopID(); } std::string diff --git a/driveruipanels.h b/driveruipanels.h index 7fd2d338..0cd628b3 100644 --- a/driveruipanels.h +++ b/driveruipanels.h @@ -106,9 +106,23 @@ private: m_eventqueuelines, m_powergridlines, m_rendererlines; - int tprev { 0 }; // poprzedni czas - double VelPrev { 0.0 }; // poprzednia prędkość - double Acc { 0.0 }; // przyspieszenie styczne + + double last_time = std::numeric_limits::quiet_NaN(); + + struct graph_data + { + double last_val = 0.0; + std::array data = { 0.0f }; + size_t pos = 0; + float range = 25.0f; + + void update(float data); + void render(); + }; + graph_data AccN_jerk_graph; + graph_data AccN_acc_graph; + float last_AccN; + bool m_eventqueueactivevehicleonly { false }; };