1 #include "Atmosphere.hpp"
2 #include "Thruster.hpp"
4 #include "RigidBody.hpp"
5 #include "Integrator.hpp"
6 #include "Propeller.hpp"
7 #include "PistonEngine.hpp"
10 #include "Launchbar.hpp"
11 #include "Surface.hpp"
13 #include "Rotorpart.hpp"
21 void printState(State* s)
24 Math::vmul33(tmp.orient, tmp.v, tmp.v);
25 Math::vmul33(tmp.orient, tmp.acc, tmp.acc);
26 Math::vmul33(tmp.orient, tmp.rot, tmp.rot);
27 Math::vmul33(tmp.orient, tmp.racc, tmp.racc);
29 printf("\nNEW STATE (LOCAL COORDS)\n");
30 printf("pos: %10.2f %10.2f %10.2f\n", tmp.pos[0], tmp.pos[1], tmp.pos[2]);
34 if(i != 0) printf(" ");
35 printf("%6.2f %6.2f %6.2f\n",
36 tmp.orient[3*i+0], tmp.orient[3*i+1], tmp.orient[3*i+2]);
38 printf("v: %6.2f %6.2f %6.2f\n", tmp.v[0], tmp.v[1], tmp.v[2]);
39 printf("acc: %6.2f %6.2f %6.2f\n", tmp.acc[0], tmp.acc[1], tmp.acc[2]);
40 printf("rot: %6.2f %6.2f %6.2f\n", tmp.rot[0], tmp.rot[1], tmp.rot[2]);
41 printf("rac: %6.2f %6.2f %6.2f\n", tmp.racc[0], tmp.racc[1], tmp.racc[2]);
48 for(i=0; i<3; i++) _wind[i] = 0;
50 _integrator.setBody(&_body);
51 _integrator.setEnvironment(this);
53 // Default value of 30 Hz
54 _integrator.setInterval(1.0f/30.0f);
59 _ground_cb = new Ground();
63 _groundEffectSpan = 0;
65 for(i=0; i<3; i++) _wingCenter[i] = 0;
67 _global_ground[0] = 0; _global_ground[1] = 0; _global_ground[2] = 1;
68 _global_ground[3] = -100000;
74 // FIXME: who owns these things? Need a policy
80 void Model::getThrust(float* out)
83 out[0] = out[1] = out[2] = 0;
85 for(i=0; i<_thrusters.size(); i++) {
86 Thruster* t = (Thruster*)_thrusters.get(i);
88 Math::add3(tmp, out, out);
92 void Model::initIteration()
94 // Precompute torque and angular momentum for the thrusters
97 _gyro[i] = _torque[i] = 0;
99 // Need a local altitude for the wind calculation
101 _s->planeGlobalToLocal(_global_ground, lground);
102 float alt = Math::abs(lground[3]);
104 for(i=0; i<_thrusters.size(); i++) {
105 Thruster* t = (Thruster*)_thrusters.get(i);
107 // Get the wind velocity at the thruster location
110 localWind(pos, _s, v, alt);
113 t->setAir(_pressure, _temp, _rho);
114 t->integrate(_integrator.getInterval());
117 Math::add3(v, _torque, _torque);
120 Math::add3(v, _gyro, _gyro);
123 // Displace the turbulence coordinates according to the local wind.
126 Math::mul3(_integrator.getInterval(), _wind, toff);
133 // This function initializes some variables for the rotor calculation
134 // Furthermore it integrates in "void Rotorpart::inititeration
135 // (float dt,float *rot)" the "rotor orientation" by omega*dt for the
136 // 3D-visualization of the heli only. and it compensates the rotation
137 // of the fuselage. The rotor does not follow the rotation of the fuselage.
138 // Therefore its rotation must be subtracted from the orientation of the
141 void Model::initRotorIteration()
143 float dt = _integrator.getInterval();
145 if (!_rotorgear.isInUse()) return;
146 Math::vmul33(_s->orient, _s->rot, lrot);
147 Math::mul3(dt,lrot,lrot);
148 _rotorgear.initRotorIteration(lrot,dt);
151 void Model::iterate()
154 initRotorIteration();
155 _body.recalc(); // FIXME: amortize this, somehow
156 _integrator.calcNewInterval();
159 bool Model::isCrashed()
164 void Model::setCrashed(bool crashed)
169 float Model::getAGL()
174 State* Model::getState()
179 void Model::setState(State* s)
181 _integrator.setState(s);
182 _s = _integrator.getState();
185 RigidBody* Model::getBody()
190 Integrator* Model::getIntegrator()
195 Surface* Model::getSurface(int handle)
197 return (Surface*)_surfaces.get(handle);
200 Rotorgear* Model::getRotorgear(void)
205 int Model::addThruster(Thruster* t)
207 return _thrusters.add(t);
210 Hook* Model::getHook(void)
215 Launchbar* Model::getLaunchbar(void)
220 int Model::numThrusters()
222 return _thrusters.size();
225 Thruster* Model::getThruster(int handle)
227 return (Thruster*)_thrusters.get(handle);
230 void Model::setThruster(int handle, Thruster* t)
232 _thrusters.set(handle, t);
235 int Model::addSurface(Surface* surf)
237 return _surfaces.add(surf);
240 int Model::addGear(Gear* gear)
242 return _gears.add(gear);
245 void Model::addHook(Hook* hook)
250 void Model::addLaunchbar(Launchbar* launchbar)
252 _launchbar = launchbar;
255 void Model::setGroundCallback(Ground* ground_cb)
258 _ground_cb = ground_cb;
261 Ground* Model::getGroundCallback(void)
266 void Model::setGroundEffect(float* pos, float span, float mul)
268 Math::set3(pos, _wingCenter);
269 _groundEffectSpan = span;
273 void Model::setAir(float pressure, float temp, float density)
275 _pressure = pressure;
280 void Model::setWind(float* wind)
282 Math::set3(wind, _wind);
285 void Model::updateGround(State* s)
288 _ground_cb->getGroundPlane(s->pos, _global_ground, dummy);
292 for(i=0; i<_gears.size(); i++) {
293 Gear* g = (Gear*)_gears.get(i);
295 // Get the point of ground contact
296 float pos[3], cmpr[3];
298 g->getCompression(cmpr);
300 Math::mul3(g->getCompressFraction(), cmpr, cmpr);
301 Math::add3(cmpr, pos, pos);
302 // Transform the local coordinates of the contact point to
303 // global coordinates.
305 s->posLocalToGlobal(pos, pt);
307 // Ask for the ground plane in the global coordinate system
308 double global_ground[4];
310 _ground_cb->getGroundPlane(pt, global_ground, global_vel);
311 g->setGlobalGround(global_ground, global_vel);
313 for(i=0; i<_rotorgear.getRotors()->size(); i++) {
314 Rotor* r = (Rotor*)_rotorgear.getRotors()->get(i);
315 r->findGroundEffectAltitude(_ground_cb,s);
321 _hook->getTipGlobalPosition(s, pt);
322 double global_ground[4];
323 _ground_cb->getGroundPlane(pt, global_ground, dummy);
324 _hook->setGlobalGround(global_ground);
327 // The launchbar/holdback
330 _launchbar->getTipGlobalPosition(s, pt);
331 double global_ground[4];
332 _ground_cb->getGroundPlane(pt, global_ground, dummy);
333 _launchbar->setGlobalGround(global_ground);
337 void Model::calcForces(State* s)
339 // Add in the pre-computed stuff. These values aren't part of the
340 // Runge-Kutta integration (they don't depend on position or
341 // velocity), and are therefore constant across the four calls to
342 // calcForces. They get computed before we begin the integration
344 _body.setGyro(_gyro);
345 _body.addTorque(_torque);
347 for(i=0; i<_thrusters.size(); i++) {
348 Thruster* t = (Thruster*)_thrusters.get(i);
349 float thrust[3], pos[3];
350 t->getThrust(thrust);
352 _body.addForce(pos, thrust);
355 // Get a ground plane in local coordinates. The first three
356 // elements are the normal vector, the final one is the distance
357 // from the local origin along that vector to the ground plane
358 // (negative for objects "above" the ground)
360 s->planeGlobalToLocal(_global_ground, ground);
361 float alt = Math::abs(ground[3]);
363 // Gravity, convert to a force, then to local coordinates
365 Glue::geodUp(s->pos, grav);
366 Math::mul3(-9.8f * _body.getTotalMass(), grav, grav);
367 Math::vmul33(s->orient, grav, grav);
368 _body.addForce(grav);
370 // Do each surface, remembering that the local velocity at each
371 // point is different due to rotation.
373 faero[0] = faero[1] = faero[2] = 0;
374 for(i=0; i<_surfaces.size(); i++) {
375 Surface* sf = (Surface*)_surfaces.get(i);
377 // Vsurf = wind - velocity + (rot cross (cg - pos))
379 sf->getPosition(pos);
380 localWind(pos, s, vs, alt);
382 float force[3], torque[3];
383 sf->calcForce(vs, _rho, force, torque);
384 Math::add3(faero, force, faero);
386 _body.addForce(pos, force);
387 _body.addTorque(torque);
389 for (j=0; j<_rotorgear.getRotors()->size();j++)
391 Rotor* r = (Rotor *)_rotorgear.getRotors()->get(j);
394 localWind(pos, s, vs, alt);
395 r->calcLiftFactor(vs, _rho,s);
397 // total torque of rotor (scalar) for calculating new rotor rpm
399 for(i=0; i<r->_rotorparts.size(); i++) {
400 float torque_scalar=0;
401 Rotorpart* rp = (Rotorpart*)r->_rotorparts.get(i);
403 // Vsurf = wind - velocity + (rot cross (cg - pos))
405 rp->getPosition(pos);
406 localWind(pos, s, vs, alt,true);
408 float force[3], torque[3];
409 rp->calcForce(vs, _rho, force, torque, &torque_scalar);
411 rp->getPositionForceAttac(pos);
413 _body.addForce(pos, force);
414 _body.addTorque(torque);
418 if (_rotorgear.isInUse())
421 _rotorgear.calcForces(torque);
422 _body.addTorque(torque);
425 // Account for ground effect by multiplying the vertical force
426 // component by an amount linear with the fraction of the wingspan
428 if ((_groundEffectSpan != 0) && (_groundEffect != 0 ))
430 float dist = ground[3] - Math::dot3(ground, _wingCenter);
431 if(dist > 0 && dist < _groundEffectSpan) {
432 float fz = Math::dot3(faero, ground);
433 fz *= (_groundEffectSpan - dist) / _groundEffectSpan;
435 Math::mul3(fz, ground, faero);
436 _body.addForce(faero);
440 // Convert the velocity and rotation vectors to local coordinates
441 float lrot[3], lv[3];
442 Math::vmul33(s->orient, s->rot, lrot);
443 Math::vmul33(s->orient, s->v, lv);
446 for(i=0; i<_gears.size(); i++) {
447 float force[3], contact[3];
448 Gear* g = (Gear*)_gears.get(i);
450 g->calcForce(&_body, s, lv, lrot);
451 g->getForce(force, contact);
452 _body.addForce(contact, force);
457 _hook->calcForce(_ground_cb, &_body, s, lv, lrot);
458 float force[3], contact[3];
459 _hook->getForce(force, contact);
460 _body.addForce(contact, force);
463 // The launchbar/holdback
465 _launchbar->calcForce(_ground_cb, &_body, s, lv, lrot);
466 float forcelb[3], contactlb[3], forcehb[3], contacthb[3];
467 _launchbar->getForce(forcelb, contactlb, forcehb, contacthb);
468 _body.addForce(contactlb, forcelb);
469 _body.addForce(contacthb, forcehb);
473 void Model::newState(State* s)
477 // Some simple collision detection
480 for(i=0; i<_gears.size(); i++) {
481 Gear* g = (Gear*)_gears.get(i);
483 // Get the point of ground contact
484 float pos[3], cmpr[3];
486 g->getCompression(cmpr);
487 Math::mul3(g->getCompressFraction(), cmpr, cmpr);
488 Math::add3(cmpr, pos, pos);
490 // The plane transformed to local coordinates.
491 double global_ground[4];
492 g->getGlobalGround(global_ground);
494 s->planeGlobalToLocal(global_ground, ground);
495 float dist = ground[3] - Math::dot3(pos, ground);
497 // Find the lowest one
502 if(_agl < -1) // Allow for some integration slop
506 // Calculates the airflow direction at the given point and for the
507 // specified aircraft velocity.
508 void Model::localWind(float* pos, State* s, float* out, float alt, bool is_rotor)
510 float tmp[3], lwind[3], lrot[3], lv[3];
512 // Get a global coordinate for our local position, and calculate
515 double gpos[3]; float up[3];
516 Math::tmul33(s->orient, pos, tmp);
517 for(int i=0; i<3; i++) {
518 gpos[i] = s->pos[i] + tmp[i];
520 Glue::geodUp(gpos, up);
521 _turb->getTurbulence(gpos, alt, up, lwind);
522 Math::add3(_wind, lwind, lwind);
524 Math::set3(_wind, lwind);
527 // Convert to local coordinates
528 Math::vmul33(s->orient, lwind, lwind);
529 Math::vmul33(s->orient, s->rot, lrot);
530 Math::vmul33(s->orient, s->v, lv);
532 _body.pointVelocity(pos, lrot, out); // rotational velocity
533 Math::mul3(-1, out, out); // (negated)
534 Math::add3(lwind, out, out); // + wind
535 Math::sub3(out, lv, out); // - velocity
537 //add the downwash of the rotors if it is not self a rotor
538 if (_rotorgear.isInUse()&&!is_rotor)
540 _rotorgear.getDownWash(pos,lv,tmp);
541 Math::add3(out,tmp, out); // + downwash
547 }; // namespace yasim