printf("\nNEW STATE (LOCAL COORDS)\n");
printf("pos: %10.2f %10.2f %10.2f\n", tmp.pos[0], tmp.pos[1], tmp.pos[2]);
printf("o: ");
- for(int i=0; i<3; i++) {
+ int i;
+ for(i=0; i<3; i++) {
if(i != 0) printf(" ");
printf("%6.2f %6.2f %6.2f\n",
tmp.orient[3*i+0], tmp.orient[3*i+1], tmp.orient[3*i+2]);
Model::Model()
{
- for(int i=0; i<3; i++) _wind[i] = 0;
+ int i;
+ for(i=0; i<3; i++) _wind[i] = 0;
_integrator.setBody(&_body);
_integrator.setEnvironment(this);
// Default value of 30 Hz
- _integrator.setInterval(1.0/30.0);
+ _integrator.setInterval(1.0f/30.0f);
+
+ _agl = 0;
+ _crashed = false;
}
Model::~Model()
{
float tmp[3];
out[0] = out[1] = out[2] = 0;
- for(int i=0; i<_thrusters.size(); i++) {
+ int i;
+ for(i=0; i<_thrusters.size(); i++) {
Thruster* t = (Thruster*)_thrusters.get(i);
t->getThrust(tmp);
Math::add3(tmp, out, out);
void Model::initIteration()
{
// Precompute torque and angular momentum for the thrusters
- for(int i=0; i<3; i++)
+ int i;
+ for(i=0; i<3; i++)
_gyro[i] = _torque[i] = 0;
- for(int i=0; i<_thrusters.size(); i++) {
+ for(i=0; i<_thrusters.size(); i++) {
Thruster* t = (Thruster*)_thrusters.get(i);
// Get the wind velocity at the thruster location
localWind(pos, _s, v);
t->setWind(v);
- t->setAir(_P, _T);
+ t->setAir(_pressure, _temp, _rho);
t->integrate(_integrator.getInterval());
t->getTorque(v);
_integrator.calcNewInterval();
}
+bool Model::isCrashed()
+{
+ return _crashed;
+}
+
+void Model::setCrashed(bool crashed)
+{
+ _crashed = crashed;
+}
+
+float Model::getAGL()
+{
+ return _agl;
+}
+
State* Model::getState()
{
return _s;
// (v dot _ground)-_ground[3] gives the distance AGL.
void Model::setGroundPlane(double* planeNormal, double fromOrigin)
{
- for(int i=0; i<3; i++) _ground[i] = planeNormal[i];
+ int i;
+ for(i=0; i<3; i++) _ground[i] = planeNormal[i];
_ground[3] = fromOrigin;
}
-void Model::setAir(float pressure, float temp)
+void Model::setAir(float pressure, float temp, float density)
{
- _P = pressure;
- _T = temp;
- _rho = Atmosphere::calcDensity(pressure, temp);
+ _pressure = pressure;
+ _temp = temp;
+ _rho = density;
}
void Model::setWind(float* wind)
// step.
_body.setGyro(_gyro);
_body.addTorque(_torque);
- for(int i=0; i<_thrusters.size(); i++) {
+ int i;
+ for(i=0; i<_thrusters.size(); i++) {
Thruster* t = (Thruster*)_thrusters.get(i);
float thrust[3], pos[3];
t->getThrust(thrust);
// Gravity, convert to a force, then to local coordinates
float grav[3];
Glue::geodUp(s->pos, grav);
- Math::mul3(-9.8 * _body.getTotalMass(), grav, grav);
+ Math::mul3(-9.8f * _body.getTotalMass(), grav, grav);
Math::vmul33(s->orient, grav, grav);
_body.addForce(grav);
// point is different due to rotation.
float faero[3];
faero[0] = faero[1] = faero[2] = 0;
- for(int i=0; i<_surfaces.size(); i++) {
+ for(i=0; i<_surfaces.size(); i++) {
Surface* sf = (Surface*)_surfaces.get(i);
// Vsurf = wind - velocity + (rot cross (cg - pos))
// Account for ground effect by multiplying the vertical force
// component by an amount linear with the fraction of the wingspan
// above the ground.
- float dist = ground[4] - Math::dot3(ground, _wingCenter);
+ float dist = ground[3] - Math::dot3(ground, _wingCenter);
if(dist > 0 && dist < _groundEffectSpan) {
float fz = Math::dot3(faero, ground);
- Math::mul3(fz * _groundEffect * dist/_groundEffectSpan,
- ground, faero);
+ fz *= (_groundEffectSpan - dist) / _groundEffectSpan;
+ fz *= _groundEffect;
+ Math::mul3(fz, ground, faero);
_body.addForce(faero);
}
Math::vmul33(s->orient, s->v, lv);
// The landing gear
- for(int i=0; i<_gears.size(); i++) {
+ for(i=0; i<_gears.size(); i++) {
float force[3], contact[3];
Gear* g = (Gear*)_gears.get(i);
g->calcForce(&_body, lv, lrot, ground);
//printState(s);
// Some simple collision detection
+ float min = 1e8;
float ground[4], pos[3], cmpr[3];
ground[3] = localGround(s, ground);
- for(int i=0; i<_gears.size(); i++) {
+ int i;
+ for(i=0; i<_gears.size(); i++) {
Gear* g = (Gear*)_gears.get(i);
+
+ // Get the point of ground contact
g->getPosition(pos);
g->getCompression(cmpr);
+ Math::mul3(g->getCompressFraction(), cmpr, cmpr);
Math::add3(cmpr, pos, pos);
float dist = ground[3] - Math::dot3(pos, ground);
- if(dist < 0) {
- printf("CRASH: gear %d\n", i);
- *(int*)0=0;
- }
+
+ // Find the lowest one
+ if(dist < min)
+ min = dist;
}
+ _agl = min;
+ if(_agl < -1) // Allow for some integration slop
+ _crashed = true;
}
// Returns a unit "down" vector for the ground in out, and the