RigsofRods
Soft-body Physics Simulation
GUI_SimActorStats.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2019 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "GUI_SimActorStats.h"
23 
24 #include "Application.h"
25 #include "Actor.h"
26 #include "SimData.h"
27 #include "Language.h"
28 #include "GfxActor.h"
29 #include "GUIManager.h"
30 #include "Utils.h"
31 #include "GUIUtils.h"
32 
33 using namespace RoR;
34 using namespace GUI;
35 
37 {
39 
40  ImGuiWindowFlags flags = ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoCollapse |
41  ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar;
42  ImGui::SetNextWindowPos(ImVec2(theme.screen_edge_padding.x, (theme.screen_edge_padding.y + 150)));
43  ImGui::PushStyleColor(ImGuiCol_WindowBg, theme.semitransparent_window_bg);
44  ImGui::Begin("SimActorStats", nullptr, flags);
45 
47  ImGui::Dummy(ImGui::GetStyle().FramePadding);
48 
49  ImGui::Separator();
50  if (m_stat_health < 1.0f)
51  {
52  const float value = static_cast<float>( Round((1.0f - m_stat_health) * 100.0f, 2) );
53  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Vehicle health: "));
54  ImGui::SameLine();
55  ImGui::Text("%.2f%%", value);
56  }
57  else if (m_stat_health >= 1.0f) //When this condition is true, it means that health is at 0% which means 100% of destruction.
58  {
59  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Vehicle destruction: "));
60  ImGui::SameLine();
61  ImGui::Text("100%%");
62  }
63 
64  const int num_beams_i = actorx->FetchNumBeams();
65  const float num_beams_f = static_cast<float>(num_beams_i);
66  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Beam count: "));
67  ImGui::SameLine();
68  ImGui::Text("%d", num_beams_i);
69 
70  const float broken_pct = static_cast<float>( Round((float)m_stat_broken_beams / num_beams_f, 2) * 100.0f );
71  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Broken beams count: "));
72  ImGui::SameLine();
73  ImGui::Text("%d (%.0f%%)", m_stat_broken_beams, broken_pct);
74 
75  const float deform_pct = static_cast<float>( Round((float)m_stat_deformed_beams / num_beams_f * 100.0f) );
76  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Deformed beams count: "));
77  ImGui::SameLine();
78  ImGui::Text("%d (%.0f%%)", m_stat_deformed_beams, deform_pct);
79 
80  const float avg_deform = static_cast<float>( Round((float)m_stat_avg_deform / num_beams_f, 4) * 100.0f );
81  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Average deformation: "));
82  ImGui::SameLine();
83  ImGui::Text("%.2f", avg_deform);
84 
85  const float avg_stress = 1.f - (float)m_stat_beam_stress / num_beams_f;
86  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Average stress: "));
87  ImGui::SameLine();
88  ImGui::Text("%+08.0f", avg_stress);
89 
90  ImGui::NewLine();
91 
92  const int num_nodes = actorx->FetchNumNodes();
93  const int num_wheelnodes = actorx->FetchNumWheelNodes();
94  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Node count: "));
95  ImGui::SameLine();
96  ImGui::Text("%d (%s%d)", num_nodes, "wheels: ", num_wheelnodes);
97 
98  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Total mass: "));
99  ImGui::SameLine();
100  ImGui::Text("%8.2f Kg (%.2f tons)", m_stat_mass_Kg, m_stat_mass_Kg / 1000.0f);
101 
102  ImGui::NewLine();
103 
104  const float n0_velo_len = actorx->GetSimDataBuffer().simbuf_node0_velo.length();
106  {
107  const double PI = 3.14159265358979323846;
108 
109  const float max_rpm = actorx->GetSimDataBuffer().simbuf_engine_max_rpm;
110  const float torque = actorx->GetSimDataBuffer().simbuf_engine_torque;
111  const float turbo_psi = actorx->GetSimDataBuffer().simbuf_engine_turbo_psi;
112  const float cur_rpm = actorx->GetSimDataBuffer().simbuf_engine_rpm;
113  const float wheel_speed = actorx->GetSimDataBuffer().simbuf_wheel_speed;
114 
115  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Engine RPM: "));
116  ImGui::SameLine();
117  ImVec4 rpm_color = (cur_rpm > max_rpm) ? theme.value_red_text_color : ImGui::GetStyle().Colors[ImGuiCol_Text];
118  ImGui::TextColored(rpm_color, "%.2f / %.2f", cur_rpm, max_rpm);
119 
120  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Input shaft RPM: "));
121  ImGui::SameLine();
122  const float inputshaft_rpm = Round(std::max(0.0f, actorx->GetSimDataBuffer().simbuf_inputshaft_rpm));
123  ImGui::TextColored(rpm_color, "%.0f", inputshaft_rpm);
124 
125  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Current torque: "));
126  ImGui::SameLine();
127  ImGui::Text("%.0f Nm", Round(torque));
128 
129  const float currentKw = (((cur_rpm * (torque + ((turbo_psi * 6.8) * torque) / 100) * ( PI / 30)) / 1000));
130  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Current power: "));
131  ImGui::SameLine();
132  ImGui::Text("%.0fhp (%.0fKw)", static_cast<float>(Round(currentKw *1.34102209)), static_cast<float>(Round(currentKw)));
133 
134  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Current gear: "));
135  ImGui::SameLine();
136  ImGui::Text("%d", actorx->GetSimDataBuffer().simbuf_gear);
137 
138  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Drive ratio: "));
139  ImGui::SameLine();
140  ImGui::Text("%.2f:1", actorx->GetSimDataBuffer().simbuf_drive_ratio);
141 
142  float velocityKPH = wheel_speed * 3.6f;
143  float velocityMPH = wheel_speed * 2.23693629f;
144  float carSpeedKPH = n0_velo_len * 3.6f;
145  float carSpeedMPH = n0_velo_len * 2.23693629f;
146 
147  // apply a deadzone ==> no flickering +/-
148  if (fabs(wheel_speed) < 1.0f)
149  {
150  velocityKPH = velocityMPH = 0.0f;
151  }
152  if (fabs(n0_velo_len) < 1.0f)
153  {
154  carSpeedKPH = carSpeedMPH = 0.0f;
155  }
156 
157  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Wheel speed: "));
158  ImGui::SameLine();
159  ImGui::Text("%.0fKm/h (%.0f mph)", Round(velocityKPH), Round(velocityMPH));
160 
161  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Vehicle speed: "));
162  ImGui::SameLine();
163  ImGui::Text("%.0fKm/h (%.0f mph)", Round(carSpeedKPH), Round(carSpeedMPH));
164  }
165  else // Aircraft or boat
166  {
167  float speedKN = n0_velo_len * 1.94384449f;
168  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Current speed: "));
169  ImGui::SameLine();
170  ImGui::Text("%.0f kn (%.0f Km/h; %.0f mph)", Round(speedKN), Round(speedKN * 1.852), Round(speedKN * 1.151));
171 
172  if (actorx->GetSimDataBuffer().simbuf_driveable == AIRPLANE)
173  {
174  const float altitude = actorx->GetSimNodeBuffer()[0].AbsPosition.y / 30.48 * 100;
175  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Altitude: "));
176  ImGui::SameLine();
177  ImGui::Text("%.0f feet (%.0f meters)", Round(altitude), Round(altitude * 0.30480));
178 
179  int engine_num = 1; // UI; count from 1
181  {
182  ImGui::TextColored(theme.value_blue_text_color, "%s #%d:", _LC("SimActorStats", "Engine "), engine_num);
183  ImGui::SameLine();
184  if (ae.simbuf_ae_type == AeroEngineType::AE_XPROP)
185  {
186  ImGui::Text("%.2f RPM", ae.simbuf_ae_rpm);
187  }
188  else // Turbojet
189  {
190  ImGui::Text("%.2f", ae.simbuf_ae_rpm);
191  }
192  ++engine_num;
193  }
194  }
195  else if (actorx->GetSimDataBuffer().simbuf_driveable == BOAT)
196  {
197  int engine_num = 1; // UI; count from 1
198  for (ScrewpropSB& screw: actorx->GetSimDataBuffer().simbuf_screwprops)
199  {
200  ImGui::TextColored(theme.value_blue_text_color, "%s #%d:", _LC("SimActorStats", "Engine "), engine_num);
201  ImGui::SameLine();
202  ImGui::Text("%f%", screw.simbuf_sp_throttle);
203  ++engine_num;
204  }
205  }
206  }
207 
208  ImGui::NewLine();
209 
210  const float speedKPH = actorx->GetSimDataBuffer().simbuf_top_speed * 3.6f;
211  const float speedMPH = actorx->GetSimDataBuffer().simbuf_top_speed * 2.23693629f;
212  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "Top speed: "));
213  ImGui::SameLine();
214  ImGui::Text("%.0f km/h (%.0f mph)", Round(speedKPH), Round(speedMPH));
215 
216  ImGui::NewLine();
217 
218  ImGui::TextColored(theme.value_blue_text_color,"%s", _LC("SimActorStats", "G-Forces:"));
219  ImGui::Text("Vertical: % 6.2fg (%1.2fg)", m_stat_gcur_x, m_stat_gmax_x);
220  ImGui::Text("Sagittal: % 6.2fg (%1.2fg)", m_stat_gcur_y, m_stat_gmax_y);
221  ImGui::Text("Lateral: % 6.2fg (%1.2fg)", m_stat_gcur_z, m_stat_gmax_z);
222 
223  ImGui::NewLine();
224  ImGui::End();
225  ImGui::PopStyleColor(1); // WindowBg
226 }
227 
229 {
230  //taken from TruckHUD.cpp (now removed)
231  beam_t* beam = actor->ar_beams;
232  float average_deformation = 0.0f;
233  float beamstress = 0.0f;
234  float mass = actor->getTotalMass();
235  int beambroken = 0;
236  int beamdeformed = 0;
237  Ogre::Vector3 gcur = actor->getGForces();
238  Ogre::Vector3 gmax = actor->getMaxGForces();
239 
240  for (int i = 0; i < actor->ar_num_beams; i++ , beam++)
241  {
242  if (beam->bm_broken != 0)
243  {
244  beambroken++;
245  }
246  beamstress += std::abs(beam->stress);
247  float current_deformation = fabs(beam->L - beam->refL);
248  if (fabs(current_deformation) > 0.0001f && beam->bm_type != BEAM_HYDRO)
249  {
250  beamdeformed++;
251  }
252  average_deformation += current_deformation;
253  }
254 
255  m_stat_health = ((float)beambroken / (float)actor->ar_num_beams) * 10.0f + ((float)beamdeformed / (float)actor->ar_num_beams);
256  m_stat_broken_beams = beambroken;
257  m_stat_deformed_beams = beamdeformed;
258  m_stat_beam_stress = beamstress;
259  m_stat_mass_Kg = mass;
260  m_stat_avg_deform = average_deformation;
261  m_stat_gcur_x = gcur.x;
262  m_stat_gcur_y = gcur.y;
263  m_stat_gcur_z = gcur.z;
264  m_stat_gmax_x = gmax.x;
265  m_stat_gmax_y = gmax.y;
266  m_stat_gmax_z = gmax.z;
267 }
RoR::ImTextWrappedColorMarked
void ImTextWrappedColorMarked(std::string const &text)
Prints multiline text with '#rrggbb' color markers. Behaves like ImGui::Text* functions.
Definition: GUIUtils.cpp:245
RoR::ActorSB::simbuf_driveable
int simbuf_driveable
Definition: SimBuffers.h:120
RoR::GUIManager::GuiTheme::value_red_text_color
ImVec4 value_red_text_color
Definition: GUIManager.h:76
RoR::GUI::SimActorStats::UpdateStats
void UpdateStats(float dt, ActorPtr actor)
Caution: touches live data, must be synced with sim. thread.
Definition: GUI_SimActorStats.cpp:228
RoR::TRUCK
@ TRUCK
its a truck (or other land vehicle)
Definition: SimData.h:92
RoR::GUIManager::GuiTheme
Definition: GUIManager.h:70
RoR::App::GetGuiManager
GUIManager * GetGuiManager()
Definition: Application.cpp:269
RoR::GUIManager::GuiTheme::value_blue_text_color
ImVec4 value_blue_text_color
Definition: GUIManager.h:77
RoR::GUI::SimActorStats::m_stat_deformed_beams
int m_stat_deformed_beams
Definition: GUI_SimActorStats.h:46
RoR::GfxActor::GetSimNodeBuffer
NodeSB * GetSimNodeBuffer()
Definition: GfxActor.h:120
RoR::GUI::SimActorStats::m_stat_gmax_x
float m_stat_gmax_x
Definition: GUI_SimActorStats.h:53
RoR::GfxActor::FetchNumBeams
int FetchNumBeams() const
Definition: GfxActor.cpp:3318
RoR::ActorSB::simbuf_screwprops
std::vector< ScrewpropSB > simbuf_screwprops
Definition: SimBuffers.h:135
GUIUtils.h
RoR::GfxActor::GetActor
ActorPtr GetActor()
Definition: GfxActor.cpp:288
RoR::ScrewpropSB
Definition: SimBuffers.h:74
RoR::AeroEngineSB
Definition: SimBuffers.h:90
RoR::ActorSB::simbuf_node0_velo
Ogre::Vector3 simbuf_node0_velo
Definition: SimBuffers.h:124
RoR::Round
Ogre::Real Round(Ogre::Real value, unsigned short ndigits=0)
Definition: Utils.cpp:98
RoR::GUI::SimActorStats::m_stat_mass_Kg
float m_stat_mass_Kg
Definition: GUI_SimActorStats.h:48
RoR::GUI::SimActorStats::m_stat_gcur_y
float m_stat_gcur_y
Definition: GUI_SimActorStats.h:51
RoR::ActorSB::simbuf_aeroengines
std::vector< AeroEngineSB > simbuf_aeroengines
Definition: SimBuffers.h:138
RoR::GUI::SimActorStats::Draw
void Draw(RoR::GfxActor *actorx)
Definition: GUI_SimActorStats.cpp:36
RoR::GUI::SimActorStats::m_stat_broken_beams
int m_stat_broken_beams
Definition: GUI_SimActorStats.h:45
Utils.h
Language.h
RoR::GUIManager::GuiTheme::semitransparent_window_bg
ImVec4 semitransparent_window_bg
Definition: GUIManager.h:83
GUI_SimActorStats.h
RefCountingObjectPtr< Actor >
GUIManager.h
Actor.h
RoR::beam_t::stress
float stress
Definition: SimData.h:343
RoR::beam_t::refL
float refL
reference length
Definition: SimData.h:355
RoR::Actor::getMaxGForces
Ogre::Vector3 getMaxGForces()
Definition: Actor.h:108
RoR::GfxActor::FetchNumNodes
int FetchNumNodes() const
Definition: GfxActor.cpp:3319
RoR::GUIManager::GetTheme
GuiTheme & GetTheme()
Definition: GUIManager.h:158
RoR::beam_t
Simulation: An edge in the softbody structure.
Definition: SimData.h:329
RoR::Actor::ar_beams
beam_t * ar_beams
Definition: Actor.h:281
SimData.h
Core data structures for simulation; Everything affected by by either physics, network or user intera...
RoR::ActorSB::simbuf_drive_ratio
float simbuf_drive_ratio
Definition: SimBuffers.h:153
RoR::ActorSB::simbuf_inputshaft_rpm
float simbuf_inputshaft_rpm
Definition: SimBuffers.h:152
RoR::ActorSB::simbuf_wheel_speed
float simbuf_wheel_speed
Definition: SimBuffers.h:127
Application.h
Central state/object manager and communications hub.
RoR::GUI::SimActorStats::m_stat_gmax_z
float m_stat_gmax_z
Definition: GUI_SimActorStats.h:55
RoR::AIRPLANE
@ AIRPLANE
its an airplane
Definition: SimData.h:93
RoR::ActorSB::simbuf_engine_rpm
float simbuf_engine_rpm
Definition: SimBuffers.h:147
RoR::GUI::SimActorStats::m_stat_gmax_y
float m_stat_gmax_y
Definition: GUI_SimActorStats.h:54
RoR::beam_t::bm_type
BeamType bm_type
Definition: SimData.h:347
RoR::Actor::ar_num_beams
int ar_num_beams
Definition: Actor.h:282
RoR::Actor::getTotalMass
float getTotalMass(bool withLocked=true)
Definition: Actor.cpp:810
_LC
#define _LC(ctx, str)
Definition: Language.h:42
RoR::NodeSB::AbsPosition
Ogre::Vector3 AbsPosition
Definition: SimBuffers.h:69
RoR::GUIManager::GuiTheme::screen_edge_padding
ImVec2 screen_edge_padding
Definition: GUIManager.h:87
RoR::ActorSB::simbuf_top_speed
float simbuf_top_speed
Definition: SimBuffers.h:128
RoR::ActorSB::simbuf_engine_max_rpm
float simbuf_engine_max_rpm
Definition: SimBuffers.h:157
RoR::GUI::SimActorStats::m_stat_avg_deform
float m_stat_avg_deform
Definition: GUI_SimActorStats.h:49
RoR::BEAM_HYDRO
@ BEAM_HYDRO
Definition: SimData.h:71
RoR::ActorSB::simbuf_gear
int simbuf_gear
Definition: SimBuffers.h:145
RoR::GUI::SimActorStats::m_stat_gcur_z
float m_stat_gcur_z
Definition: GUI_SimActorStats.h:52
RoR::GUI::SimActorStats::m_stat_health
float m_stat_health
Definition: GUI_SimActorStats.h:44
RoR::ActorSB::simbuf_engine_turbo_psi
float simbuf_engine_turbo_psi
Definition: SimBuffers.h:149
RoR::AeroEngineType::AE_XPROP
@ AE_XPROP
RoR::GUI::SimActorStats::m_stat_beam_stress
float m_stat_beam_stress
Definition: GUI_SimActorStats.h:47
RoR::GfxActor
Definition: GfxActor.h:52
GfxActor.h
Manager for all visuals belonging to a single actor.
RoR::Actor::getTruckName
std::string getTruckName()
Definition: Actor.h:220
RoR::GfxActor::FetchNumWheelNodes
int FetchNumWheelNodes() const
Definition: GfxActor.cpp:3320
RoR::GUI::SimActorStats::m_stat_gcur_x
float m_stat_gcur_x
Definition: GUI_SimActorStats.h:50
RoR::BOAT
@ BOAT
its a boat
Definition: SimData.h:94
RoR::Actor::getGForces
Ogre::Vector3 getGForces()
Definition: Actor.h:86
RoR::GfxActor::GetSimDataBuffer
ActorSB & GetSimDataBuffer()
Definition: GfxActor.h:119
RoR
Definition: AppContext.h:36
RoR::ActorSB::simbuf_has_engine
bool simbuf_has_engine
Definition: SimBuffers.h:144
RoR::ActorSB::simbuf_engine_torque
float simbuf_engine_torque
Definition: SimBuffers.h:151
RoR::beam_t::L
float L
length
Definition: SimData.h:338
RoR::beam_t::bm_broken
bool bm_broken
Definition: SimData.h:351