Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. //Original from tutorial
  2. public Vector Wheel::CalculateForce(Vector relativeGroundSpeed, float dt)
  3. {
  4.     //calculate speed of tire patch at ground
  5.     Vector patchSpeed = LocalForwardDir * PatchSpeed;
  6.  
  7.     //get velocity difference between ground and patch
  8.     Vector velDifference = relativeGroundSpeed + patchSpeed;
  9.  
  10.     //project ground speed onto side axis
  11.     float forwardMag = 0;
  12.     Vector sideVel = velDifference.Project(mSideAxis);
  13.     Vector forwardVel = velDifference.Project(mForwardAxis, out forwardMag);
  14.  
  15.     //calculate super fake friction forces
  16.     //calculate response force
  17.     Vector responseForce = -sideVel * 2.0f;
  18.     responseForce -= forwardVel;
  19.  
  20.     //calculate torque on wheel
  21.     mTorque += forwardMag * mRadius;
  22.  
  23.     //return force acting on body
  24.     return responseForce;
  25. }
  26.  
  27.  
  28. // New physics model:
  29. public void ApplyWheelConstraint(Wheel wheel, Vector worldGravity, float dt)
  30. {
  31.     // Gather a lot of data
  32.     float maxKineticAcc = cGravity * wheel.mKineticFrictionCoef;
  33.     float maxKineticDV = maxKineticAcc / mMass * dt;
  34.  
  35.     Vector offset = ToWorld(wheel.mPosition);
  36.     Vector forwardDir = ToWorld(wheel.LocalForwardDir);
  37.     Vector constraintDir = ToWorld(wheel.LocalConstraintDir);
  38.  
  39.     Vector groundVel = PointVel(offset);
  40.     Vector constraintVel = groundVel.Project(constraintDir);
  41.     Vector forwardVel = groundVel.Project(forwardDir);
  42.     float  forwardVelMag = forwardVel.Length;
  43.     Vector patchVel = forwardDir * wheel.PatchSpeed;
  44.     Vector patchVelDiff = groundVel + patchVel;
  45.     float  patchVelDiffMag = patchVelDiff.Length;
  46.  
  47.     Vector torqueDV = new Vector( );
  48.  
  49.     if (wheel.mStaticFriction)
  50.         torqueDV = forwardDir * wheel.ComputeTorqueDV(mMass, dt);
  51.     else
  52.     {
  53.         float forwardVelDiff = patchVelDiff.Dot(forwardDir);
  54.         torqueDV = forwardDir * forwardVelDiff;
  55.     }
  56.  
  57.     // Accumulate accelerations
  58.     Vector desiredDeltaV = constraintVel + torqueDV;
  59.  
  60.     // Compute the corrective impulse and its resultant acceleration
  61.     Vector deltaVDir = desiredDeltaV;
  62.     float deltaVMag = 0.0f;
  63.     deltaVDir.Normalize(out deltaVMag);
  64.     Vector roadImpulse = ComputeImpulseToChangePointVel(dt, offset, deltaVDir, deltaVMag);
  65.     Vector roadImpulseAcc = roadImpulse / mMass / dt;
  66.  
  67.     // Determine whether to switch to kinetic or static friction
  68.     Vector frictionAcc = roadImpulseAcc;
  69.     float accMag = frictionAcc.Length;
  70.  
  71.     if (patchVelDiffMag < 0.25f)
  72.         wheel.mStaticFriction = true;
  73.     else if (wheel.mStaticFriction)
  74.     {
  75.         // Static friction
  76.         float maxAcc = cGravity * wheel.Friction;
  77.         float totalPatchAcc = accMag;
  78.         if (totalPatchAcc > maxAcc)
  79.             wheel.mStaticFriction = false;
  80.     }
  81.  
  82.     if (!wheel.mStaticFriction)
  83.     {
  84.         // Kinetic friction, clamp acceleration
  85.         accMag = Math.Min(accMag, maxKineticAcc);
  86.         frictionAcc = frictionAcc.Normalize() * accMag;
  87.         roadImpulse = frictionAcc * mMass * dt;
  88.     }
  89.  
  90.     // Gather some debugging stats
  91.     wheel.mStats.mWorldLinearAcceleration.mValue = frictionAcc;
  92.  
  93.     // Apply result
  94.     Vector totalImpulse = roadImpulse;
  95.     AddImpulse(totalImpulse, offset);
  96.     Vector newGroundV = PointVel(offset);
  97.  
  98.     // Integrate wheel
  99.     if (wheel.mStaticFriction)
  100.     {
  101.         wheel.mAngVel = newGroundV.Dot(forwardDir) / wheel.mRadius;
  102.     }
  103.     else
  104.     {
  105.         float frictionForce = frictionAcc.Dot(forwardDir) * mMass;
  106.         float frictionTorque = frictionForce * wheel.mRadius;
  107.         wheel.mAngVel += frictionTorque / wheel.mInertia * dt;
  108.     }
  109.  
  110.     wheel.mAngle += wheel.mAngVel * dt;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement