Certainly! To align the calculateTimeToSideWalls function with the structure and style of the calculateTimeToBackWalls and calculateTimeToFloor functions, we'll adopt an analytical approach using quadratic equations. This method avoids iterative simulations and provides a more efficient computation.
Here's the refactored code:
#include <cmath>
#include <limits>
// ========================
// Constants Related to the Arena Dimensions
// ========================
constexpr float BACK_WALL_Y = 5120.0f;
constexpr float NEG_BACK_WALL_Y = -5120.0f; // Negative back wall at y = -5120.0f
constexpr float MAX_BACKWALL_Y = 5100.0f; // New threshold as per user request
constexpr float SIDE_WALL_X = 4096.0f;
constexpr float NEAR_WALL_DISTANCE = 4077.0f; // Threshold for x close to the wall
constexpr float CLOSE_DISTANCE = 10.0f; // Distance threshold for proximity to the walls
constexpr float Z_THRESHOLD = 256.0f; // Threshold for z axis proximity to the ground
constexpr float EPSILON = 1e-3f; // Small value for floating point comparison
// ========================
// Movement-Related Constants
// ========================
constexpr float DIST_REACH_THRESH = 80.0f;
constexpr float STEP_INTERVAL = 1.0f / 60.0f; // Time step (e.g., 60 FPS)
constexpr float FLIP_FORCE = 495.0f;
constexpr float AIR_ACCEL = 66.66667f;
constexpr float BOOST_ACCEL_AIR = 1058.33333f;
constexpr float MAX_TOTAL_SPEED = 2300.0f;
constexpr float BRAKE_ACCEL = 3500.0f;
constexpr float BOOST_ACCEL_GROUND = 991.66667f;
constexpr float DRIVE_ACCEL = 1600.0f;
constexpr float MAX_DRIVE_SPEED = 1410.0f;
constexpr float GRAVITY_Z = -650.0f; // Example gravity value, adjust as needed
constexpr float BOOST_ACCEL_POS = 1000.0f; // Boost acceleration towards positive back wall (units/s²)
constexpr float BOOST_ACCEL_NEG = -1000.0f; // Boost acceleration towards negative back wall (units/s²)
constexpr float BOOST_ACCEL_DOWN = -1000.0f;
// ========================
// Data Structures
// ========================
struct Vec {
float x, y, z;
};
struct Physics {
Vec pos;
Vec vel;
};
struct PlayerData {
Physics phys;
// Other player-related data...
};
// ========================
// Function to Calculate Time to Side Walls
// ========================
float calculateTimeToSideWalls(const PlayerData& player) {
const Vec& pos = player.phys.pos;
const Vec& vel = player.phys.vel;
// Initialize minimum time to infinity
float minTime = std::numeric_limits<float>::infinity();
// ** Immediate Proximity Checks **
if (pos.z <= Z_THRESHOLD) {
return 0.0f; // Player is too close to the ground
}
if (std::abs(pos.x) >= NEAR_WALL_DISTANCE && std::abs(pos.x) <= SIDE_WALL_X) {
return 0.0f; // Player is very close to the side walls
}
// ** Calculate Time to Positive Side Wall (x = SIDE_WALL_X) **
if (vel.x > EPSILON) { // Player is moving towards the positive side wall
float a = 0.5f * DRIVE_ACCEL; // Acceleration coefficient (positive)
float b = vel.x; // Initial velocity towards positive x
float c = pos.x - SIDE_WALL_X; // Initial position offset from positive side wall
// Quadratic equation: a*t^2 + b*t + c = 0
float discriminant = b * b - 4.0f * a * c;
if (discriminant >= 0.0f) { // Real solutions exist
float sqrtD = std::sqrt(discriminant);
float t1 = (-b + sqrtD) / (2.0f * a);
float t2 = (-b - sqrtD) / (2.0f * a);
// Select the smallest positive time
if (t1 > EPSILON && t1 < minTime) {
minTime = t1;
}
if (t2 > EPSILON && t2 < minTime) {
minTime = t2;
}
}
}
// ** Calculate Time to Negative Side Wall (x = -SIDE_WALL_X) **
if (vel.x < -EPSILON) { // Player is moving towards the negative side wall
float a = 0.5f * (-DRIVE_ACCEL); // Acceleration coefficient (negative)
float b = vel.x; // Initial velocity towards negative x
float c = pos.x + SIDE_WALL_X; // Initial position offset from negative side wall
// Quadratic equation: a*t^2 + b*t + c = 0
float discriminant = b * b - 4.0f * a * c;
if (discriminant >= 0.0f) { // Real solutions exist
float sqrtD = std::sqrt(discriminant);
float t1 = (-b + sqrtD) / (2.0f * a);
float t2 = (-b - sqrtD) / (2.0f * a);
// Select the smallest positive time
if (t1 > EPSILON && t1 < minTime) {
minTime = t1;
}
if (t2 > EPSILON && t2 < minTime) {
minTime = t2;
}
}
}
return minTime;
}
// ========================
// Function to Calculate Time to Back Walls (For Reference)
// ========================
// ... (Existing `calculateTimeToBackWalls` function remains unchanged)
// ========================
// Function to Calculate Time to Floor (For Reference)
// ========================
// ... (Existing `calculateTimeToFloor` function remains unchanged)
Explanation of Changes
-
Analytical Approach Using Quadratic Equations:
- Similar to the
calculateTimeToBackWalls and calculateTimeToFloor functions, calculateTimeToSideWalls now uses quadratic equations to determine the exact time when the player will collide with the side walls.
-
Constants Organization:
- Constants are organized at the top, categorizing arena dimensions and movement-related parameters for better readability and maintenance.
-
Immediate Proximity Checks:
- Before performing any calculations, the function checks if the player is already too close to the walls or the ground, returning
0.0f if an immediate collision is detected.
-
Handling Positive and Negative Side Walls Separately:
-
Quadratic Equation Structure:
- The standard form used is:
0.5 * a * t² + b * t + c = 0, where:
a is half of the acceleration.
b is the initial velocity.
c is the initial position offset from the wall.
-
Discriminant Check:
- Ensures that the discriminant (
b² - 4ac) is non-negative to have real solutions, implying a potential collision.
-
Selecting the Appropriate Time:
- From the quadratic solutions ( and ), the function selects the smallest positive time greater than to avoid floating-point inaccuracies and ensure meaningful collision time.
Additional Considerations
Conclusion
By adopting an analytical method using quadratic equations, the calculateTimeToSideWalls function now mirrors the structure and efficiency of the calculateTimeToBackWalls and calculateTimeToFloor functions. This approach enhances performance by eliminating the need for iterative simulations and provides precise collision timing based on the player's current state.