LIBRCSC Docs
Documentation for HELIOS's BASE LIBRCSC library for RoboCup 2D Simulation League.
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
soccer_math.h
Go to the documentation of this file.
1// -*-c++-*-
2
8/*
9 *Copyright:
10
11 Copyright (C) Hidehisa AKIYAMA
12
13 This code is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27 *EndCopyright:
28 */
29
31
32#ifndef RCSC_SOCCERMATH_H
33#define RCSC_SOCCERMATH_H
34
35#include <rcsc/geom/vector_2d.h>
36
37namespace rcsc {
38
40constexpr double SERVER_EPS = 1.0e-10;
41
43// kick command related
44
45/*-------------------------------------------------------------------*/
58inline
59double
60kick_rate( const double & dist,
61 const double & dir_diff,
62 const double & kprate,
63 const double & bsize,
64 const double & psize,
65 const double & kmargin )
66{
67 return kprate * ( 1.0
68 - 0.25 * std::fabs( dir_diff ) / 180.0
69 - 0.25 * ( dist - bsize - psize ) / kmargin );
70}
71
73// dash command related
74
75/*-------------------------------------------------------------------*/
83inline
84double
85dir_rate( const double & dir,
86 const double & back_dash_rate,
87 const double & side_dash_rate )
88{
89 return ( std::fabs( dir ) > 90.0
90 ? back_dash_rate - ( ( back_dash_rate - side_dash_rate )
91 * ( 1.0 - ( std::fabs( dir ) - 90.0 ) / 90.0 )
92 )
93 : side_dash_rate + ( ( 1.0 - side_dash_rate )
94 * ( 1.0 - std::fabs( dir ) / 90.0 ) )
95 );
96}
97
99// turn command related
100
101/*-------------------------------------------------------------------*/
110inline
111double
112effective_turn( const double & turn_moment,
113 const double & speed,
114 const double & inertiamoment )
115{
116 return turn_moment / ( 1.0 + inertiamoment * speed );
117}
118
120// dash command related
121
122/*-------------------------------------------------------------------*/
133inline
134double
135final_speed( const double & dash_power,
136 const double & dprate,
137 const double & effort,
138 const double & decay )
139{
140 // if player continue to run using the same dash power
141 // archieved speed at n step later is sum of infinite geometric series
142
143 // !! NOTE !!
144 // You must compare this value to the PlayerType::playerSpeedMax()
145
146 //return ( (dash_power * dprate * effort) // == accel
147 // * (1.0 / (1.0 - decay)) ); // == sum inf geom series
148 return ( ( std::fabs( dash_power ) * dprate * effort ) // == accel
149 / ( 1.0 - decay ) ); // == sum inf geom series
150}
151
152/*-------------------------------------------------------------------*/
163inline
164bool
165can_over_speed_max( const double & dash_power,
166 const double & dprate,
167 const double & effort,
168 const double & decay,
169 const double & speed_max )
170
171{
172 return ( std::fabs( dash_power ) * dprate * effort // max accel
173 > speed_max * ( 1.0 - decay ) ); // is over speed decay
174}
175
177// predictor method for inertia movement
178
179/*-------------------------------------------------------------------*/
188inline
189Vector2D
190inertia_n_step_travel( const Vector2D & initial_vel,
191 const int n_step,
192 const double & decay )
193{
194 return Vector2D( initial_vel )
195 *= ( ( 1.0 - std::pow( decay, n_step ) ) / ( 1.0 - decay ) );
196}
197
198/*-------------------------------------------------------------------*/
208inline
209Vector2D
210inertia_n_step_point( const Vector2D & initial_pos,
211 const Vector2D & initial_vel,
212 const int n_step,
213 const double & decay )
214{
215 /*
216 return
217 initial_pos
218 + initial_vel * ( (1.0 - std::pow(decay, static_cast<double>(n_step)))
219 / (1.0 - decay) );
220 */
221 /*
222 return Vector2D(initial_vel
223 * (1.0 - std::pow(decay, n_step) / (1.0 - decay)))
224 += initial_pos;
225 */
226 /*
227 double rate = ( (1.0 - std::pow(decay, static_cast<double>(n_step)))
228 / (1.0 - decay) );
229 return initial_pos + ( initial_vel * rate );
230 */
231 return Vector2D( initial_pos )
232 += inertia_n_step_travel( initial_vel, n_step, decay );
233}
234
235/*-------------------------------------------------------------------*/
244inline
245double
246inertia_n_step_distance( const double & initial_speed,
247 const int n_step,
248 const double & decay )
249{
250 return initial_speed
251 * ( 1.0 - std::pow( decay, n_step ) )
252 / ( 1.0 - decay );
253}
254
255/*-------------------------------------------------------------------*/
264inline
265double
266inertia_n_step_distance( const double & initial_speed,
267 const double & n_step_real,
268 const double & decay )
269{
270 return initial_speed
271 * ( 1.0 - std::pow( decay, n_step_real ) )
272 / ( 1.0 - decay );
273}
274
275
276/*-------------------------------------------------------------------*/
283inline
284Vector2D
285inertia_final_travel( const Vector2D & initial_vel,
286 const double & decay )
287{
288 return Vector2D( initial_vel ) /= ( 1.0 - decay );
289}
290
291/*-------------------------------------------------------------------*/
299inline
300Vector2D
301inertia_final_point( const Vector2D & initial_pos,
302 const Vector2D & initial_vel,
303 const double & decay )
304{
305 /*
306 return Vector2D(initial_vel / (1.0 - decay)) += initial_pos;
307 */
308 /*
309 Vector2D end_pos(initial_vel);
310 end_pos /= (1.0 - decay);
311 return end_pos += initial_pos;
312 */
313 /*
314 return initial_pos + ( initial_vel / (1.0 - decay) );
315 */
316 return Vector2D( initial_pos )
317 += inertia_final_travel( initial_vel, decay );
318}
319
320/*-------------------------------------------------------------------*/
327inline
328double
329inertia_final_distance( const double & initial_speed,
330 const double & decay )
331{
332 return initial_speed / ( 1.0 - decay );
333}
334
336// localization
337
338/*-------------------------------------------------------------------*/
347inline
348double
349quantize( const double & value,
350 const double & qstep )
351{
352 return rint( value / qstep ) * qstep;
353}
354
355/*-------------------------------------------------------------------*/
374inline
375double
376quantize_dist( const double & unq_dist,
377 const double & qstep )
378{
379 return quantize( std::exp
380 ( quantize( std::log
381 ( unq_dist + SERVER_EPS ), qstep ) ), 0.1 );
382}
383
384/*-------------------------------------------------------------------*/
391inline
392double
393unquantize_min( const double & dist,
394 const double & qstep )
395{
396 return ( rint( dist / qstep ) - 0.5 ) * qstep;
397}
398
399/*-------------------------------------------------------------------*/
406inline
407double
408unquantize_max( const double & dist,
409 const double & qstep )
410{
411 return ( rint( dist / qstep ) + 0.5 ) * qstep;
412}
413
414/*-------------------------------------------------------------------*/
426Vector2D
427wind_effect( const double & speed,
428 const double & weight,
429 const double & wind_force,
430 const double & wind_dir,
431 const double & wind_weight,
432 const double & wind_rand,
433 Vector2D * wind_error );
434
435/*-------------------------------------------------------------------*/
442double
443unquantize_error( const double & see_dist,
444 const double & qstep );
445
446} // end namespace
447
448#endif
2D point vector class
Definition: vector_2d.h:46
double unquantize_min(const double &dist, const double &qstep)
calculate minnimal value by inverse quantize function
Definition: soccer_math.h:393
constexpr double SERVER_EPS
epsilon value
Definition: soccer_math.h:40
double unquantize_max(const double &dist, const double &qstep)
calculate maximal value by inverse quantize function
Definition: soccer_math.h:408
Vector2D wind_effect(const double &speed, const double &weight, const double &wind_force, const double &wind_dir, const double &wind_weight, const double &wind_rand, Vector2D *wind_error)
calculate wind effect
Vector2D inertia_n_step_point(const Vector2D &initial_pos, const Vector2D &initial_vel, const int n_step, const double &decay)
estimate future point after n steps only by inertia. No additional acceleration
Definition: soccer_math.h:210
double unquantize_error(const double &see_dist, const double &qstep)
calculate minmax error range by inverse quantize function
double final_speed(const double &dash_power, const double &dprate, const double &effort, const double &decay)
calculate converged max speed, when using "dash_power"
Definition: soccer_math.h:135
double effective_turn(const double &turn_moment, const double &speed, const double &inertiamoment)
calculate effective turn moment. it may be useful to redefine this algorighm in movement action modul...
Definition: soccer_math.h:112
double quantize(const double &value, const double &qstep)
quantize a floating point number
Definition: soccer_math.h:349
double kick_rate(const double &dist, const double &dir_diff, const double &kprate, const double &bsize, const double &psize, const double &kmargin)
calculate kick rate
Definition: soccer_math.h:60
Vector2D inertia_final_travel(const Vector2D &initial_vel, const double &decay)
calculate total travel only by inertia movement.
Definition: soccer_math.h:285
Vector2D inertia_n_step_travel(const Vector2D &initial_vel, const int n_step, const double &decay)
estimate future travel after n steps only by inertia. No additional acceleration.
Definition: soccer_math.h:190
double quantize_dist(const double &unq_dist, const double &qstep)
calculate quantized distance value about dist quantization
Definition: soccer_math.h:376
double inertia_n_step_distance(const double &initial_speed, const int n_step, const double &decay)
estimate travel distance only by inertia. No additional acceleration
Definition: soccer_math.h:246
double inertia_final_distance(const double &initial_speed, const double &decay)
calculate total travel distance only by inertia.
Definition: soccer_math.h:329
double dir_rate(const double &dir, const double &back_dash_rate, const double &side_dash_rate)
calculate effective dash power rate according to the input dash direction
Definition: soccer_math.h:85
bool can_over_speed_max(const double &dash_power, const double &dprate, const double &effort, const double &decay, const double &speed_max)
check if player's poteantial max speed is over plsyer_speed_max parameter.
Definition: soccer_math.h:165
Vector2D inertia_final_point(const Vector2D &initial_pos, const Vector2D &initial_vel, const double &decay)
calculate final reach point only by inertia.
Definition: soccer_math.h:301
2d vector class Header File.