2#include "Integrator.hpp"
9void Integrator::extrapolatePosition(
double* pos,
float* v,
float dt,
18 l2gVector(o1, cg, cg);
20 Math::mul3(dt, tmp, tmp);
21 Math::add3(cg, tmp, tmp);
23 l2gVector(o2, cg, cg);
24 Math::sub3(tmp, cg, tmp);
33void Integrator::calcNewInterval()
40 orthonormalize(_s.orient);
46 _body->getAccel(s.acc);
47 l2gVector(_s.orient, s.acc, s.acc);
49 _body->getAngularAccel(s.racc);
50 l2gVector(_s.orient, s.racc, s.racc);
53 rotMatrix(s.rot, dt, rotmat);
54 Math::mmul33(_s.orient, rotmat, s.orient);
56 extrapolatePosition(s.pos, s.v, dt, _s.orient, s.orient);
58 Math::mul3(dt, s.acc, tmp);
59 Math::add3(tmp, s.v, s.v);
61 Math::mul3(dt, s.racc, tmp);
62 Math::add3(tmp, s.rot, s.rot);
71void Integrator::calcNewInterval()
75 const static int NITER=4;
76 static float TIMESTEP[] = { 1.0, 0.5, 0.5, 1.0 };
77 static float WEIGHTS[] = { 6.0, 3.0, 3.0, 6.0 };
80 double pos[NITER][3];
float vel[NITER][3];
float acc[NITER][3];
81 float ori[NITER][9];
float rot[NITER][3];
float rac[NITER][3];
83 float *currVel = _s.v;
84 float *currAcc = _s.acc;
85 float *currRot = _s.rot;
86 float *currRac = _s.racc;
89 orthonormalize(_s.orient);
92 for(
i=0;
i<NITER;
i++) {
98 float dt = _dt * TIMESTEP[
i];
103 rotMatrix(currRot, dt, rotmat);
104 Math::mmul33(_s.orient, rotmat, ori[
i]);
108 for(j=0; j<3; j++) pos[
i][j] = _s.pos[j];
109 extrapolatePosition(pos[
i], currVel, dt, _s.orient, ori[
i]);
112 Math::set3(currAcc, tmp);
113 Math::mul3(dt, tmp, tmp);
114 Math::add3(_s.v, tmp, vel[
i]);
117 Math::set3(currRac, tmp);
118 Math::mul3(dt, tmp, tmp);
119 Math::add3(_s.rot, tmp, rot[
i]);
133 stmp.pos[j] = pos[
i][j];
134 stmp.v[j] = vel[
i][j];
135 stmp.rot[j] = rot[
i][j];
138 stmp.orient[j] = ori[
i][j];
140 _env->calcForces(&stmp);
142 _body->getAccel(acc[
i]);
143 _body->getAngularAccel(rac[
i]);
144 l2gVector(_s.orient, acc[
i], acc[
i]);
145 l2gVector(_s.orient, rac[
i], rac[
i]);
150 currVel = vel[
i]; currAcc = acc[
i];
151 currRot = rot[
i]; currRac = rac[
i];
160 for(
i=0;
i<NITER;
i++) {
161 float wgt = WEIGHTS[
i];
165 derivs.v[j] += wgt*vel[
i][j]; derivs.rot[j] += wgt*rot[
i][j];
166 derivs.acc[j] += wgt*acc[
i][j]; derivs.racc[j] += wgt*rac[
i][j];
171 derivs.v[
i] *= itot; derivs.rot[
i] *= itot;
172 derivs.acc[
i] *= itot; derivs.racc[
i] *= itot;
182 for(
i=0;
i<9;
i++) orient0[
i] = _s.orient[
i];
185 rotMatrix(derivs.rot, _dt, rotmat);
186 Math::mmul33(orient0, rotmat, _s.orient);
188 extrapolatePosition(_s.pos, derivs.v, _dt, orient0, _s.orient);
191 Math::mul3(_dt, derivs.acc, tmp);
192 Math::add3(_s.v, tmp, _s.v);
194 Math::mul3(_dt, derivs.racc, tmp);
195 Math::add3(_s.rot, tmp, _s.rot);
198 _s.acc[
i] = derivs.acc[
i];
199 _s.racc[
i] = derivs.racc[
i];
215void Integrator::rotMatrix(
float* r,
float dt,
float* out)
219 float mag = Math::mag3(r);
220 float angle = dt*mag;
229 out[0] = 1; out[1] = 0; out[2] = 0;
230 out[3] = 0; out[4] = 1; out[5] = 0;
231 out[6] = 0; out[7] = 0; out[8] = 1;
236 Math::mul3(1/mag, r, runit);
238 float s = Math::sin(angle);
239 float c = Math::cos(angle);
241 float c1rx = c1*runit[0];
242 float c1ry = c1*runit[1];
243 float xx = c1rx*runit[0];
244 float xy = c1rx*runit[1];
245 float xz = c1rx*runit[2];
246 float yy = c1ry*runit[1];
247 float yz = c1ry*runit[2];
248 float zz = c1*runit[2]*runit[2];
249 float xs = runit[0]*s;
250 float ys = runit[1]*s;
251 float zs = runit[2]*s;
253 out[0] = xx+c ; out[3] = xy-zs; out[6] = xz+ys;
254 out[1] = xy+zs; out[4] = yy+c ; out[7] = yz-xs;
255 out[2] = xz-ys; out[5] = yz+xs; out[8] = zz+c ;
263void Integrator::orthonormalize(
float* m)
267 float *x = m, *y = m+3, *z = m+6;
272 Math::mul3(Math::dot3(x, z), z, v);
276 Math::cross3(z, x, y);
State
Models an aircraft axis for purposes of trimming.