]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/Gear.cpp
David Megginson writes:
[flightgear.git] / src / FDM / YASim / Gear.cpp
1 #include "Math.hpp"
2 #include "RigidBody.hpp"
3
4 #include "Gear.hpp"
5 namespace yasim {
6
7 Gear::Gear()
8 {
9     int i;
10     for(i=0; i<3; i++)
11         _pos[i] = _cmpr[i] = 0;
12     _spring = 1;
13     _damp = 0;
14     _sfric = 0.8;
15     _dfric = 0.7;
16     _brake = 0;
17     _rot = 0;
18     _extension = 1;
19 }
20
21 void Gear::setPosition(float* position)
22 {
23     int i;
24     for(i=0; i<3; i++) _pos[i] = position[i];
25 }
26
27 void Gear::setCompression(float* compression)
28 {
29     int i;
30     for(i=0; i<3; i++) _cmpr[i] = compression[i];
31 }
32
33 void Gear::setSpring(float spring)
34 {
35     _spring = spring;
36 }
37
38 void Gear::setDamping(float damping)
39 {
40     _damp = damping;
41 }
42
43 void Gear::setStaticFriction(float sfric)
44 {
45     _sfric = sfric;
46 }
47
48 void Gear::setDynamicFriction(float dfric)
49 {
50     _dfric = dfric;
51 }
52
53 void Gear::setBrake(float brake)
54 {
55     _brake = Math::clamp(brake, 0, 1);
56 }
57
58 void Gear::setRotation(float rotation)
59 {
60     _rot = rotation;
61 }
62
63 void Gear::setExtension(float extension)
64 {
65     _extension = Math::clamp(extension, 0, 1);
66 }
67
68 void Gear::getPosition(float* out)
69 {
70     int i;
71     for(i=0; i<3; i++) out[i] = _pos[i];
72 }
73
74 void Gear::getCompression(float* out)
75 {
76     int i;
77     for(i=0; i<3; i++) out[i] = _cmpr[i];    
78 }
79
80 float Gear::getSpring()
81 {
82     return _spring;
83 }
84
85 float Gear::getDamping()
86 {
87     return _damp;
88 }
89
90 float Gear::getStaticFriction()
91 {
92     return _sfric;
93 }
94
95 float Gear::getDynamicFriction()
96 {
97     return _dfric;
98 }
99
100 float Gear::getBrake()
101 {
102     return _brake;
103 }
104
105 float Gear::getRotation()
106 {
107     return _rot;
108 }
109
110 float Gear::getExtension()
111 {
112     return _extension;
113 }
114
115 void Gear::getForce(float* force, float* contact)
116 {
117     Math::set3(_force, force);
118     Math::set3(_contact, contact);
119 }
120
121 float Gear::getWoW()
122 {
123     return _wow;
124 }
125
126 float Gear::getCompressFraction()
127 {
128     return _frac;
129 }
130
131 void Gear::calcForce(RigidBody* body, float* v, float* rot, float* ground)
132 {
133     // Init the return values
134     int i;
135     for(i=0; i<3; i++) _force[i] = _contact[i] = 0;
136
137     // Don't bother if it's not down
138     if(_extension < 1)
139         return;
140
141     float tmp[3];
142
143     // First off, make sure that the gear "tip" is below the ground.
144     // If it's not, there's no force.
145     float a = ground[3] - Math::dot3(_pos, ground);
146     if(a > 0)
147         return;
148
149     // Now a is the distance from the tip to ground, so make b the
150     // distance from the base to ground.  We can get the fraction
151     // (0-1) of compression from a/(a-b). Note the minus sign -- stuff
152     // above ground is negative.
153     Math::add3(_cmpr, _pos, tmp);
154     float b = ground[3] - Math::dot3(tmp, ground);
155
156     // Calculate the point of ground _contact.
157     _frac = a/(a-b);
158     if(b < 0) _frac = 1;
159     for(i=0; i<3; i++)
160         _contact[i] = _pos[i] + _frac*_cmpr[i];
161
162     // Turn _cmpr into a unit vector and a magnitude
163     float cmpr[3];
164     float clen = Math::mag3(_cmpr);
165     Math::mul3(1/clen, _cmpr, cmpr);
166
167     // Now get the velocity of the point of contact
168     float cv[3];
169     body->pointVelocity(_contact, rot, cv);
170     Math::add3(cv, v, cv);
171
172     // Finally, we can start adding up the forces.  First the
173     // spring compression (note the clamping of _frac to 1):
174     _frac = (_frac > 1) ? 1 : _frac;
175     float fmag = _frac*clen*_spring;
176     Math::mul3(fmag, cmpr, _force);
177
178     // Then the damping.  Use the only the velocity into the ground
179     // (projection along "ground") projected along the compression
180     // axis.  So Vdamp = ground*(ground dot cv) dot cmpr
181     Math::mul3(Math::dot3(ground, cv), ground, tmp);
182     float dv = Math::dot3(cmpr, tmp);
183     float damp = _damp * dv;
184     if(damp > fmag) damp = fmag; // can't pull the plane down!
185     if(damp < -fmag) damp = -fmag; // sanity
186     Math::mul3(-damp, cmpr, tmp);
187     Math::add3(_force, tmp, _force);
188
189     _wow = fmag - damp;    
190
191     // Wheels are funky.  Split the velocity along the ground plane
192     // into rolling and skidding components.  Assuming small angles,
193     // we generate "forward" and "left" unit vectors (the compression
194     // goes "up") for the gear, make a "steer" direction from these,
195     // and then project it onto the ground plane.  Project the
196     // velocity onto the ground plane too, and extract the "steer"
197     // component.  The remainder is the skid velocity.
198
199     float gup[3]; // "up" unit vector from the ground
200     Math::set3(ground, gup);
201     Math::mul3(-1, gup, gup);
202
203     float xhat[] = {1,0,0};
204     float steer[3], skid[3];
205     Math::cross3(gup, xhat, skid);  // up cross xhat =~ skid
206     Math::unit3(skid, skid);        //               == skid
207
208     Math::cross3(skid, gup, steer); // skid cross up == steer
209
210     if(_rot != 0) {
211         // Correct for a (small) rotation
212         Math::mul3(_rot, steer, tmp);
213         Math::add3(tmp, skid, skid);
214         Math::unit3(skid, skid);
215         Math::cross3(skid, gup, steer);
216     }
217
218     float vsteer = Math::dot3(cv, steer);
219     float vskid  = Math::dot3(cv, skid);
220     float wgt = Math::dot3(_force, gup); // force into the ground
221
222     float fsteer = _brake * calcFriction(wgt, vsteer);
223     float fskid  = calcFriction(wgt, vskid);
224     if(vsteer > 0) fsteer = -fsteer;
225     if(vskid > 0) fskid = -fskid;
226
227     // Phoo!  All done.  Add it up and get out of here.
228     Math::mul3(fsteer, steer, tmp);
229     Math::add3(tmp, _force, _force);
230
231     Math::mul3(fskid, skid, tmp);
232     Math::add3(tmp, _force, _force);
233 }
234
235 float Gear::calcFriction(float wgt, float v)
236 {
237     // How slow is stopped?  50 cm/second?
238     const float STOP = 0.5;
239     const float iSTOP = 1/STOP;
240     v = Math::abs(v);
241     if(v < STOP) return v*iSTOP * wgt * _sfric;
242     else         return wgt * _dfric;
243 }
244
245 }; // namespace yasim
246