| 1 | /* |
| 2 | * groundreactions.hxx |
| 3 | * caclulate ground reactions based on ground properties and weather scenarios |
| 4 | * |
| 5 | * SPDX-FileCopyrightText: (C) 2023 Erik Hofman <erik@ehof,am.com> |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | #include <cmath> |
| 11 | |
| 12 | #include <simgear/bvh/BVHMaterial.hxx> |
| 13 | #include <simgear/math/sg_random.hxx> |
| 14 | #include <Main/fg_props.hxx> |
| 15 | |
| 16 | #include "groundreactions.hxx" |
| 17 | |
| 18 | GroundReactions::GroundReactions() |
| 19 | { |
| 20 | precipitation = fgGetNode("/environment/rain-norm", true); |
| 21 | temperature = fgGetNode("/environment/temperature-degc",true); |
| 22 | dew_point = fgGetNode("/environment/dewpoint-degc", true); |
| 23 | ground_wind = fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt",true); |
| 24 | turbulence_gain = fgGetNode("/environment/turbulence/magnitude-norm",true); |
| 25 | turbulence_rate = fgGetNode("/environment/turbulence/rate-hz",true); |
| 26 | turbulence_model = fgGetNode("/environment/params/jsbsim-turbulence-model",true); |
| 27 | |
| 28 | wind_from_north= fgGetNode("/environment/wind-from-north-fps",true); |
| 29 | wind_from_east = fgGetNode("/environment/wind-from-east-fps" ,true); |
| 30 | wind_from_down = fgGetNode("/environment/wind-from-down-fps" ,true); |
| 31 | } |
| 32 | |
| 33 | bool |
| 34 | GroundReactions::update(simgear::BVHMaterial const*& material) |
| 35 | { |
| 36 | if (material) |
| 37 | { |
| 38 | float friction_factor = (*material).get_friction_factor(); |
| 39 | |
| 40 | // TODO: Moist Mud, Wed Mud, Frozen Mud |
| 41 | // Traction coefficient factors for tires vs. dry asphalt: |
| 42 | // Dry Asphalt 1.0 Wet Asphalt 0.75 |
| 43 | // Dry Ice/Snow 0.375 Wet Ice 0.125 |
| 44 | wet = (precipitation->getDoubleValue() > 0.0); |
| 45 | snow = (wet && temperature->getDoubleValue() <= 0.0); |
| 46 | water_body = (*material).solid_is_prop(); |
| 47 | solid = (*material).get_solid(); |
| 48 | frozen = (water_body && solid); |
| 49 | if (frozen) // on ice |
| 50 | { |
| 51 | bumpiness = 0.1f; |
| 52 | if (wet) |
| 53 | { |
| 54 | if (!snow) // Wet Ice |
| 55 | { |
| 56 | roling_friction_factor = 0.05f; |
| 57 | static_friction_factor = 0.125f*friction_factor; |
| 58 | } |
| 59 | else // snow |
| 60 | { |
| 61 | roling_friction_factor = 0.15f; |
| 62 | static_friction_factor = 0.375f*friction_factor; |
| 63 | } |
| 64 | } |
| 65 | else // Dry Ice |
| 66 | { |
| 67 | roling_friction_factor = 0.05f; |
| 68 | static_friction_factor = 0.375f*friction_factor; |
| 69 | } |
| 70 | pressure = (*material).get_load_resistance()*1000; |
| 71 | } |
| 72 | else // not on ice |
| 73 | { |
| 74 | roling_friction_factor = (*material).get_rolling_friction()/0.02f; |
| 75 | bumpiness = (*material).get_bumpiness(); |
| 76 | static_friction_factor = friction_factor; |
| 77 | if (wet) static_friction_factor *= 0.75f; |
| 78 | pressure = (*material).get_load_resistance(); |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | GroundReactions::setPosition(const double pt[3]) |
| 87 | { |
| 88 | pos[0] = pt[0]; |
| 89 | pos[1] = pt[1]; |
| 90 | pos[2] = pt[2]; |
| 91 | } |
| 92 | |
| 93 | float |
| 94 | GroundReactions::getGroundDisplacement() |
| 95 | { |
| 96 | static const float maxGroundBumpAmplitude = 0.4; |
| 97 | |
| 98 | if (bumpiness < 0.001f) return 0.0f; |
| 99 | |
| 100 | float s = sinf(heading); |
| 101 | float c = cosf(heading); |
| 102 | float px = pos[0]*0.1f; |
| 103 | float py = pos[1]*0.1f; |
| 104 | px -= floor(px); |
| 105 | py -= floor(py); |
| 106 | px *= 1000.0f; |
| 107 | py *= 1000.0f; |
| 108 | |
| 109 | int x = c*px - s*py; |
| 110 | int y = s*px + c*py; |
| 111 | |
| 112 | // TODO: differentiate between land/water and |
| 113 | // implement hydrodynamcs |
| 114 | |
| 115 | // pc_init(x+y); |
| 116 | return pc_map_rand(x, y, 1) * bumpiness*maxGroundBumpAmplitude; |
| 117 | } |
| 118 |