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];
324 const SGMaterial* material;
325 _ground_cb->getGroundPlane(pt, global_ground, global_vel, &material);
326 g->setGlobalGround(global_ground, global_vel, pt[0], pt[1], material);
329 for(i=0; i<_hitches.size(); i++) {
330 Hitch* h = (Hitch*)_hitches.get(i);
332 // Get the point of interest
336 // Transform the local coordinates of the contact point to
337 // global coordinates.
339 s->posLocalToGlobal(pos, pt);
341 // Ask for the ground plane in the global coordinate system
342 double global_ground[4];
344 _ground_cb->getGroundPlane(pt, global_ground, global_vel);
345 h->setGlobalGround(global_ground, global_vel);
348 for(i=0; i<_rotorgear.getRotors()->size(); i++) {
349 Rotor* r = (Rotor*)_rotorgear.getRotors()->get(i);
350 r->findGroundEffectAltitude(_ground_cb,s);
356 _hook->getTipGlobalPosition(s, pt);
357 double global_ground[4];
358 _ground_cb->getGroundPlane(pt, global_ground, dummy);
359 _hook->setGlobalGround(global_ground);
362 // The launchbar/holdback
365 _launchbar->getTipGlobalPosition(s, pt);
366 double global_ground[4];
367 _ground_cb->getGroundPlane(pt, global_ground, dummy);
368 _launchbar->setGlobalGround(global_ground);
372 void Model::calcForces(State* s)
374 // Add in the pre-computed stuff. These values aren't part of the
375 // Runge-Kutta integration (they don't depend on position or
376 // velocity), and are therefore constant across the four calls to
377 // calcForces. They get computed before we begin the integration
379 _body.setGyro(_gyro);
380 _body.addTorque(_torque);
382 for(i=0; i<_thrusters.size(); i++) {
383 Thruster* t = (Thruster*)_thrusters.get(i);
384 float thrust[3], pos[3];
385 t->getThrust(thrust);
387 _body.addForce(pos, thrust);
390 // Get a ground plane in local coordinates. The first three
391 // elements are the normal vector, the final one is the distance
392 // from the local origin along that vector to the ground plane
393 // (negative for objects "above" the ground)
395 s->planeGlobalToLocal(_global_ground, ground);
396 float alt = Math::abs(ground[3]);
398 // Gravity, convert to a force, then to local coordinates
400 Glue::geodUp(s->pos, grav);
401 Math::mul3(-9.8f * _body.getTotalMass(), grav, grav);
402 Math::vmul33(s->orient, grav, grav);
403 _body.addForce(grav);
405 // Do each surface, remembering that the local velocity at each
406 // point is different due to rotation.
408 faero[0] = faero[1] = faero[2] = 0;
409 for(i=0; i<_surfaces.size(); i++) {
410 Surface* sf = (Surface*)_surfaces.get(i);
412 // Vsurf = wind - velocity + (rot cross (cg - pos))
414 sf->getPosition(pos);
415 localWind(pos, s, vs, alt);
417 float force[3], torque[3];
418 sf->calcForce(vs, _rho, force, torque);
419 Math::add3(faero, force, faero);
421 _body.addForce(pos, force);
422 _body.addTorque(torque);
424 for (j=0; j<_rotorgear.getRotors()->size();j++)
426 Rotor* r = (Rotor *)_rotorgear.getRotors()->get(j);
429 localWind(pos, s, vs, alt);
430 r->calcLiftFactor(vs, _rho,s);
432 // total torque of rotor (scalar) for calculating new rotor rpm
434 for(i=0; i<r->_rotorparts.size(); i++) {
435 float torque_scalar=0;
436 Rotorpart* rp = (Rotorpart*)r->_rotorparts.get(i);
438 // Vsurf = wind - velocity + (rot cross (cg - pos))
440 rp->getPosition(pos);
441 localWind(pos, s, vs, alt,true);
443 float force[3], torque[3];
444 rp->calcForce(vs, _rho, force, torque, &torque_scalar);
446 rp->getPositionForceAttac(pos);
448 _body.addForce(pos, force);
449 _body.addTorque(torque);
453 if (_rotorgear.isInUse())
456 _rotorgear.calcForces(torque);
457 _body.addTorque(torque);
460 // Account for ground effect by multiplying the vertical force
461 // component by an amount linear with the fraction of the wingspan
463 if ((_groundEffectSpan != 0) && (_groundEffect != 0 ))
465 float dist = ground[3] - Math::dot3(ground, _wingCenter);
466 if(dist > 0 && dist < _groundEffectSpan) {
467 float fz = Math::dot3(faero, ground);
468 fz *= (_groundEffectSpan - dist) / _groundEffectSpan;
470 Math::mul3(fz, ground, faero);
471 _body.addForce(faero);
475 // Convert the velocity and rotation vectors to local coordinates
476 float lrot[3], lv[3];
477 Math::vmul33(s->orient, s->rot, lrot);
478 Math::vmul33(s->orient, s->v, lv);
481 for(i=0; i<_gears.size(); i++) {
482 float force[3], contact[3];
483 Gear* g = (Gear*)_gears.get(i);
485 g->calcForce(&_body, s, lv, lrot);
486 g->getForce(force, contact);
487 _body.addForce(contact, force);
492 _hook->calcForce(_ground_cb, &_body, s, lv, lrot);
493 float force[3], contact[3];
494 _hook->getForce(force, contact);
495 _body.addForce(contact, force);
498 // The launchbar/holdback
500 _launchbar->calcForce(_ground_cb, &_body, s, lv, lrot);
501 float forcelb[3], contactlb[3], forcehb[3], contacthb[3];
502 _launchbar->getForce(forcelb, contactlb, forcehb, contacthb);
503 _body.addForce(contactlb, forcelb);
504 _body.addForce(contacthb, forcehb);
508 for(i=0; i<_hitches.size(); i++) {
509 float force[3], contact[3];
510 Hitch* h = (Hitch*)_hitches.get(i);
511 h->calcForce(_ground_cb,&_body, s);
512 h->getForce(force, contact);
513 _body.addForce(contact, force);
516 void Model::newState(State* s)
520 // Some simple collision detection
523 for(i=0; i<_gears.size(); i++) {
524 Gear* g = (Gear*)_gears.get(i);
526 if (!g->getSubmergable())
528 // Get the point of ground contact
529 float pos[3], cmpr[3];
531 g->getCompression(cmpr);
532 Math::mul3(g->getCompressFraction(), cmpr, cmpr);
533 Math::add3(cmpr, pos, pos);
535 // The plane transformed to local coordinates.
536 double global_ground[4];
537 g->getGlobalGround(global_ground);
539 s->planeGlobalToLocal(global_ground, ground);
540 float dist = ground[3] - Math::dot3(pos, ground);
542 // Find the lowest one
548 if(_agl < -1) // Allow for some integration slop
552 // Calculates the airflow direction at the given point and for the
553 // specified aircraft velocity.
554 void Model::localWind(float* pos, State* s, float* out, float alt, bool is_rotor)
556 float tmp[3], lwind[3], lrot[3], lv[3];
558 // Get a global coordinate for our local position, and calculate
561 double gpos[3]; float up[3];
562 Math::tmul33(s->orient, pos, tmp);
563 for(int i=0; i<3; i++) {
564 gpos[i] = s->pos[i] + tmp[i];
566 Glue::geodUp(gpos, up);
567 _turb->getTurbulence(gpos, alt, up, lwind);
568 Math::add3(_wind, lwind, lwind);
570 Math::set3(_wind, lwind);
573 // Convert to local coordinates
574 Math::vmul33(s->orient, lwind, lwind);
575 Math::vmul33(s->orient, s->rot, lrot);
576 Math::vmul33(s->orient, s->v, lv);
578 _body.pointVelocity(pos, lrot, out); // rotational velocity
579 Math::mul3(-1, out, out); // (negated)
580 Math::add3(lwind, out, out); // + wind
581 Math::sub3(out, lv, out); // - velocity
583 //add the downwash of the rotors if it is not self a rotor
584 if (_rotorgear.isInUse()&&!is_rotor)
586 _rotorgear.getDownWash(pos,lv,tmp);
587 Math::add3(out,tmp, out); // + downwash
593 }; // namespace yasim