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);
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.
196 Math::add3(_cmpr, _pos, tmp);
197 float b = ground[3] - Math::dot3(tmp, ground);
199 // Calculate the point of ground _contact.
203 _contact[i] = _pos[i] + _frac*_cmpr[i];
205 // Turn _cmpr into a unit vector and a magnitude
207 float clen = Math::mag3(_cmpr);
208 Math::mul3(1/clen, _cmpr, cmpr);
210 // Now get the velocity of the point of contact
212 body->pointVelocity(_contact, rot, cv);
213 Math::add3(cv, v, cv);
214 Math::sub3(cv, glvel, cv);
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;
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
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);
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.
243 float gup[3]; // "up" unit vector from the ground
244 Math::set3(ground, gup);
245 Math::mul3(-1, gup, gup);
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
252 Math::cross3(skid, gup, steer); // skid cross up == steer
255 // Correct for a rotation
256 float srot = Math::sin(_rot);
257 float crot = Math::cos(_rot);
260 steer[0] = crot*tx + srot*ty;
261 steer[1] = -srot*tx + crot*ty;
265 skid[0] = crot*tx + srot*ty;
266 skid[1] = -srot*tx + crot*ty;
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
274 _rollSpeed = Math::sqrt(vsteer*vsteer + vskid*vskid);
275 _casterAngle = Math::atan2(vskid, vsteer);
282 float fsteer = _brake * calcFriction(wgt, vsteer);
283 float fskid = calcFriction(wgt, vskid);
284 if(vsteer > 0) fsteer = -fsteer;
285 if(vskid > 0) fskid = -fskid;
287 // Phoo! All done. Add it up and get out of here.
288 Math::mul3(fsteer, steer, tmp);
289 Math::add3(tmp, _force, _force);
291 Math::mul3(fskid, skid, tmp);
292 Math::add3(tmp, _force, _force);
295 float Gear::calcFriction(float wgt, float v)
297 // How slow is stopped? 10 cm/second?
298 const float STOP = 0.1f;
299 const float iSTOP = 1.0f/STOP;
301 if(v < STOP) return v*iSTOP * wgt * _sfric;
302 else return wgt * _dfric;
305 }; // namespace yasim