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"
22 void printState(State* s)
25 Math::vmul33(tmp.orient, tmp.v, tmp.v);
26 Math::vmul33(tmp.orient, tmp.acc, tmp.acc);
27 Math::vmul33(tmp.orient, tmp.rot, tmp.rot);
28 Math::vmul33(tmp.orient, tmp.racc, tmp.racc);
30 printf("\nNEW STATE (LOCAL COORDS)\n");
31 printf("pos: %10.2f %10.2f %10.2f\n", tmp.pos[0], tmp.pos[1], tmp.pos[2]);
35 if(i != 0) printf(" ");
36 printf("%6.2f %6.2f %6.2f\n",
37 tmp.orient[3*i+0], tmp.orient[3*i+1], tmp.orient[3*i+2]);
39 printf("v: %6.2f %6.2f %6.2f\n", tmp.v[0], tmp.v[1], tmp.v[2]);
40 printf("acc: %6.2f %6.2f %6.2f\n", tmp.acc[0], tmp.acc[1], tmp.acc[2]);
41 printf("rot: %6.2f %6.2f %6.2f\n", tmp.rot[0], tmp.rot[1], tmp.rot[2]);
42 printf("rac: %6.2f %6.2f %6.2f\n", tmp.racc[0], tmp.racc[1], tmp.racc[2]);
49 for(i=0; i<3; i++) _wind[i] = 0;
51 _integrator.setBody(&_body);
52 _integrator.setEnvironment(this);
54 // Default value of 30 Hz
55 _integrator.setInterval(1.0f/30.0f);
60 _ground_cb = new Ground();
64 _groundEffectSpan = 0;
66 for(i=0; i<3; i++) _wingCenter[i] = 0;
68 _global_ground[0] = 0; _global_ground[1] = 0; _global_ground[2] = 1;
69 _global_ground[3] = -100000;
75 // FIXME: who owns these things? Need a policy
79 for(int i=0; i<_hitches.size();i++)
80 delete (Hitch*)_hitches.get(i);
84 void Model::getThrust(float* out)
87 out[0] = out[1] = out[2] = 0;
89 for(i=0; i<_thrusters.size(); i++) {
90 Thruster* t = (Thruster*)_thrusters.get(i);
92 Math::add3(tmp, out, out);
96 void Model::initIteration()
98 // Precompute torque and angular momentum for the thrusters
101 _gyro[i] = _torque[i] = 0;
103 // Need a local altitude for the wind calculation
105 _s->planeGlobalToLocal(_global_ground, lground);
106 float alt = Math::abs(lground[3]);
108 for(i=0; i<_thrusters.size(); i++) {
109 Thruster* t = (Thruster*)_thrusters.get(i);
111 // Get the wind velocity at the thruster location
114 localWind(pos, _s, v, alt);
117 t->setAir(_pressure, _temp, _rho);
118 t->integrate(_integrator.getInterval());
121 Math::add3(v, _torque, _torque);
124 Math::add3(v, _gyro, _gyro);
127 // Displace the turbulence coordinates according to the local wind.
130 Math::mul3(_integrator.getInterval(), _wind, toff);
134 for(i=0; i<_hitches.size(); i++) {
135 Hitch* h = (Hitch*)_hitches.get(i);
136 h->integrate(_integrator.getInterval());
142 // This function initializes some variables for the rotor calculation
143 // Furthermore it integrates in "void Rotorpart::inititeration
144 // (float dt,float *rot)" the "rotor orientation" by omega*dt for the
145 // 3D-visualization of the heli only. and it compensates the rotation
146 // of the fuselage. The rotor does not follow the rotation of the fuselage.
147 // Therefore its rotation must be subtracted from the orientation of the
150 void Model::initRotorIteration()
152 float dt = _integrator.getInterval();
154 if (!_rotorgear.isInUse()) return;
155 Math::vmul33(_s->orient, _s->rot, lrot);
156 Math::mul3(dt,lrot,lrot);
157 _rotorgear.initRotorIteration(lrot,dt);
160 void Model::iterate()
163 initRotorIteration();
164 _body.recalc(); // FIXME: amortize this, somehow
165 _integrator.calcNewInterval();
168 bool Model::isCrashed()
173 void Model::setCrashed(bool crashed)
178 float Model::getAGL()
183 State* Model::getState()
188 void Model::setState(State* s)
190 _integrator.setState(s);
191 _s = _integrator.getState();
194 RigidBody* Model::getBody()
199 Integrator* Model::getIntegrator()
204 Surface* Model::getSurface(int handle)
206 return (Surface*)_surfaces.get(handle);
209 Rotorgear* Model::getRotorgear(void)
214 int Model::addThruster(Thruster* t)
216 return _thrusters.add(t);
219 Hook* Model::getHook(void)
224 Launchbar* Model::getLaunchbar(void)
229 int Model::numThrusters()
231 return _thrusters.size();
234 Thruster* Model::getThruster(int handle)
236 return (Thruster*)_thrusters.get(handle);
239 void Model::setThruster(int handle, Thruster* t)
241 _thrusters.set(handle, t);
244 int Model::addSurface(Surface* surf)
246 return _surfaces.add(surf);
249 int Model::addGear(Gear* gear)
251 return _gears.add(gear);
254 void Model::addHook(Hook* hook)
259 void Model::addLaunchbar(Launchbar* launchbar)
261 _launchbar = launchbar;
264 int Model::addHitch(Hitch* hitch)
266 return _hitches.add(hitch);
269 void Model::setGroundCallback(Ground* ground_cb)
272 _ground_cb = ground_cb;
275 Ground* Model::getGroundCallback(void)
280 void Model::setGroundEffect(float* pos, float span, float mul)
282 Math::set3(pos, _wingCenter);
283 _groundEffectSpan = span;
287 void Model::setAir(float pressure, float temp, float density)
289 _pressure = pressure;
294 void Model::setWind(float* wind)
296 Math::set3(wind, _wind);
299 void Model::updateGround(State* s)
302 _ground_cb->getGroundPlane(s->pos, _global_ground, dummy);
306 for(i=0; i<_gears.size(); i++) {
307 Gear* g = (Gear*)_gears.get(i);
309 // Get the point of ground contact
310 float pos[3], cmpr[3];
312 g->getCompression(cmpr);
314 Math::mul3(g->getCompressFraction(), cmpr, cmpr);
315 Math::add3(cmpr, pos, pos);
316 // Transform the local coordinates of the contact point to
317 // global coordinates.
319 s->posLocalToGlobal(pos, pt);
321 // Ask for the ground plane in the global coordinate system
322 double global_ground[4];
325 const SGMaterial* material;
326 _ground_cb->getGroundPlane(pt, global_ground, global_vel,
329 g->setGlobalGround(global_ground, global_vel, pt[0], pt[1],
333 for(i=0; i<_hitches.size(); i++) {
334 Hitch* h = (Hitch*)_hitches.get(i);
336 // Get the point of interest
340 // Transform the local coordinates of the contact point to
341 // global coordinates.
343 s->posLocalToGlobal(pos, pt);
345 // Ask for the ground plane in the global coordinate system
346 double global_ground[4];
348 _ground_cb->getGroundPlane(pt, global_ground, global_vel);
349 h->setGlobalGround(global_ground, global_vel);
352 for(i=0; i<_rotorgear.getRotors()->size(); i++) {
353 Rotor* r = (Rotor*)_rotorgear.getRotors()->get(i);
354 r->findGroundEffectAltitude(_ground_cb,s);
360 _hook->getTipGlobalPosition(s, pt);
361 double global_ground[4];
362 _ground_cb->getGroundPlane(pt, global_ground, dummy);
363 _hook->setGlobalGround(global_ground);
366 // The launchbar/holdback
369 _launchbar->getTipGlobalPosition(s, pt);
370 double global_ground[4];
371 _ground_cb->getGroundPlane(pt, global_ground, dummy);
372 _launchbar->setGlobalGround(global_ground);
376 void Model::calcForces(State* s)
378 // Add in the pre-computed stuff. These values aren't part of the
379 // Runge-Kutta integration (they don't depend on position or
380 // velocity), and are therefore constant across the four calls to
381 // calcForces. They get computed before we begin the integration
383 _body.setGyro(_gyro);
384 _body.addTorque(_torque);
386 for(i=0; i<_thrusters.size(); i++) {
387 Thruster* t = (Thruster*)_thrusters.get(i);
388 float thrust[3], pos[3];
389 t->getThrust(thrust);
391 _body.addForce(pos, thrust);
394 // Get a ground plane in local coordinates. The first three
395 // elements are the normal vector, the final one is the distance
396 // from the local origin along that vector to the ground plane
397 // (negative for objects "above" the ground)
399 s->planeGlobalToLocal(_global_ground, ground);
400 float alt = Math::abs(ground[3]);
402 // Gravity, convert to a force, then to local coordinates
404 Glue::geodUp(s->pos, grav);
405 Math::mul3(-9.8f * _body.getTotalMass(), grav, grav);
406 Math::vmul33(s->orient, grav, grav);
407 _body.addForce(grav);
409 // Do each surface, remembering that the local velocity at each
410 // point is different due to rotation.
412 faero[0] = faero[1] = faero[2] = 0;
413 for(i=0; i<_surfaces.size(); i++) {
414 Surface* sf = (Surface*)_surfaces.get(i);
416 // Vsurf = wind - velocity + (rot cross (cg - pos))
418 sf->getPosition(pos);
419 localWind(pos, s, vs, alt);
421 float force[3], torque[3];
422 sf->calcForce(vs, _rho, force, torque);
423 Math::add3(faero, force, faero);
425 _body.addForce(pos, force);
426 _body.addTorque(torque);
428 for (j=0; j<_rotorgear.getRotors()->size();j++)
430 Rotor* r = (Rotor *)_rotorgear.getRotors()->get(j);
433 localWind(pos, s, vs, alt);
434 r->calcLiftFactor(vs, _rho,s);
436 // total torque of rotor (scalar) for calculating new rotor rpm
438 for(i=0; i<r->_rotorparts.size(); i++) {
439 float torque_scalar=0;
440 Rotorpart* rp = (Rotorpart*)r->_rotorparts.get(i);
442 // Vsurf = wind - velocity + (rot cross (cg - pos))
444 rp->getPosition(pos);
445 localWind(pos, s, vs, alt,true);
447 float force[3], torque[3];
448 rp->calcForce(vs, _rho, force, torque, &torque_scalar);
450 rp->getPositionForceAttac(pos);
452 _body.addForce(pos, force);
453 _body.addTorque(torque);
457 if (_rotorgear.isInUse())
460 _rotorgear.calcForces(torque);
461 _body.addTorque(torque);
464 // Account for ground effect by multiplying the vertical force
465 // component by an amount linear with the fraction of the wingspan
467 if ((_groundEffectSpan != 0) && (_groundEffect != 0 ))
469 float dist = ground[3] - Math::dot3(ground, _wingCenter);
470 if(dist > 0 && dist < _groundEffectSpan) {
471 float fz = Math::dot3(faero, ground);
472 fz *= (_groundEffectSpan - dist) / _groundEffectSpan;
474 Math::mul3(fz, ground, faero);
475 _body.addForce(faero);
479 // Convert the velocity and rotation vectors to local coordinates
480 float lrot[3], lv[3];
481 Math::vmul33(s->orient, s->rot, lrot);
482 Math::vmul33(s->orient, s->v, lv);
485 for(i=0; i<_gears.size(); i++) {
486 float force[3], contact[3];
487 Gear* g = (Gear*)_gears.get(i);
489 g->calcForce(&_body, s, lv, lrot);
490 g->getForce(force, contact);
491 _body.addForce(contact, force);
496 _hook->calcForce(_ground_cb, &_body, s, lv, lrot);
497 float force[3], contact[3];
498 _hook->getForce(force, contact);
499 _body.addForce(contact, force);
502 // The launchbar/holdback
504 _launchbar->calcForce(_ground_cb, &_body, s, lv, lrot);
505 float forcelb[3], contactlb[3], forcehb[3], contacthb[3];
506 _launchbar->getForce(forcelb, contactlb, forcehb, contacthb);
507 _body.addForce(contactlb, forcelb);
508 _body.addForce(contacthb, forcehb);
512 for(i=0; i<_hitches.size(); i++) {
513 float force[3], contact[3];
514 Hitch* h = (Hitch*)_hitches.get(i);
515 h->calcForce(_ground_cb,&_body, s);
516 h->getForce(force, contact);
517 _body.addForce(contact, force);
520 void Model::newState(State* s)
524 // Some simple collision detection
527 for(i=0; i<_gears.size(); i++) {
528 Gear* g = (Gear*)_gears.get(i);
530 if (!g->getSubmergable())
532 // Get the point of ground contact
533 float pos[3], cmpr[3];
535 g->getCompression(cmpr);
536 Math::mul3(g->getCompressFraction(), cmpr, cmpr);
537 Math::add3(cmpr, pos, pos);
539 // The plane transformed to local coordinates.
540 double global_ground[4];
541 g->getGlobalGround(global_ground);
543 s->planeGlobalToLocal(global_ground, ground);
544 float dist = ground[3] - Math::dot3(pos, ground);
546 // Find the lowest one
552 if(_agl < -1) // Allow for some integration slop
556 // Calculates the airflow direction at the given point and for the
557 // specified aircraft velocity.
558 void Model::localWind(float* pos, State* s, float* out, float alt, bool is_rotor)
560 float tmp[3], lwind[3], lrot[3], lv[3];
562 // Get a global coordinate for our local position, and calculate
565 double gpos[3]; float up[3];
566 Math::tmul33(s->orient, pos, tmp);
567 for(int i=0; i<3; i++) {
568 gpos[i] = s->pos[i] + tmp[i];
570 Glue::geodUp(gpos, up);
571 _turb->getTurbulence(gpos, alt, up, lwind);
572 Math::add3(_wind, lwind, lwind);
574 Math::set3(_wind, lwind);
577 // Convert to local coordinates
578 Math::vmul33(s->orient, lwind, lwind);
579 Math::vmul33(s->orient, s->rot, lrot);
580 Math::vmul33(s->orient, s->v, lv);
582 _body.pointVelocity(pos, lrot, out); // rotational velocity
583 Math::mul3(-1, out, out); // (negated)
584 Math::add3(lwind, out, out); // + wind
585 Math::sub3(out, lv, out); // - velocity
587 //add the downwash of the rotors if it is not self a rotor
588 if (_rotorgear.isInUse()&&!is_rotor)
590 _rotorgear.getDownWash(pos,lv,tmp);
591 Math::add3(out,tmp, out); // + downwash
597 }; // namespace yasim