Last active
December 14, 2015 06:19
-
-
Save andrewdolce/5041388 to your computer and use it in GitHub Desktop.
lookahead
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The "optimal" velocity is the velocity needed to bring us to | |
// the target angle in a single physics step. | |
var optimalVelocity = targetAngle / dt; | |
// The "optimal" torque is the amount of torque needed this | |
// frame to achieve the "optimal" velocity. | |
var optimalTorque = ( optimalVelocity - angularVelocity ) / ( dt * inverseInertia ); | |
var desiredTorqueSign = nonZeroSign( desiredTorque ); | |
if ( desiredTorqueSign === nonZeroSign(optimalTorque) ) { | |
// If the torque we desire is greater than the "optimal" | |
// torque, we obviously don't need all of it. | |
if ( Math.abs(desiredTorque) > Math.abs(optimalTorque) ) { | |
// We look forward to the following physics step and predict | |
// the amount of braking force required to "freeze" the | |
// mouse look. | |
var requiredBrakeTorque = -optimalVelocity / ( dt * inverseInertia ); | |
var availableBrakeTorque; | |
if ( desiredTorqueSign === nonZeroSign(requiredBrakeTorque) ) { | |
availableBrakeTorque = desiredTorque; | |
} else { | |
availableBrakeTorque = brakeTorque; | |
} | |
// If the amount of available braking torque is enough, then | |
// it will be possible to stop immediately, so we use the | |
// optimal torque. | |
if ( Math.abs(requiredBrakeTorque) <= Math.abs(availableBrakeTorque) ) { | |
desiredTorque = optimalTorque; | |
} | |
} | |
} | |
result.torque = desiredTorque; | |
return result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment