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"
14 #include "Rotorblade.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();
67 // FIXME: who owns these things? Need a policy
73 void Model::getThrust(float* out)
76 out[0] = out[1] = out[2] = 0;
78 for(i=0; i<_thrusters.size(); i++) {
79 Thruster* t = (Thruster*)_thrusters.get(i);
81 Math::add3(tmp, out, out);
85 void Model::initIteration()
87 // Precompute torque and angular momentum for the thrusters
90 _gyro[i] = _torque[i] = 0;
92 // Need a local altitude for the wind calculation
94 _s->planeGlobalToLocal(_global_ground, lground);
95 float alt = Math::abs(lground[3]);
97 for(i=0; i<_thrusters.size(); i++) {
98 Thruster* t = (Thruster*)_thrusters.get(i);
100 // Get the wind velocity at the thruster location
103 localWind(pos, _s, v, alt);
106 t->setAir(_pressure, _temp, _rho);
107 t->integrate(_integrator.getInterval());
110 Math::add3(v, _torque, _torque);
113 Math::add3(v, _gyro, _gyro);
116 // Displace the turbulence coordinates according to the local wind.
119 Math::mul3(_integrator.getInterval(), _wind, toff);
126 // FIXME: This method looks to me like it's doing *integration*, not
127 // initialization. Integration code should ideally go into
128 // calcForces. Only very "unstiff" problems can be solved well like
129 // this (see the engine code for an example); I don't know if rotor
130 // dynamics qualify or not.
132 void Model::initRotorIteration()
135 float dt = _integrator.getInterval();
137 Math::vmul33(_s->orient, _s->rot, lrot);
138 Math::mul3(dt,lrot,lrot);
139 for(i=0; i<_rotors.size(); i++) {
140 Rotor* r = (Rotor*)_rotors.get(i);
141 r->inititeration(dt);
143 for(i=0; i<_rotorparts.size(); i++) {
144 Rotorpart* rp = (Rotorpart*)_rotorparts.get(i);
145 rp->inititeration(dt,lrot);
147 for(i=0; i<_rotorblades.size(); i++) {
148 Rotorblade* rp = (Rotorblade*)_rotorblades.get(i);
149 rp->inititeration(dt,lrot);
153 void Model::iterate()
156 initRotorIteration();
157 _body.recalc(); // FIXME: amortize this, somehow
158 _integrator.calcNewInterval();
161 bool Model::isCrashed()
166 void Model::setCrashed(bool crashed)
171 float Model::getAGL()
176 State* Model::getState()
181 void Model::setState(State* s)
183 _integrator.setState(s);
184 _s = _integrator.getState();
187 RigidBody* Model::getBody()
192 Integrator* Model::getIntegrator()
197 Surface* Model::getSurface(int handle)
199 return (Surface*)_surfaces.get(handle);
202 Rotorpart* Model::getRotorpart(int handle)
204 return (Rotorpart*)_rotorparts.get(handle);
206 Rotorblade* Model::getRotorblade(int handle)
208 return (Rotorblade*)_rotorblades.get(handle);
210 Rotor* Model::getRotor(int handle)
212 return (Rotor*)_rotors.get(handle);
215 int Model::addThruster(Thruster* t)
217 return _thrusters.add(t);
220 Hook* Model::getHook(void)
225 Launchbar* Model::getLaunchbar(void)
230 int Model::numThrusters()
232 return _thrusters.size();
235 Thruster* Model::getThruster(int handle)
237 return (Thruster*)_thrusters.get(handle);
240 void Model::setThruster(int handle, Thruster* t)
242 _thrusters.set(handle, t);
245 int Model::addSurface(Surface* surf)
247 return _surfaces.add(surf);
250 int Model::addRotorpart(Rotorpart* rpart)
252 return _rotorparts.add(rpart);
254 int Model::addRotorblade(Rotorblade* rblade)
256 return _rotorblades.add(rblade);
258 int Model::addRotor(Rotor* r)
260 return _rotors.add(r);
263 int Model::addGear(Gear* gear)
265 return _gears.add(gear);
268 void Model::addHook(Hook* hook)
273 void Model::addLaunchbar(Launchbar* launchbar)
275 _launchbar = launchbar;
278 void Model::setGroundCallback(Ground* ground_cb)
281 _ground_cb = ground_cb;
284 Ground* Model::getGroundCallback(void)
289 void Model::setGroundEffect(float* pos, float span, float mul)
291 Math::set3(pos, _wingCenter);
292 _groundEffectSpan = span;
296 void Model::setAir(float pressure, float temp, float density)
298 _pressure = pressure;
303 void Model::setWind(float* wind)
305 Math::set3(wind, _wind);
308 void Model::updateGround(State* s)
311 _ground_cb->getGroundPlane(s->pos, _global_ground, dummy);
315 for(i=0; i<_gears.size(); i++) {
316 Gear* g = (Gear*)_gears.get(i);
318 // Get the point of ground contact
319 float pos[3], cmpr[3];
321 g->getCompression(cmpr);
323 Math::mul3(g->getCompressFraction(), cmpr, cmpr);
324 Math::add3(cmpr, pos, pos);
325 // Transform the local coordinates of the contact point to
326 // global coordinates.
328 s->posLocalToGlobal(pos, pt);
330 // Ask for the ground plane in the global coordinate system
331 double global_ground[4];
333 _ground_cb->getGroundPlane(pt, global_ground, global_vel);
334 g->setGlobalGround(global_ground, global_vel);
340 _hook->getTipGlobalPosition(s, pt);
341 double global_ground[4];
342 _ground_cb->getGroundPlane(pt, global_ground, dummy);
343 _hook->setGlobalGround(global_ground);
346 // The launchbar/holdback
349 _launchbar->getTipGlobalPosition(s, pt);
350 double global_ground[4];
351 _ground_cb->getGroundPlane(pt, global_ground, dummy);
352 _launchbar->setGlobalGround(global_ground);
356 void Model::calcForces(State* s)
358 // Add in the pre-computed stuff. These values aren't part of the
359 // Runge-Kutta integration (they don't depend on position or
360 // velocity), and are therefore constant across the four calls to
361 // calcForces. They get computed before we begin the integration
363 _body.setGyro(_gyro);
364 _body.addTorque(_torque);
366 for(i=0; i<_thrusters.size(); i++) {
367 Thruster* t = (Thruster*)_thrusters.get(i);
368 float thrust[3], pos[3];
369 t->getThrust(thrust);
371 _body.addForce(pos, thrust);
374 // Get a ground plane in local coordinates. The first three
375 // elements are the normal vector, the final one is the distance
376 // from the local origin along that vector to the ground plane
377 // (negative for objects "above" the ground)
379 s->planeGlobalToLocal(_global_ground, ground);
380 float alt = Math::abs(ground[3]);
382 // Gravity, convert to a force, then to local coordinates
384 Glue::geodUp(s->pos, grav);
385 Math::mul3(-9.8f * _body.getTotalMass(), grav, grav);
386 Math::vmul33(s->orient, grav, grav);
387 _body.addForce(grav);
389 // Do each surface, remembering that the local velocity at each
390 // point is different due to rotation.
392 faero[0] = faero[1] = faero[2] = 0;
393 for(i=0; i<_surfaces.size(); i++) {
394 Surface* sf = (Surface*)_surfaces.get(i);
396 // Vsurf = wind - velocity + (rot cross (cg - pos))
398 sf->getPosition(pos);
399 localWind(pos, s, vs, alt);
401 float force[3], torque[3];
402 sf->calcForce(vs, _rho, force, torque);
403 Math::add3(faero, force, faero);
405 _body.addForce(pos, force);
406 _body.addTorque(torque);
408 for(i=0; i<_rotorparts.size(); i++) {
409 Rotorpart* sf = (Rotorpart*)_rotorparts.get(i);
411 // Vsurf = wind - velocity + (rot cross (cg - pos))
413 sf->getPosition(pos);
414 localWind(pos, s, vs, alt);
416 float force[3], torque[3];
417 sf->calcForce(vs, _rho, force, torque);
418 //Math::add3(faero, force, faero);
420 sf->getPositionForceAttac(pos);
422 _body.addForce(pos, force);
423 _body.addTorque(torque);
425 for(i=0; i<_rotorblades.size(); i++) {
426 Rotorblade* sf = (Rotorblade*)_rotorblades.get(i);
428 // Vsurf = wind - velocity + (rot cross (cg - pos))
430 sf->getPosition(pos);
431 localWind(pos, s, vs, alt);
433 float force[3], torque[3];
434 sf->calcForce(vs, _rho, force, torque);
435 //Math::add3(faero, force, faero);
437 sf->getPositionForceAttac(pos);
439 _body.addForce(pos, force);
440 _body.addTorque(torque);
443 // Account for ground effect by multiplying the vertical force
444 // component by an amount linear with the fraction of the wingspan
446 float dist = ground[3] - Math::dot3(ground, _wingCenter);
447 if(dist > 0 && dist < _groundEffectSpan) {
448 float fz = Math::dot3(faero, ground);
449 fz *= (_groundEffectSpan - dist) / _groundEffectSpan;
451 Math::mul3(fz, ground, faero);
452 _body.addForce(faero);
455 // Convert the velocity and rotation vectors to local coordinates
456 float lrot[3], lv[3];
457 Math::vmul33(s->orient, s->rot, lrot);
458 Math::vmul33(s->orient, s->v, lv);
461 for(i=0; i<_gears.size(); i++) {
462 float force[3], contact[3];
463 Gear* g = (Gear*)_gears.get(i);
465 g->calcForce(&_body, s, lv, lrot);
466 g->getForce(force, contact);
467 _body.addForce(contact, force);
472 float v[3], rot[3], glvel[3], ground[3];
473 _hook->calcForce(_ground_cb, &_body, s, lv, lrot);
474 float force[3], contact[3];
475 _hook->getForce(force, contact);
476 _body.addForce(contact, force);
479 // The launchbar/holdback
481 float v[3], rot[3], glvel[3], ground[3];
482 _launchbar->calcForce(_ground_cb, &_body, s, lv, lrot);
483 float force[3], contact[3];
484 _launchbar->getForce(force, contact);
485 _body.addForce(contact, force);
489 void Model::newState(State* s)
493 // Some simple collision detection
496 for(i=0; i<_gears.size(); i++) {
497 Gear* g = (Gear*)_gears.get(i);
499 // Get the point of ground contact
500 float pos[3], cmpr[3];
502 g->getCompression(cmpr);
503 Math::mul3(g->getCompressFraction(), cmpr, cmpr);
504 Math::add3(cmpr, pos, pos);
506 // The plane transformed to local coordinates.
507 double global_ground[4];
508 g->getGlobalGround(global_ground);
510 s->planeGlobalToLocal(global_ground, ground);
511 float dist = ground[3] - Math::dot3(pos, ground);
513 // Find the lowest one
518 if(_agl < -1) // Allow for some integration slop
522 // Calculates the airflow direction at the given point and for the
523 // specified aircraft velocity.
524 void Model::localWind(float* pos, State* s, float* out, float alt)
526 float tmp[3], lwind[3], lrot[3], lv[3];
528 // Get a global coordinate for our local position, and calculate
531 double gpos[3]; float up[3];
532 Math::tmul33(s->orient, pos, tmp);
533 for(int i=0; i<3; i++) {
534 gpos[i] = s->pos[i] + tmp[i];
536 Glue::geodUp(gpos, up);
537 _turb->getTurbulence(gpos, alt, up, lwind);
538 Math::add3(_wind, lwind, lwind);
540 Math::set3(_wind, lwind);
543 // Convert to local coordinates
544 Math::vmul33(s->orient, lwind, lwind);
545 Math::vmul33(s->orient, s->rot, lrot);
546 Math::vmul33(s->orient, s->v, lv);
548 _body.pointVelocity(pos, lrot, out); // rotational velocity
549 Math::mul3(-1, out, out); // (negated)
550 Math::add3(lwind, out, out); // + wind
551 Math::sub3(out, lv, out); // - velocity
554 }; // namespace yasim