7 #include "BodyEnvironment.hpp"
8 #include "RigidBody.hpp"
10 #include <simgear/scene/material/mat.hxx>
11 #include <FDM/flight.hxx>
14 static const float YASIM_PI = 3.14159265358979323846;
15 static const float maxGroundBumpAmplitude=0.4;
16 //Amplitude can be positive and negative
22 _pos[i] = _cmpr[i] = 0;
33 _ground_frictionFactor = 1;
34 _ground_rollingFriction = 0.02;
35 _ground_loadCapacity = 1e30;
36 _ground_loadResistance = 1e30;
38 _ground_bumpiness = 0;
43 _reduceFrictionByExtension = 0;
44 _spring_factor_not_planing = 1;
47 _ignoreWhileSolving = 0;
50 _global_ground[i] = _global_vel[i] = 0;
51 _global_ground[2] = 1;
52 _global_ground[3] = -1e3;
55 void Gear::setPosition(float* position)
58 for(i=0; i<3; i++) _pos[i] = position[i];
61 void Gear::setCompression(float* compression)
64 for(i=0; i<3; i++) _cmpr[i] = compression[i];
67 void Gear::setSpring(float spring)
72 void Gear::setDamping(float damping)
77 void Gear::setStaticFriction(float sfric)
82 void Gear::setDynamicFriction(float dfric)
87 void Gear::setBrake(float brake)
89 _brake = Math::clamp(brake, 0, 1);
92 void Gear::setRotation(float rotation)
97 void Gear::setExtension(float extension)
99 _extension = Math::clamp(extension, 0, 1);
102 void Gear::setCastering(bool c)
107 void Gear::setContactPoint(bool c)
112 void Gear::setOnWater(bool c)
117 void Gear::setOnSolid(bool c)
122 void Gear::setIgnoreWhileSolving(bool c)
124 _ignoreWhileSolving = c;
127 void Gear::setSpringFactorNotPlaning(float f)
129 _spring_factor_not_planing = f;
132 void Gear::setSpeedPlaning(float s)
137 void Gear::setReduceFrictionByExtension(float s)
139 _reduceFrictionByExtension = s;
142 void Gear::setInitialLoad(float l)
147 void Gear::setGlobalGround(double *global_ground, float* global_vel,
148 double globalX, double globalY,
149 const SGMaterial *material)
152 double frictionFactor,rollingFriction,loadCapacity,loadResistance,bumpiness;
155 for(i=0; i<4; i++) _global_ground[i] = global_ground[i];
156 for(i=0; i<3; i++) _global_vel[i] = global_vel[i];
159 loadCapacity = (*material).get_load_resistance();
160 frictionFactor =(*material).get_friction_factor();
161 rollingFriction = (*material).get_rolling_friction();
162 loadResistance = (*material).get_load_resistance();
163 bumpiness = (*material).get_bumpiness();
164 isSolid = (*material).get_solid();
166 // no material, assume solid
167 loadCapacity = DBL_MAX;
168 frictionFactor = 1.0;
169 rollingFriction = 0.02;
170 loadResistance = DBL_MAX;
174 _ground_frictionFactor = frictionFactor;
175 _ground_rollingFriction = rollingFriction;
176 _ground_loadCapacity = loadCapacity;
177 _ground_loadResistance = loadResistance;
178 _ground_bumpiness = bumpiness;
179 _ground_isSolid = isSolid;
185 void Gear::getPosition(float* out)
188 for(i=0; i<3; i++) out[i] = _pos[i];
191 void Gear::getCompression(float* out)
194 for(i=0; i<3; i++) out[i] = _cmpr[i];
197 void Gear::getGlobalGround(double* global_ground)
200 for(i=0; i<4; i++) global_ground[i] = _global_ground[i];
203 float Gear::getSpring()
208 float Gear::getDamping()
213 float Gear::getStaticFriction()
218 float Gear::getDynamicFriction()
223 float Gear::getBrake()
228 float Gear::getRotation()
233 float Gear::getExtension()
238 void Gear::getForce(float* force, float* contact)
240 Math::set3(_force, force);
241 Math::set3(_contact, contact);
249 float Gear::getCompressFraction()
254 bool Gear::getCastering()
259 bool Gear::getGroundIsSolid()
261 return _ground_isSolid;
264 float Gear::getBumpAltitude()
266 if (_ground_bumpiness<0.001) return 0.0;
267 double x = _global_x*0.1;
268 double y = _global_y*0.1;
273 //now x and y are in the range of 0..2pi
274 //we need a function, that is periodically on 2pi and gives some
275 //height. This is not very fast, but for a beginning.
276 //maybe this should be done by interpolating between some precalculated
278 float h = Math::sin(x)+Math::sin(7*x)+Math::sin(8*x)+Math::sin(13*x);
279 h += Math::sin(2*y)+Math::sin(5*y)+Math::sin(9*y*x)+Math::sin(17*y);
281 return h*(1/8.)*_ground_bumpiness*maxGroundBumpAmplitude;
284 void Gear::calcForce(RigidBody* body, State *s, float* v, float* rot)
286 // Init the return values
288 for(i=0; i<3; i++) _force[i] = _contact[i] = 0;
290 // Don't bother if it's not down
298 // Dont bother if we are in the "wrong" ground
299 if (!((_onWater&&!_ground_isSolid)||(_onSolid&&_ground_isSolid))) {
308 // The ground plane transformed to the local frame.
310 s->planeGlobalToLocal(_global_ground, ground);
312 // The velocity of the contact patch transformed to local coordinates.
314 s->velGlobalToLocal(_global_vel, glvel);
316 // First off, make sure that the gear "tip" is below the ground.
317 // If it's not, there's no force.
318 float a = ground[3] - Math::dot3(_pos, ground);
319 float BumpAltitude=0;
320 if (a<maxGroundBumpAmplitude)
322 BumpAltitude=getBumpAltitude();
335 // Now a is the distance from the tip to ground, so make b the
336 // distance from the base to ground. We can get the fraction
337 // (0-1) of compression from a/(a-b). Note the minus sign -- stuff
338 // above ground is negative.
340 Math::add3(_cmpr, _pos, tmp);
341 float b = ground[3] - Math::dot3(tmp, ground)+BumpAltitude;
343 // Calculate the point of ground _contact.
349 _contact[i] = _pos[i] + _frac*_cmpr[i];
351 // Turn _cmpr into a unit vector and a magnitude
353 float clen = Math::mag3(_cmpr);
354 Math::mul3(1/clen, _cmpr, cmpr);
356 // Now get the velocity of the point of contact
358 body->pointVelocity(_contact, rot, cv);
359 Math::add3(cv, v, cv);
360 Math::sub3(cv, glvel, cv);
362 // Finally, we can start adding up the forces. First the spring
363 // compression. (note the clamping of _frac to 1):
364 _frac = (_frac > 1) ? 1 : _frac;
366 // Add the initial load to frac, but with continous transistion around 0
367 float frac_with_initial_load;
368 if (_frac>0.2 || _initialLoad==0.0)
369 frac_with_initial_load = _frac+_initialLoad;
371 frac_with_initial_load = (_frac+_initialLoad)
372 *_frac*_frac*3*25-_frac*_frac*_frac*2*125;
374 float fmag = frac_with_initial_load*clen*_spring;
375 if (_speed_planing>0)
377 float v = Math::mag3(cv);
378 if (v < _speed_planing)
380 float frac = v/_speed_planing;
381 fmag = fmag*_spring_factor_not_planing*(1-frac)+fmag*frac;
384 // Then the damping. Use the only the velocity into the ground
385 // (projection along "ground") projected along the compression
386 // axis. So Vdamp = ground*(ground dot cv) dot cmpr
387 Math::mul3(Math::dot3(ground, cv), ground, tmp);
388 float dv = Math::dot3(cmpr, tmp);
389 float damp = _damp * dv;
390 if(damp > fmag) damp = fmag; // can't pull the plane down!
391 if(damp < -fmag) damp = -fmag; // sanity
393 // The actual force applied is only the component perpendicular to
394 // the ground. Side forces come from velocity only.
395 _wow = (fmag - damp) * -Math::dot3(cmpr, ground);
396 Math::mul3(-_wow, ground, _force);
398 // Wheels are funky. Split the velocity along the ground plane
399 // into rolling and skidding components. Assuming small angles,
400 // we generate "forward" and "left" unit vectors (the compression
401 // goes "up") for the gear, make a "steer" direction from these,
402 // and then project it onto the ground plane. Project the
403 // velocity onto the ground plane too, and extract the "steer"
404 // component. The remainder is the skid velocity.
406 float gup[3]; // "up" unit vector from the ground
407 Math::set3(ground, gup);
408 Math::mul3(-1, gup, gup);
410 float xhat[] = {1,0,0};
411 float steer[3], skid[3];
412 Math::cross3(gup, xhat, skid); // up cross xhat =~ skid
413 Math::unit3(skid, skid); // == skid
415 Math::cross3(skid, gup, steer); // skid cross up == steer
418 // Correct for a rotation
419 float srot = Math::sin(_rot);
420 float crot = Math::cos(_rot);
423 steer[0] = crot*tx + srot*ty;
424 steer[1] = -srot*tx + crot*ty;
428 skid[0] = crot*tx + srot*ty;
429 skid[1] = -srot*tx + crot*ty;
432 float vsteer = Math::dot3(cv, steer);
433 float vskid = Math::dot3(cv, skid);
434 float wgt = Math::dot3(_force, gup); // force into the ground
437 _rollSpeed = Math::sqrt(vsteer*vsteer + vskid*vskid);
438 // Don't modify caster angle when the wheel isn't moving,
439 // or else the angle will animate the "jitter" of a stopped
441 if(_rollSpeed > 0.05)
442 _casterAngle = Math::atan2(vskid, vsteer);
451 fsteer = (_brake * _ground_frictionFactor
452 +(1-_brake)*_ground_rollingFriction
453 )*calcFriction(wgt, vsteer);
454 fskid = calcFriction(wgt, vskid)*(_ground_frictionFactor);
458 fsteer = calcFrictionFluid(wgt, vsteer)*_ground_frictionFactor;
459 fskid = 10*calcFrictionFluid(wgt, vskid)*_ground_frictionFactor;
460 //factor 10: floats have different drag in x and y.
462 if(vsteer > 0) fsteer = -fsteer;
463 if(vskid > 0) fskid = -fskid;
465 //reduce friction if wanted by _reduceFrictionByExtension
466 float factor = (1-_frac)*(1-_reduceFrictionByExtension)+_frac*1;
467 factor = Math::clamp(factor,0,1);
471 // Phoo! All done. Add it up and get out of here.
472 Math::mul3(fsteer, steer, tmp);
473 Math::add3(tmp, _force, _force);
475 Math::mul3(fskid, skid, tmp);
476 Math::add3(tmp, _force, _force);
479 float Gear::calcFriction(float wgt, float v) //used on solid ground
481 // How slow is stopped? 10 cm/second?
482 const float STOP = 0.1f;
483 const float iSTOP = 1.0f/STOP;
485 if(v < STOP) return v*iSTOP * wgt * _sfric;
486 else return wgt * _dfric;
489 float Gear::calcFrictionFluid(float wgt, float v) //used on fluid ground
491 // How slow is stopped? 1 cm/second?
492 const float STOP = 0.01f;
493 const float iSTOP = 1.0f/STOP;
495 if(v < STOP) return v*iSTOP * wgt * _sfric;
496 else return wgt * _dfric*v*v*0.01;
497 //*0.01: to get _dfric of the same size than _dfric on solid
499 }; // namespace yasim