2 #include "RigidBody.hpp"
11 _pos[i] = _cmpr[i] = 0;
22 void Gear::setPosition(float* position)
25 for(i=0; i<3; i++) _pos[i] = position[i];
28 void Gear::setCompression(float* compression)
31 for(i=0; i<3; i++) _cmpr[i] = compression[i];
34 void Gear::setSpring(float spring)
39 void Gear::setDamping(float damping)
44 void Gear::setStaticFriction(float sfric)
49 void Gear::setDynamicFriction(float dfric)
54 void Gear::setBrake(float brake)
56 _brake = Math::clamp(brake, 0, 1);
59 void Gear::setRotation(float rotation)
64 void Gear::setExtension(float extension)
66 _extension = Math::clamp(extension, 0, 1);
69 void Gear::setCastering(bool c)
74 void Gear::getPosition(float* out)
77 for(i=0; i<3; i++) out[i] = _pos[i];
80 void Gear::getCompression(float* out)
83 for(i=0; i<3; i++) out[i] = _cmpr[i];
86 float Gear::getSpring()
91 float Gear::getDamping()
96 float Gear::getStaticFriction()
101 float Gear::getDynamicFriction()
106 float Gear::getBrake()
111 float Gear::getRotation()
116 float Gear::getExtension()
121 void Gear::getForce(float* force, float* contact)
123 Math::set3(_force, force);
124 Math::set3(_contact, contact);
132 float Gear::getCompressFraction()
137 bool Gear::getCastering()
142 void Gear::calcForce(RigidBody* body, float* v, float* rot, float* ground)
144 // Init the return values
146 for(i=0; i<3; i++) _force[i] = _contact[i] = 0;
148 // Don't bother if it's not down
154 // First off, make sure that the gear "tip" is below the ground.
155 // If it's not, there's no force.
156 float a = ground[3] - Math::dot3(_pos, ground);
163 // Now a is the distance from the tip to ground, so make b the
164 // distance from the base to ground. We can get the fraction
165 // (0-1) of compression from a/(a-b). Note the minus sign -- stuff
166 // above ground is negative.
167 Math::add3(_cmpr, _pos, tmp);
168 float b = ground[3] - Math::dot3(tmp, ground);
170 // Calculate the point of ground _contact.
174 _contact[i] = _pos[i] + _frac*_cmpr[i];
176 // Turn _cmpr into a unit vector and a magnitude
178 float clen = Math::mag3(_cmpr);
179 Math::mul3(1/clen, _cmpr, cmpr);
181 // Now get the velocity of the point of contact
183 body->pointVelocity(_contact, rot, cv);
184 Math::add3(cv, v, cv);
186 // Finally, we can start adding up the forces. First the spring
187 // compression. (note the clamping of _frac to 1):
188 _frac = (_frac > 1) ? 1 : _frac;
189 float fmag = _frac*clen*_spring;
191 // Then the damping. Use the only the velocity into the ground
192 // (projection along "ground") projected along the compression
193 // axis. So Vdamp = ground*(ground dot cv) dot cmpr
194 Math::mul3(Math::dot3(ground, cv), ground, tmp);
195 float dv = Math::dot3(cmpr, tmp);
196 float damp = _damp * dv;
197 if(damp > fmag) damp = fmag; // can't pull the plane down!
198 if(damp < -fmag) damp = -fmag; // sanity
200 // The actual force applied is only the component perpendicular to
201 // the ground. Side forces come from velocity only.
202 _wow = (fmag - damp) * -Math::dot3(cmpr, ground);
203 Math::mul3(-_wow, ground, _force);
205 // Castering gear feel no force in the ground plane
209 // Wheels are funky. Split the velocity along the ground plane
210 // into rolling and skidding components. Assuming small angles,
211 // we generate "forward" and "left" unit vectors (the compression
212 // goes "up") for the gear, make a "steer" direction from these,
213 // and then project it onto the ground plane. Project the
214 // velocity onto the ground plane too, and extract the "steer"
215 // component. The remainder is the skid velocity.
217 float gup[3]; // "up" unit vector from the ground
218 Math::set3(ground, gup);
219 Math::mul3(-1, gup, gup);
221 float xhat[] = {1,0,0};
222 float steer[3], skid[3];
223 Math::cross3(gup, xhat, skid); // up cross xhat =~ skid
224 Math::unit3(skid, skid); // == skid
226 Math::cross3(skid, gup, steer); // skid cross up == steer
229 // Correct for a (small) rotation
230 Math::mul3(_rot, steer, tmp);
231 Math::add3(tmp, skid, skid);
232 Math::unit3(skid, skid);
233 Math::cross3(skid, gup, steer);
236 float vsteer = Math::dot3(cv, steer);
237 float vskid = Math::dot3(cv, skid);
238 float wgt = Math::dot3(_force, gup); // force into the ground
240 float fsteer = _brake * calcFriction(wgt, vsteer);
241 float fskid = calcFriction(wgt, vskid);
242 if(vsteer > 0) fsteer = -fsteer;
243 if(vskid > 0) fskid = -fskid;
245 // Phoo! All done. Add it up and get out of here.
246 Math::mul3(fsteer, steer, tmp);
247 Math::add3(tmp, _force, _force);
249 Math::mul3(fskid, skid, tmp);
250 Math::add3(tmp, _force, _force);
253 float Gear::calcFriction(float wgt, float v)
255 // How slow is stopped? 10 cm/second?
256 const float STOP = 0.1f;
257 const float iSTOP = 1.0f/STOP;
259 if(v < STOP) return v*iSTOP * wgt * _sfric;
260 else return wgt * _dfric;
263 }; // namespace yasim