/* Physics.h - Tables with default physics data for trains and road vehicles */ #ifndef PHYSICS_H #define PHYSICS_H /******** Conversion functions ********/ // (obsolete/taken out) /******** Universal constants ********/ // Gradient of the hills in parts per 1000 #define SLOPE_PARTS_PER_1000 50 // Acceleration magic constant for trains; some number that should relate SI units and the TTD units #define T_ACCEL_MAGIC_CONSTANT 220 // Acceleration magic constant for road vehicles #define RV_ACCEL_MAGIC_CONSTANT 45 // Gravity acceleration // Measured in 10^-2 m s^-2 = cm s^-2. Earth gravity is 9.8 m s^-2. // (TODO: (Future work) Change this constant to a variable for worlds with lower gravity. What about trains on the Moon or on Mars?) #define G_ACCEL 980 /******** Road vehicle physics (coefficents and ratings) ********/ //Maximum speed for all road vehicles (in 10^-2 m/s, i.e. 1000 = 10 m/s = 36 km/h) //Note: the old speed limit (without realistic acceleration) is 127 km/h (value 3550) #define ROAD_MAX_SPEED 14220 // Minimum weight for road vehicle (in 1/4 tons) for curve speed calculation (must be > 0) #define RV_MIN_WEIGHT 4 // Speed tolerance for comparisons, used for road vehicles, in 0.01 m/s #define RV_SPEED_TOLERANCE_UP 80 #define RV_SPEED_TOLERANCE_DN 0 //Number of road vehicle physics classes (RVPC) #define NUM_RVPC 11 //Indices for RVPC enum { RVPC_SPEED = 0, RVPC_AIRDRAG = 1, RVPC_END }; // Table of default constants static const uint16 _rv_default_physics[RVPC_END][NUM_RVPC] = { { 25, 38, 51, 77, 102, 154, 205, 308, 411, 617, 0xFFFF }, // Road vehicles up to these max speeds (km/h) { 192, 128, 96, 64, 48, 32, 24, 16, 12, 8, 6 }, // have these airdrag coefficients }; // Default maximum tractive effort ratio (76 = 30%) #define RV_MAXTE 51 // Constant friction in N #define RV_COEFF_FR_CONST 800 // Rolling friction coefficient in 10^-3 N kg^-1 (10^-3 m s^-2) #define RV_COEFF_FR_ROLL 120 // Internal friction coefficient in kg s^-1 (N s m^-1) #define RV_COEFF_FR_INT 18 // Curve constant for road vehicles in 10^3 N m (10^3 kg m^2 s^-2) #define RV_CURVE_CONST 2500 /******** Train physics ********/ /* Maximum speed for all railtypes, in m/s and in km/h. * The second should be exactly 3.6 times as large as the first. * Otherwise, unexpected things may happen (i.e. trains flying all over the place) */ #define RAIL_MAX_SPEED 1500 #define RAIL_MAX_SPEED_KMH 4800 // Speed tolerance for comparisons, used for trains, in 0.01 m/s #define T_SPEED_TOLERANCE_UP 60 #define T_SPEED_TOLERANCE_DN 0 // Minimum weight per wagon (in tons) for curve speed calculation (must be > 0) #define T_WAGON_MIN_WEIGHT 5 // Default TE ratio for trains (to be used if the TE ratio is not present or defined to be 0) // measured in parts per 255; for TTDPatch compatibility, use 76 (= 30%) #define T_MAXTE 76 // Number of train physics classes (TPC) #define NUM_TPC 15 // Indices for TPC enum { TPC_SPEED = 0, TPC_AIRDRAG = 1, TPC_END }; static const uint16 _t_default_physics[TPC_END][NUM_TPC] = { { 25, 38, 51, 77, 102, 154, 205, 308, 411, 617, 823, 1235, 1647, 2471, 0xFFFF }, // Trains up to these max speeds (km/h) { 192, 128, 96, 64, 48, 32, 24, 16, 12, 8, 6, 4, 3, 2, 1 }, // have these airdrag coefficients }; /* The following constants are coefficients for the force equations in GetTrainAcceleration * The indices are railtypes: RAILTYPE_RAIL, RAILTYPE_ELECTRIC, RAILTYPE_MONO, RAILTYPE_MAGLEV * airdrag air resistance for carriages, quadratic in speed, coeff in 10^-3 kg m^-1 (10^-3 N s^2 m^-2) * intfr internal friction, linear in speed, coeff in kg s^-1 (N s m^-1) * rollfr rolling friction, constant in speed, linear in mass (weight), coeff in 10^-4 m s^-2 (10^-4 N kg^-1) * staticfr static friction, constant, coeff in kg m s^-2 (N) * (default railtype values; air drag of front engine is determined by its maximum speed or by newgrf override) */ static const int coeff_airdrag_percar[RAILTYPE_END] = { 500, 500, 450, 400}; static const int coeff_intfr[RAILTYPE_END] = { 150, 150, 50, 20}; static const int coeff_rollfr[RAILTYPE_END] = { 550, 550, 400, 0}; static const int coeff_staticfr[RAILTYPE_END] = {1500, 1500, 400, 300}; /* Maximum braking force of carriage */ static const int coeff_brake_percar[RAILTYPE_END] = {30000, 30000, 45000, 75000}; /* MagLev trains use air resistance to brake at high speeds * The air brake force of MagLev trains is equal to the original air resistance times the multiplier T_MAGLEV_AIRBRAKE_MULT * Below the threshold speed T_MAGLEV_MECHBRAKE_MAXSPEED (in m/s), the mechanical brakes of the carriages (not locomotives!) * apply brake force as well. This is to overcome the problem of the airbrakes being weak at slow speeds. * Moreover, when braking, the traction force is reversed, which is most effective at slow speeds. * NOTE: To disable the airbrakes (i.e. to use mechanical brakes and negative traction only), * set the multiplier to 0, and the threshold for mechanical brakes very high (e.g. to RAIL_MAX_SPEED). */ #define T_MAGLEV_AIRBRAKE_MULT 24 #define T_MAGLEV_MECHBRAKE_MAXSPEED 50 /* Speed which is considered "slow". * The threshold (in 0.01 m/s) between slow speed power handling and normal power handling * Also a typical speed for some other processes (e.g. stopping at station, underpowered engine traction handling) */ static const int slow_speed[RAILTYPE_END] = {800, 800, 1200, 2000}; /* Maximum speed in depot (multiplier; maximum speed in units of 0.01 m/s is equal to the "slow speed" times this value) */ #define T_DEPOT_SPEED_MULT 3 /* Curve constants * Curve constants determine how fast train can move through curves * They are equal to maximum force times curve radius. Units are 10^3 N m (10^3 kg m^2 s^-2) */ static const int sbend_curve_const[RAILTYPE_END] = {180000, 180000, 500000, 1100000}; static const int huge_curve_const[RAILTYPE_END] = { 27000, 27000, 70000, 150000}; static const int large_curve_const[RAILTYPE_END] = { 55000, 55000, 150000, 330000}; static const int medium_curve_const[RAILTYPE_END] = { 18000, 18000, 45000, 95000}; static const int small_curve_const[RAILTYPE_END] = { 6500, 6500, 14000, 30000}; /******** Piecewise linear approximation to square root function ********/ /* Upper bound of error for x > 100 is 6.1%: * (1 - 0.061) * sqrt(x) < SqrtLinApprox(x) < (1 + 0.061) * sqrt(x) */ static inline int32 SqrtLinApprox(int32 x) { int i; assert(x >= 0); if (x == 0) return 0; else if (x <= 2) return 1; else if (x <= (1 << 29)) { for (i = 1; i < 14; i++) { if (x <= (1 << (2 * i + 1))) return (x + (1 << (2 * i))) >> (i + 1); } } // (otherwise we have x > 2^29, then do a little workaround to prevent overflows) return ((x >> 1) + (1 << 29)) >> 15; } #endif // PHYSICS_H