]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/Gear.cpp
Vivian reported that the caster angle of the Hunter was experiencing
[flightgear.git] / src / FDM / YASim / Gear.cpp
1 #include "Math.hpp"
2 #include "BodyEnvironment.hpp"
3 #include "RigidBody.hpp"
4
5 #include "Gear.hpp"
6 namespace yasim {
7
8 Gear::Gear()
9 {
10     int i;
11     for(i=0; i<3; i++)
12         _pos[i] = _cmpr[i] = 0;
13     _spring = 1;
14     _damp = 0;
15     _sfric = 0.8f;
16     _dfric = 0.7f;
17     _brake = 0;
18     _rot = 0;
19     _extension = 1;
20     _castering = false;
21     _frac = 0;
22
23     for(i=0; i<3; i++)
24         _global_ground[i] = _global_vel[i] = 0;
25     _global_ground[2] = 1;
26     _global_ground[3] = -1e3;
27 }
28
29 void Gear::setPosition(float* position)
30 {
31     int i;
32     for(i=0; i<3; i++) _pos[i] = position[i];
33 }
34
35 void Gear::setCompression(float* compression)
36 {
37     int i;
38     for(i=0; i<3; i++) _cmpr[i] = compression[i];
39 }
40
41 void Gear::setSpring(float spring)
42 {
43     _spring = spring;
44 }
45
46 void Gear::setDamping(float damping)
47 {
48     _damp = damping;
49 }
50
51 void Gear::setStaticFriction(float sfric)
52 {
53     _sfric = sfric;
54 }
55
56 void Gear::setDynamicFriction(float dfric)
57 {
58     _dfric = dfric;
59 }
60
61 void Gear::setBrake(float brake)
62 {
63     _brake = Math::clamp(brake, 0, 1);
64 }
65
66 void Gear::setRotation(float rotation)
67 {
68     _rot = rotation;
69 }
70
71 void Gear::setExtension(float extension)
72 {
73     _extension = Math::clamp(extension, 0, 1);
74 }
75
76 void Gear::setCastering(bool c)
77 {
78     _castering = c;
79 }
80
81 void Gear::setGlobalGround(double *global_ground, float* global_vel)
82 {
83     int i;
84     for(i=0; i<4; i++) _global_ground[i] = global_ground[i];
85     for(i=0; i<3; i++) _global_vel[i] = global_vel[i];
86 }
87
88 void Gear::getPosition(float* out)
89 {
90     int i;
91     for(i=0; i<3; i++) out[i] = _pos[i];
92 }
93
94 void Gear::getCompression(float* out)
95 {
96     int i;
97     for(i=0; i<3; i++) out[i] = _cmpr[i];    
98 }
99
100 void Gear::getGlobalGround(double* global_ground)
101 {
102     int i;
103     for(i=0; i<4; i++) global_ground[i] = _global_ground[i];
104 }
105
106 float Gear::getSpring()
107 {
108     return _spring;
109 }
110
111 float Gear::getDamping()
112 {
113     return _damp;
114 }
115
116 float Gear::getStaticFriction()
117 {
118     return _sfric;
119 }
120
121 float Gear::getDynamicFriction()
122 {
123     return _dfric;
124 }
125
126 float Gear::getBrake()
127 {
128     return _brake;
129 }
130
131 float Gear::getRotation()
132 {
133     return _rot;
134 }
135
136 float Gear::getExtension()
137 {
138     return _extension;
139 }
140
141 void Gear::getForce(float* force, float* contact)
142 {
143     Math::set3(_force, force);
144     Math::set3(_contact, contact);
145 }
146
147 float Gear::getWoW()
148 {
149     return _wow;
150 }
151
152 float Gear::getCompressFraction()
153 {
154     return _frac;
155 }
156
157 bool Gear::getCastering()
158 {
159     return _castering;
160 }
161
162 void Gear::calcForce(RigidBody* body, State *s, float* v, float* rot)
163 {
164     // Init the return values
165     int i;
166     for(i=0; i<3; i++) _force[i] = _contact[i] = 0;
167
168     // Don't bother if it's not down
169     if(_extension < 1)
170         return;
171
172     // The ground plane transformed to the local frame.
173     float ground[4];
174     s->planeGlobalToLocal(_global_ground, ground);
175         
176     // The velocity of the contact patch transformed to local coordinates.
177     float glvel[3];
178     s->velGlobalToLocal(_global_vel, glvel);
179
180     // First off, make sure that the gear "tip" is below the ground.
181     // If it's not, there's no force.
182     float a = ground[3] - Math::dot3(_pos, ground);
183     if(a > 0) {
184         _wow = 0;
185         _frac = 0;
186         _rollSpeed = 0;
187         _casterAngle = 0;
188         return;
189     }
190
191     // Now a is the distance from the tip to ground, so make b the
192     // distance from the base to ground.  We can get the fraction
193     // (0-1) of compression from a/(a-b). Note the minus sign -- stuff
194     // above ground is negative.
195     float tmp[3];
196     Math::add3(_cmpr, _pos, tmp);
197     float b = ground[3] - Math::dot3(tmp, ground);
198
199     // Calculate the point of ground _contact.
200     _frac = a/(a-b);
201     if(b < 0) _frac = 1;
202     for(i=0; i<3; i++)
203         _contact[i] = _pos[i] + _frac*_cmpr[i];
204
205     // Turn _cmpr into a unit vector and a magnitude
206     float cmpr[3];
207     float clen = Math::mag3(_cmpr);
208     Math::mul3(1/clen, _cmpr, cmpr);
209
210     // Now get the velocity of the point of contact
211     float cv[3];
212     body->pointVelocity(_contact, rot, cv);
213     Math::add3(cv, v, cv);
214     Math::sub3(cv, glvel, cv);
215
216     // Finally, we can start adding up the forces.  First the spring
217     // compression.   (note the clamping of _frac to 1):
218     _frac = (_frac > 1) ? 1 : _frac;
219     float fmag = _frac*clen*_spring;
220
221     // Then the damping.  Use the only the velocity into the ground
222     // (projection along "ground") projected along the compression
223     // axis.  So Vdamp = ground*(ground dot cv) dot cmpr
224     Math::mul3(Math::dot3(ground, cv), ground, tmp);
225     float dv = Math::dot3(cmpr, tmp);
226     float damp = _damp * dv;
227     if(damp > fmag) damp = fmag; // can't pull the plane down!
228     if(damp < -fmag) damp = -fmag; // sanity
229
230     // The actual force applied is only the component perpendicular to
231     // the ground.  Side forces come from velocity only.
232     _wow = (fmag - damp) * -Math::dot3(cmpr, ground);
233     Math::mul3(-_wow, ground, _force);
234
235     // Wheels are funky.  Split the velocity along the ground plane
236     // into rolling and skidding components.  Assuming small angles,
237     // we generate "forward" and "left" unit vectors (the compression
238     // goes "up") for the gear, make a "steer" direction from these,
239     // and then project it onto the ground plane.  Project the
240     // velocity onto the ground plane too, and extract the "steer"
241     // component.  The remainder is the skid velocity.
242
243     float gup[3]; // "up" unit vector from the ground
244     Math::set3(ground, gup);
245     Math::mul3(-1, gup, gup);
246
247     float xhat[] = {1,0,0};
248     float steer[3], skid[3];
249     Math::cross3(gup, xhat, skid);  // up cross xhat =~ skid
250     Math::unit3(skid, skid);        //               == skid
251
252     Math::cross3(skid, gup, steer); // skid cross up == steer
253
254     if(_rot != 0) {
255         // Correct for a rotation
256         float srot = Math::sin(_rot);
257         float crot = Math::cos(_rot);
258         float tx = steer[0];
259         float ty = steer[1];
260         steer[0] =  crot*tx + srot*ty;
261         steer[1] = -srot*tx + crot*ty;
262
263         tx = skid[0];
264         ty = skid[1];
265         skid[0] =  crot*tx + srot*ty;
266         skid[1] = -srot*tx + crot*ty;
267     }
268
269     float vsteer = Math::dot3(cv, steer);
270     float vskid  = Math::dot3(cv, skid);
271     float wgt = Math::dot3(_force, gup); // force into the ground
272
273     if(_castering) {
274         _rollSpeed = Math::sqrt(vsteer*vsteer + vskid*vskid);
275         // Don't modify caster angle when the wheel isn't moving,
276         // or else the angle will animate the "jitter" of a stopped
277         // gear.
278         if(_rollSpeed > 0.05)
279             _casterAngle = Math::atan2(vskid, vsteer);
280         return;
281     } else {
282         _rollSpeed = vsteer;
283         _casterAngle = _rot;
284     }
285
286     float fsteer = _brake * calcFriction(wgt, vsteer);
287     float fskid  = calcFriction(wgt, vskid);
288     if(vsteer > 0) fsteer = -fsteer;
289     if(vskid > 0) fskid = -fskid;
290
291     // Phoo!  All done.  Add it up and get out of here.
292     Math::mul3(fsteer, steer, tmp);
293     Math::add3(tmp, _force, _force);
294
295     Math::mul3(fskid, skid, tmp);
296     Math::add3(tmp, _force, _force);
297 }
298
299 float Gear::calcFriction(float wgt, float v)
300 {
301     // How slow is stopped?  10 cm/second?
302     const float STOP = 0.1f;
303     const float iSTOP = 1.0f/STOP;
304     v = Math::abs(v);
305     if(v < STOP) return v*iSTOP * wgt * _sfric;
306     else         return wgt * _dfric;
307 }
308
309 }; // namespace yasim
310