2 #include "RigidBody.hpp"
11 _pos[i] = _cmpr[i] = 0;
21 void Gear::setPosition(float* position)
24 for(i=0; i<3; i++) _pos[i] = position[i];
27 void Gear::setCompression(float* compression)
30 for(i=0; i<3; i++) _cmpr[i] = compression[i];
33 void Gear::setSpring(float spring)
38 void Gear::setDamping(float damping)
43 void Gear::setStaticFriction(float sfric)
48 void Gear::setDynamicFriction(float dfric)
53 void Gear::setBrake(float brake)
55 _brake = Math::clamp(brake, 0, 1);
58 void Gear::setRotation(float rotation)
63 void Gear::setExtension(float extension)
65 _extension = Math::clamp(extension, 0, 1);
68 void Gear::getPosition(float* out)
71 for(i=0; i<3; i++) out[i] = _pos[i];
74 void Gear::getCompression(float* out)
77 for(i=0; i<3; i++) out[i] = _cmpr[i];
80 float Gear::getSpring()
85 float Gear::getDamping()
90 float Gear::getStaticFriction()
95 float Gear::getDynamicFriction()
100 float Gear::getBrake()
105 float Gear::getRotation()
110 float Gear::getExtension()
115 void Gear::getForce(float* force, float* contact)
117 Math::set3(_force, force);
118 Math::set3(_contact, contact);
126 float Gear::getCompressFraction()
131 void Gear::calcForce(RigidBody* body, float* v, float* rot, float* ground)
133 // Init the return values
135 for(i=0; i<3; i++) _force[i] = _contact[i] = 0;
137 // Don't bother if it's not down
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);
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);
156 // Calculate the point of ground _contact.
160 _contact[i] = _pos[i] + _frac*_cmpr[i];
162 // Turn _cmpr into a unit vector and a magnitude
164 float clen = Math::mag3(_cmpr);
165 Math::mul3(1/clen, _cmpr, cmpr);
167 // Now get the velocity of the point of contact
169 body->pointVelocity(_contact, rot, cv);
170 Math::add3(cv, v, cv);
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);
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);
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.
199 float gup[3]; // "up" unit vector from the ground
200 Math::set3(ground, gup);
201 Math::mul3(-1, gup, gup);
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
208 Math::cross3(skid, gup, steer); // skid cross up == steer
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);
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
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;
227 // Phoo! All done. Add it up and get out of here.
228 Math::mul3(fsteer, steer, tmp);
229 Math::add3(tmp, _force, _force);
231 Math::mul3(fskid, skid, tmp);
232 Math::add3(tmp, _force, _force);
235 float Gear::calcFriction(float wgt, float v)
237 // How slow is stopped? 50 cm/second?
238 const float STOP = 0.5;
239 const float iSTOP = 1/STOP;
241 if(v < STOP) return v*iSTOP * wgt * _sfric;
242 else return wgt * _dfric;
245 }; // namespace yasim