2 #include "BodyEnvironment.hpp"
3 #include "RigidBody.hpp"
12 _pos[i] = _cmpr[i] = 0;
24 _global_ground[i] = _global_vel[i] = 0;
25 _global_ground[2] = 1;
26 _global_ground[3] = -1e3;
29 void Gear::setPosition(float* position)
32 for(i=0; i<3; i++) _pos[i] = position[i];
35 void Gear::setCompression(float* compression)
38 for(i=0; i<3; i++) _cmpr[i] = compression[i];
41 void Gear::setSpring(float spring)
46 void Gear::setDamping(float damping)
51 void Gear::setStaticFriction(float sfric)
56 void Gear::setDynamicFriction(float dfric)
61 void Gear::setBrake(float brake)
63 _brake = Math::clamp(brake, 0, 1);
66 void Gear::setRotation(float rotation)
71 void Gear::setExtension(float extension)
73 _extension = Math::clamp(extension, 0, 1);
76 void Gear::setCastering(bool c)
81 void Gear::setGlobalGround(double *global_ground, float* global_vel)
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];
88 void Gear::getPosition(float* out)
91 for(i=0; i<3; i++) out[i] = _pos[i];
94 void Gear::getCompression(float* out)
97 for(i=0; i<3; i++) out[i] = _cmpr[i];
100 void Gear::getGlobalGround(double* global_ground)
103 for(i=0; i<4; i++) global_ground[i] = _global_ground[i];
106 float Gear::getSpring()
111 float Gear::getDamping()
116 float Gear::getStaticFriction()
121 float Gear::getDynamicFriction()
126 float Gear::getBrake()
131 float Gear::getRotation()
136 float Gear::getExtension()
141 void Gear::getForce(float* force, float* contact)
143 Math::set3(_force, force);
144 Math::set3(_contact, contact);
152 float Gear::getCompressFraction()
157 bool Gear::getCastering()
162 void Gear::calcForce(RigidBody* body, State *s, float* v, float* rot)
164 // Init the return values
166 for(i=0; i<3; i++) _force[i] = _contact[i] = 0;
168 // Don't bother if it's not down
172 // The ground plane transformed to the local frame.
174 s->planeGlobalToLocal(_global_ground, ground);
176 // The velocity of the contact patch transformed to local coordinates.
178 s->velGlobalToLocal(_global_vel, glvel);
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);
189 // Now a is the distance from the tip to ground, so make b the
190 // distance from the base to ground. We can get the fraction
191 // (0-1) of compression from a/(a-b). Note the minus sign -- stuff
192 // above ground is negative.
194 Math::add3(_cmpr, _pos, tmp);
195 float b = ground[3] - Math::dot3(tmp, ground);
197 // Calculate the point of ground _contact.
201 _contact[i] = _pos[i] + _frac*_cmpr[i];
203 // Turn _cmpr into a unit vector and a magnitude
205 float clen = Math::mag3(_cmpr);
206 Math::mul3(1/clen, _cmpr, cmpr);
208 // Now get the velocity of the point of contact
210 body->pointVelocity(_contact, rot, cv);
211 Math::add3(cv, v, cv);
212 Math::sub3(cv, glvel, cv);
214 // Finally, we can start adding up the forces. First the spring
215 // compression. (note the clamping of _frac to 1):
216 _frac = (_frac > 1) ? 1 : _frac;
217 float fmag = _frac*clen*_spring;
219 // Then the damping. Use the only the velocity into the ground
220 // (projection along "ground") projected along the compression
221 // axis. So Vdamp = ground*(ground dot cv) dot cmpr
222 Math::mul3(Math::dot3(ground, cv), ground, tmp);
223 float dv = Math::dot3(cmpr, tmp);
224 float damp = _damp * dv;
225 if(damp > fmag) damp = fmag; // can't pull the plane down!
226 if(damp < -fmag) damp = -fmag; // sanity
228 // The actual force applied is only the component perpendicular to
229 // the ground. Side forces come from velocity only.
230 _wow = (fmag - damp) * -Math::dot3(cmpr, ground);
231 Math::mul3(-_wow, ground, _force);
233 // Wheels are funky. Split the velocity along the ground plane
234 // into rolling and skidding components. Assuming small angles,
235 // we generate "forward" and "left" unit vectors (the compression
236 // goes "up") for the gear, make a "steer" direction from these,
237 // and then project it onto the ground plane. Project the
238 // velocity onto the ground plane too, and extract the "steer"
239 // component. The remainder is the skid velocity.
241 float gup[3]; // "up" unit vector from the ground
242 Math::set3(ground, gup);
243 Math::mul3(-1, gup, gup);
245 float xhat[] = {1,0,0};
246 float steer[3], skid[3];
247 Math::cross3(gup, xhat, skid); // up cross xhat =~ skid
248 Math::unit3(skid, skid); // == skid
250 Math::cross3(skid, gup, steer); // skid cross up == steer
253 // Correct for a rotation
254 float srot = Math::sin(_rot);
255 float crot = Math::cos(_rot);
258 steer[0] = crot*tx + srot*ty;
259 steer[1] = -srot*tx + crot*ty;
263 skid[0] = crot*tx + srot*ty;
264 skid[1] = -srot*tx + crot*ty;
267 float vsteer = Math::dot3(cv, steer);
268 float vskid = Math::dot3(cv, skid);
269 float wgt = Math::dot3(_force, gup); // force into the ground
272 _rollSpeed = Math::sqrt(vsteer*vsteer + vskid*vskid);
273 _casterAngle = Math::atan2(vskid, vsteer);
280 float fsteer = _brake * calcFriction(wgt, vsteer);
281 float fskid = calcFriction(wgt, vskid);
282 if(vsteer > 0) fsteer = -fsteer;
283 if(vskid > 0) fskid = -fskid;
285 // Phoo! All done. Add it up and get out of here.
286 Math::mul3(fsteer, steer, tmp);
287 Math::add3(tmp, _force, _force);
289 Math::mul3(fskid, skid, tmp);
290 Math::add3(tmp, _force, _force);
293 float Gear::calcFriction(float wgt, float v)
295 // How slow is stopped? 10 cm/second?
296 const float STOP = 0.1f;
297 const float iSTOP = 1.0f/STOP;
299 if(v < STOP) return v*iSTOP * wgt * _sfric;
300 else return wgt * _dfric;
303 }; // namespace yasim