4 float acc_prevtime, acc_avg, top_speed, top_speed_time;
5 float physics_update_time, discrete_speed, discrete_acceleration;
8 if(!autocvar__hud_configure)
10 if(!autocvar_hud_panel_physics) return;
11 if(spectatee_status == -1 && (autocvar_hud_panel_physics == 1 || autocvar_hud_panel_physics == 3)) return;
12 if(autocvar_hud_panel_physics == 3 && !(gametype == MAPINFO_TYPE_RACE || gametype == MAPINFO_TYPE_CTS)) return;
15 HUD_Panel_UpdateCvars();
22 panel_pos += '1 1 0' * panel_bg_padding;
23 panel_size -= '2 2 0' * panel_bg_padding;
26 float acceleration_progressbar_scale = 0;
27 if(autocvar_hud_panel_physics_progressbar && autocvar_hud_panel_physics_acceleration_progressbar_scale > 1)
28 acceleration_progressbar_scale = autocvar_hud_panel_physics_acceleration_progressbar_scale;
31 if (autocvar_hud_panel_physics_text_scale <= 0)
34 text_scale = min(autocvar_hud_panel_physics_text_scale, 1);
37 float speed, conversion_factor;
40 switch(autocvar_hud_panel_physics_speed_unit)
45 conversion_factor = 1.0;
49 conversion_factor = 0.0254;
53 conversion_factor = 0.0254 * 3.6;
57 conversion_factor = 0.0254 * 3.6 * 0.6213711922;
61 conversion_factor = 0.0254 * 1.943844492; // 1 m/s = 1.943844492 knots, because 1 knot = 1.852 km/h
65 vector vel = (csqcplayer ? csqcplayer.velocity : pmove_vel);
67 float max_speed = floor( autocvar_hud_panel_physics_speed_max * conversion_factor + 0.5 );
68 if (autocvar__hud_configure)
69 speed = floor( max_speed * 0.65 + 0.5 );
70 else if(autocvar_hud_panel_physics_speed_vertical)
71 speed = floor( vlen(vel) * conversion_factor + 0.5 );
73 speed = floor( vlen(vel - vel.z * '0 0 1') * conversion_factor + 0.5 );
75 //compute acceleration
76 float acceleration, f;
77 if (autocvar__hud_configure)
78 acceleration = autocvar_hud_panel_physics_acceleration_max * 0.3;
81 // 1 m/s = 0.0254 qu/s; 1 g = 9.80665 m/s^2
82 f = time - acc_prevtime;
83 if(autocvar_hud_panel_physics_acceleration_vertical)
84 acceleration = (vlen(vel) - vlen(acc_prevspeed));
86 acceleration = (vlen(vel - '0 0 1' * vel.z) - vlen(acc_prevspeed - '0 0 1' * acc_prevspeed.z));
88 acceleration = acceleration * (1 / max(0.0001, f)) * (0.0254 / 9.80665);
93 if(autocvar_hud_panel_physics_acceleration_movingaverage)
95 f = bound(0, f * 10, 1);
96 acc_avg = acc_avg * (1 - f) + acceleration * f;
97 acceleration = acc_avg;
101 int acc_decimals = 2;
102 if(time > physics_update_time)
104 // workaround for ftos_decimals returning a negative 0
105 if(discrete_acceleration > -1 / pow(10, acc_decimals) && discrete_acceleration < 0)
106 discrete_acceleration = 0;
107 discrete_acceleration = acceleration;
108 discrete_speed = speed;
109 physics_update_time += autocvar_hud_panel_physics_update_interval;
113 float panel_ar = panel_size.x/panel_size.y;
114 vector speed_offset = '0 0 0', acceleration_offset = '0 0 0';
115 if (panel_ar >= 5 && !acceleration_progressbar_scale)
118 if (autocvar_hud_panel_physics_flip)
119 speed_offset.x = panel_size.x;
121 acceleration_offset.x = panel_size.x;
126 if (autocvar_hud_panel_physics_flip)
127 speed_offset.y = panel_size.y;
129 acceleration_offset.y = panel_size.y;
131 int speed_baralign, acceleration_baralign;
132 if (autocvar_hud_panel_physics_baralign == 1)
133 acceleration_baralign = speed_baralign = 1;
134 else if(autocvar_hud_panel_physics_baralign == 4)
135 acceleration_baralign = speed_baralign = 2;
136 else if (autocvar_hud_panel_physics_flip)
138 acceleration_baralign = (autocvar_hud_panel_physics_baralign == 2);
139 speed_baralign = (autocvar_hud_panel_physics_baralign == 3);
143 speed_baralign = (autocvar_hud_panel_physics_baralign == 2);
144 acceleration_baralign = (autocvar_hud_panel_physics_baralign == 3);
146 if (autocvar_hud_panel_physics_acceleration_progressbar_mode == 0)
147 acceleration_baralign = 3; //override hud_panel_physics_baralign value for acceleration
151 if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 2)
152 HUD_Panel_DrawProgressBar(panel_pos + speed_offset, panel_size, "progressbar", speed/max_speed, 0, speed_baralign, autocvar_hud_progressbar_speed_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
153 vector tmp_offset = '0 0 0', tmp_size = '0 0 0';
154 if (autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 2)
156 tmp_size.x = panel_size.x * 0.75;
157 tmp_size.y = panel_size.y * text_scale;
159 tmp_offset.x = panel_size.x - tmp_size.x;
162 tmp_offset.y = (panel_size.y - tmp_size.y) / 2;
163 drawstring_aspect(panel_pos + speed_offset + tmp_offset, ftos(discrete_speed), tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
169 tmp_offset.x = tmp_size.x;
170 if (autocvar_hud_panel_physics_speed_unit_show)
173 tmp_size.x = panel_size.x * (1 - 0.75);
174 tmp_size.y = panel_size.y * 0.4 * text_scale;
175 tmp_offset.y = (panel_size.y * 0.4 - tmp_size.y) / 2;
176 drawstring_aspect(panel_pos + speed_offset + tmp_offset, unit, tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
180 //compute and draw top speed
181 if (autocvar_hud_panel_physics_topspeed)
182 if (autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 2)
184 if (autocvar__hud_configure)
186 top_speed = floor( max_speed * 0.75 + 0.5 );
191 if (speed >= top_speed)
194 top_speed_time = time;
198 f = max(1, autocvar_hud_panel_physics_topspeed_time);
199 // divide by f to make it start from 1
200 f = cos( ((time - top_speed_time) / f) * PI/2 );
202 else //hide top speed 0, it would be stupid
207 //top speed progressbar peak
208 if(speed < top_speed)
209 if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 2)
212 vector peak_size = '0 0 0';
213 if (speed_baralign == 0)
214 peak_offsetX = min(top_speed, max_speed)/max_speed * panel_size.x;
215 else if (speed_baralign == 1)
216 peak_offsetX = (1 - min(top_speed, max_speed)/max_speed) * panel_size.x;
217 else // if (speed_baralign == 2)
218 peak_offsetX = min(top_speed, max_speed)/max_speed * panel_size.x * 0.5;
219 peak_size.x = floor(panel_size.x * 0.01 + 1.5);
220 peak_size.y = panel_size.y;
221 if (speed_baralign == 2) // draw two peaks, on both sides
223 drawfill(panel_pos + speed_offset + eX * (0.5 * panel_size.x + peak_offsetX - peak_size.x), peak_size, autocvar_hud_progressbar_speed_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
224 drawfill(panel_pos + speed_offset + eX * (0.5 * panel_size.x - peak_offsetX + peak_size.x), peak_size, autocvar_hud_progressbar_speed_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
227 drawfill(panel_pos + speed_offset + eX * (peak_offsetX - peak_size.x), peak_size, autocvar_hud_progressbar_speed_color, f * autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
231 tmp_offset.y = panel_size.y * 0.4;
232 tmp_size.x = panel_size.x * (1 - 0.75);
233 tmp_size.y = (panel_size.y - tmp_offset.y) * text_scale;
234 tmp_offset.y += (panel_size.y - tmp_offset.y - tmp_size.y) / 2;
235 drawstring_aspect(panel_pos + speed_offset + tmp_offset, ftos(top_speed), tmp_size, '1 0 0', f * panel_fg_alpha, DRAWFLAG_NORMAL);
243 if(autocvar_hud_panel_physics_progressbar == 1 || autocvar_hud_panel_physics_progressbar == 3)
245 vector progressbar_color;
247 progressbar_color = autocvar_hud_progressbar_acceleration_neg_color;
249 progressbar_color = autocvar_hud_progressbar_acceleration_color;
251 f = acceleration/autocvar_hud_panel_physics_acceleration_max;
252 if (autocvar_hud_panel_physics_acceleration_progressbar_nonlinear)
253 f = (f >= 0 ? sqrt(f) : -sqrt(-f));
255 if (acceleration_progressbar_scale) // allow progressbar to go out of panel bounds
257 tmp_size = acceleration_progressbar_scale * panel_size.x * eX + panel_size.y * eY;
259 if (acceleration_baralign == 1)
260 tmp_offset.x = panel_size.x - tmp_size.x;
261 else if (acceleration_baralign == 2 || acceleration_baralign == 3)
262 tmp_offset.x = (panel_size.x - tmp_size.x) / 2;
269 tmp_size = panel_size;
270 tmp_offset = '0 0 0';
273 HUD_Panel_DrawProgressBar(panel_pos + acceleration_offset + tmp_offset, tmp_size, "accelbar", f, 0, acceleration_baralign, progressbar_color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
276 if(autocvar_hud_panel_physics_text == 1 || autocvar_hud_panel_physics_text == 3)
278 tmp_size.x = panel_size.x;
279 tmp_size.y = panel_size.y * text_scale;
281 tmp_offset.y = (panel_size.y - tmp_size.y) / 2;
283 drawstring_aspect(panel_pos + acceleration_offset + tmp_offset, strcat(ftos_decimals(discrete_acceleration, acc_decimals), "g"), tmp_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);