Code:
// Returns time until player can intercept linear path of ball,
//
// Inputs: ball's radius, position, velocity,
// time delay until it can start moving
// player's radius, position, speed (no acceleration, assumes that
// player can go full speed in any direction necessary),
// time delay until it can start moving
//
// returning by reference the position along ball's path where player will first be able to touch it
// returning by reference the extra time player has until ball reaches that interception point
// returning the time it took for the player to reach the point where it would touch the ball at the interception point
b2Float predictBallPlayerInterception(const b2Float &ballRadius, const b2Vec2 &ballPosition, const b2Vec2 &ballVelocity, const b2Float &ballBeginMovementDelay,
const b2Float &playerRadius, const b2Vec2 &playerPosition, const b2Float &playerSpeed, const b2Float &playerBeginMovementDelay,
b2Vec2& interceptionPosition, b2Float &extraTime)
{
b2Vec2 ballMovementDirection = ballVelocity; // get ball movement direction
b2Float ballSpeed = ballMovementDirection.Normalize(); // get ball speed
b2Vec2 offsetToBall = ballPosition - playerPosition; // get player movement required to get to current ball position
b2Float distanceFromPlayerToBallAtInterception = fabs(b2Cross(ballMovementDirection, offsetToBall)); // distance to from player center to center of ball at interception point
b2Float distanceBallTravelsToPassPlayer = -b2Dot(ballMovementDirection, offsetToBall); // distance until ball passes player
b2Float timeUntilBallPassesPlayer = ballSpeed == 0.0 ? 0.0 : // interval until ball passes player
distanceBallTravelsToPassPlayer / ballSpeed + ballBeginMovementDelay;
interceptionPosition = ballPosition + distanceBallTravelsToPassPlayer * ballMovementDirection; // calculate interception position
b2Float timeUntilPlayerReachesInterception = b2Max(0.0, // discover how long it will take for the player to reach the interception point
(distanceFromPlayerToBallAtInterception - ballRadius - playerRadius) / playerSpeed + playerBeginMovementDelay);
if(timeUntilPlayerReachesInterception <= timeUntilBallPassesPlayer) // if the player can get to the interception point before the ball arrives
{
extraTime = timeUntilBallPassesPlayer - timeUntilPlayerReachesInterception; // most of the time is extra
return timeUntilPlayerReachesInterception; // and it takes no time for the player to arrive
}
else if(ballSpeed > playerSpeed) // misses the chance or there is no chance to get the ball
{
return DBL_MAX;
}
b2Float headStartToInterceptionPoint = ballRadius + playerRadius - // formula's adjustment for radii and player start delay
playerSpeed * playerBeginMovementDelay;
b2Float ballSpeedSquared = ballSpeed * ballSpeed; // convenience since it is used multiple times
b2Float a = ballSpeedSquared - playerSpeed * playerSpeed; // calculate coefficients of quadratic
b2Float b = -2.0 * (ballSpeedSquared * timeUntilBallPassesPlayer +
playerSpeed * headStartToInterceptionPoint);
b2Float c = ballSpeedSquared * timeUntilBallPassesPlayer * timeUntilBallPassesPlayer +
distanceFromPlayerToBallAtInterception * distanceFromPlayerToBallAtInterception +
headStartToInterceptionPoint * headStartToInterceptionPoint;
b2Float discriminant = b * b - 4.0 * a * c;
b2Assert(discriminant >= 0.0);
b2Float sqrtDet = sqrt(discriminant);
b2Float root1 = (-b - sqrtDet) / (2.0 * a);
b2Float root2 = (-b + sqrtDet) / (2.0 * a);
b2Float timeUntilInterception = // choose smallest positive root, otherwise choose smallest absolute value
(root1 >= 0.0 && root2 >= 0.0) ? b2Min(root1, root2) : b2Max(root1, root2);
extraTime = timeUntilBallPassesPlayer - timeUntilInterception; // player waits this long for ball to arrive at interception position
return timeUntilInterception; // return interval until interception occurs
}
Got it working. You can see the if and else if, which handle the special cases.