this has to be exactly tha same as this // Constants related to the arena dimensions
constexpr float SIDE_WALL_X = 4096.0f;
constexpr float CLOSE_DISTANCE = 10.0f; // Distance threshold for proximity to the walls
constexpr float NEAR_WALL_DISTANCE = 4077.0f; // Threshold for x close to the wall
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
// Initialize minimum time to infinity
float minTime = std::numeric_limits<float>::infinity();
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;
}
// 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 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²)
// Initialize minTime to infinity
float minTime = std::numeric_limits<float>::infinity();
// ** New Condition: If y is >=5100 or <=-5100, set time to back wall as 0 **
if (pos.y >= MAX_BACKWALL_Y || pos.y <= -MAX_BACKWALL_Y) {
return 0.0f; // Immediate collision with back wall
}
// ** Existing Immediate Collision Checks **
if (pos.y >= BACK_WALL_Y) {
return 0.0f; // Immediate collision with positive back wall
}
if (pos.y <= NEG_BACK_WALL_Y) {
return 0.0f; // Immediate collision with negative back wall
}
// ** Calculate Time to Positive Back Wall (y = BACK_WALL_Y) **
if (vel.y > EPSILON) { // Player is moving towards the positive back wall
float a = 0.5f * BOOST_ACCEL_POS; // Acceleration coefficient (positive)
float b = vel.y; // Initial velocity towards positive y
float c = pos.y - BACK_WALL_Y; // Initial position offset from positive back 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 Back Wall (y = NEG_BACK_WALL_Y) **
if (vel.y < -EPSILON) { // Player is moving towards the negative back wall
float a = 0.5f * BOOST_ACCEL_NEG; // Acceleration coefficient (negative)
float b = vel.y; // Initial velocity towards negative y
float c = pos.y - NEG_BACK_WALL_Y; // Initial position offset from negative back 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;
}
// Constants related to the arena dimensions and gravity
constexpr float FLOOR_Z = 18.0f; // Assuming the floor is at z = 0
constexpr float BOOST_ACCEL_DOWN = -1000.0f;
// ** Immediate Floor Proximity Check **
if (pos.z <= FLOOR_Z) {
return 0.0f; // Player is already at or below the floor
}
// ** Calculate Total Downward Acceleration **
// Sum of gravity and boosting acceleration (both downward, hence negative)
float totalAccel = GRAVITY_Z + BOOST_ACCEL_DOWN; // e.g., -650 + (-1000) = -1650 units/s²
// ** Define Quadratic Coefficients **
// Using the kinematic equation: 0.5 * a * t² + v₀ * t + s₀ = 0
float a = 0.5f * totalAccel; // Acceleration coefficient (negative)
float b = vel.z; // Initial vertical velocity
float c = pos.z; // Initial position above the floor
// ** Calculate Discriminant **
float discriminant = b * b - 4.0f * a * c;
// ** Check for Real Solutions **
if (discriminant < 0.0f) {
return std::numeric_limits<float>::infinity(); // No real solution; player won't reach the floor
}
// ** Calculate the Two Possible Times Using the Quadratic Formula **
float sqrtD = std::sqrt(discriminant);
float t1 = (-b - sqrtD) / (2.0f * a); // Smaller time
float t2 = (-b + sqrtD) / (2.0f * a); // Larger time
// ** Select the Positive and Smaller Time **
float timeToFloor = std::numeric_limits<float>::infinity();
if (t1 > 0.0f) {
timeToFloor = t1;
}
if (t2 > 0.0f && t2 < timeToFloor) {
timeToFloor = t2;
}
return timeToFloor;
}
#include // for std::min
float calculateTimeToReachAnySurface(const PlayerData& player) {
// Calculate individual times to each surface
float timeToSideWalls = calculateTimeToSideWalls(player);
// RG_LOG(timeToSideWalls); // Uncomment if logging is needed
code
float timeToBackWalls = calculateTimeToBackWalls(player);
//RG_LOG(timeToBackWalls); // Uncomment if logging is needed
float timeToFloor = calculateTimeToFloor(player);
// RG_LOG(timeToFloor); // Uncomment if logging is needed
// Assign identifier variables for clarity
float minTimeBetweenBackWallsAndFloor = RS_MIN(timeToBackWalls, timeToFloor);
// Now find the minimum time among side walls and the previously computed minimum
float minTime = RS_MIN(timeToSideWalls, minTimeBetweenBackWallsAndFloor);
return minTime;
It looks like you're aiming to ensure that your TimeToReachCalculator class mirrors the functionality and structure of the provided free-function-based implementation. To achieve an exact match, we'll need to address several aspects, including constant definitions, function implementations, and overall structure.
1. Constant Definitions
In your class-based implementation, constants are defined as static constexpr within the class. In the free-function-based code, constants are defined in the global namespace. To make them identical, you have two primary options:
Option A: Keep Constants Within the Class
If you prefer encapsulating constants within the class (which is generally good practice to avoid global namespace pollution), ensure that all constants in both implementations have the same values and names.
Option B: Move Constants to the Global Namespace
If the free-function-based code relies on global constants, you can refactor your class to use these global constants instead of defining its own. This promotes consistency across different parts of your codebase.
Recommendation: Given that constants like SIDE_WALL_X, BOOST_ACCEL_GROUND, etc., are fundamental to both implementations, it's often better to define them globally if multiple classes or functions use them. This avoids duplication and potential discrepancies.
2. Function Implementations
Your class TimeToReachCalculator contains several private static helper functions (calculateTimeToSideWalls, calculateTimeToBackWalls, etc.), which correspond directly to the free functions in the second code snippet.
To ensure exact parity:
Ensure Identical Implementations: Verify that the logic within each helper function matches precisely between the class and the free functions. This includes variable names, calculations, and control flow.
Consistent Function Signatures: The parameters and return types should match exactly. For instance, if calculateTimeToSideWalls takes a const PlayerData&, ensure both implementations do so.
3. Overall Structure
The main function calculateTimeToReach in both implementations follows a similar loop-based approach to simulate the time it takes for a player to reach a target position, considering various accelerations and environmental factors.
Key Points to Ensure Consistency:
Initialization: Both functions initialize variables like carPos, carVel, dirToTarget, dist, vel, rotTime, and timeToSurface in the same manner.
Simulation Loop: The loop structure, including the condition checks (curDist > DIST_REACH_THRESH), time increments (t += STEP_INTERVAL), and distance reduction (curDist -= curVel * STEP_INTERVAL), should be identical.
Velocity Adjustments: The logic for updating curVel based on whether the player has rotated or is on the surface must match exactly. Pay special attention to how BOOST_ACCEL_GROUND and other accelerations are applied.
Termination Condition: Both implementations return the calculated time t once certain conditions are met (e.g., curVel >= MAX_TOTAL_SPEED).
4. Refactoring for Exact Match
To align the class-based implementation with the free-function-based one precisely, consider the following refactored version of your TimeToReachCalculator class:
cpp
5. Ensuring Identical Behavior with RLBotBot Class
You also provided an RLBotBot class with its own calculateTimeToReach and calculateRotationTime methods. To ensure that both the TimeToReachCalculator class and the RLBotBot class behave identically:
Use TimeToReachCalculator Within RLBotBot
Instead of having separate implementations, you can utilize the TimeToReachCalculator within the RLBotBot methods. This promotes code reuse and consistency.
Make sure that PlayerData in TimeToReachCalculator is compatible with the car object from gs.players[this->index]. If there are differences in types or accessed members, adjust accordingly.
6. Final Checks
Consistent Naming: Ensure all constants and functions have identical names and values across both implementations.
Testing: Rigorously test both implementations to confirm they produce identical results under various scenarios.
Documentation: Comment your code to indicate that TimeToReachCalculator is used within RLBotBot, ensuring future maintainers understand the relationship.
7. Summary
By centralizing the time calculation logic within the TimeToReachCalculator class and having the RLBotBot class utilize it, you ensure that both implementations remain consistent and maintainable. This approach reduces redundancy and potential bugs stemming from having multiple versions of the same logic.
If you have any specific discrepancies or issues after refactoring, feel free to provide details, and I can assist further!
Not yet included in a published release.
#include <cmath>
#include <algorithm>
#include <limits>
// Constants related to the arena dimensions
constexpr float SIDE_WALL_X = 4096.0f;
constexpr float CLOSE_DISTANCE = 10.0f; // Distance threshold for proximity to the walls
constexpr float NEAR_WALL_DISTANCE = 4077.0f; // Threshold for x close to the wall
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
// 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 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²)
// Constants related to the arena dimensions and gravity
constexpr float FLOOR_Z = 18.0f; // Assuming the floor is at z = 0
constexpr float BOOST_ACCEL_DOWN = -1000.0f;
// Utility Macros or Inline Functions
inline float RS_MIN(float a, float b) { return std::min(a, b); }
inline float RS_CLAMP(float val, float minVal, float maxVal) {
return std::max(minVal, std::min(val, maxVal));
}
// Assuming definitions for PlayerData and Vec are available
class TimeToReachCalculator {
public:
static float calculateTimeToReach(const PlayerData& player, const Vec& targetPos) {
Vec carPos = player.phys.pos;
Vec carVel = player.phys.vel;
Vec dirToTarget = (targetPos - carPos).Normalized();
float dist = (targetPos - carPos).Length() - CommonValues::BALL_RADIUS;
float vel = carVel.Dot(dirToTarget);
float rotTime = calculateRotationTime(player, targetPos);
float timeToSurface = calculateTimeToReachAnySurface(player);
float t = 0;
for (float curDist = dist, curVel = vel; curDist > DIST_REACH_THRESH; t += STEP_INTERVAL, curDist -= curVel * STEP_INTERVAL) {
bool hasRotated = t >= rotTime;
bool onSurface = t >= timeToSurface;
if (curVel < MAX_TOTAL_SPEED && t == 0) {
// Flip force
curVel += FLIP_FORCE;
}
if (onSurface) {
if (curVel < 0) {
curVel += (BRAKE_ACCEL + BOOST_ACCEL_GROUND * hasRotated) * STEP_INTERVAL;
}
else if (curVel < MAX_DRIVE_SPEED) {
curVel += (DRIVE_ACCEL + BOOST_ACCEL_GROUND * hasRotated) * STEP_INTERVAL;
}
else if (curVel < MAX_TOTAL_SPEED) {
curVel += BOOST_ACCEL_GROUND * hasRotated * STEP_INTERVAL;
}
}
if (curVel >= MAX_TOTAL_SPEED) {
return t + (curDist / curVel);
}
}
return t;
}
private:
static float calculateTimeToSideWalls(const PlayerData& player) {
const Vec& pos = player.phys.pos;
const Vec& vel = player.phys.vel;
float minTime = std::numeric_limits<float>::infinity();
if (std::abs(pos.x) >= NEAR_WALL_DISTANCE && std::abs(pos.x) <= SIDE_WALL_X) {
return 0.0f;
}
if (vel.x > EPSILON) {
float a = 0.5f * DRIVE_ACCEL;
float b = vel.x;
float c = pos.x - SIDE_WALL_X;
float discriminant = b * b - 4.0f * a * c;
if (discriminant >= 0.0f) {
float sqrtD = std::sqrt(discriminant);
float t1 = (-b + sqrtD) / (2.0f * a);
float t2 = (-b - sqrtD) / (2.0f * a);
if (t1 > EPSILON && t1 < minTime) {
minTime = t1;
}
if (t2 > EPSILON && t2 < minTime) {
minTime = t2;
}
}
}
if (vel.x < -EPSILON) {
float a = 0.5f * (-DRIVE_ACCEL);
float b = vel.x;
float c = pos.x + SIDE_WALL_X;
float discriminant = b * b - 4.0f * a * c;
if (discriminant >= 0.0f) {
float sqrtD = std::sqrt(discriminant);
float t1 = (-b + sqrtD) / (2.0f * a);
float t2 = (-b - sqrtD) / (2.0f * a);
if (t1 > EPSILON && t1 < minTime) {
minTime = t1;
}
if (t2 > EPSILON && t2 < minTime) {
minTime = t2;
}
}
}
return minTime;
}
static float calculateTimeToBackWalls(const PlayerData& player) {
const Vec& pos = player.phys.pos;
const Vec& vel = player.phys.vel;
float minTime = std::numeric_limits<float>::infinity();
if (pos.y >= MAX_BACKWALL_Y || pos.y <= -MAX_BACKWALL_Y) {
return 0.0f;
}
if (pos.y >= BACK_WALL_Y || pos.y <= NEG_BACK_WALL_Y) {
return 0.0f;
}
if (vel.y > EPSILON) {
float a = 0.5f * BOOST_ACCEL_POS;
float b = vel.y;
float c = pos.y - BACK_WALL_Y;
float discriminant = b * b - 4.0f * a * c;
if (discriminant >= 0.0f) {
float sqrtD = std::sqrt(discriminant);
float t1 = (-b + sqrtD) / (2.0f * a);
float t2 = (-b - sqrtD) / (2.0f * a);
if (t1 > EPSILON && t1 < minTime) {
minTime = t1;
}
if (t2 > EPSILON && t2 < minTime) {
minTime = t2;
}
}
}
if (vel.y < -EPSILON) {
float a = 0.5f * BOOST_ACCEL_NEG;
float b = vel.y;
float c = pos.y - NEG_BACK_WALL_Y;
float discriminant = b * b - 4.0f * a * c;
if (discriminant >= 0.0f) {
float sqrtD = std::sqrt(discriminant);
float t1 = (-b + sqrtD) / (2.0f * a);
float t2 = (-b - sqrtD) / (2.0f * a);
if (t1 > EPSILON && t1 < minTime) {
minTime = t1;
}
if (t2 > EPSILON && t2 < minTime) {
minTime = t2;
}
}
}
return minTime;
}
static float calculateTimeToFloor(const PlayerData& player) {
const Vec& pos = player.phys.pos;
const Vec& vel = player.phys.vel;
if (pos.z <= FLOOR_Z) {
return 0.0f;
}
float totalAccel = GRAVITY_Z + BOOST_ACCEL_DOWN;
float a = 0.5f * totalAccel;
float b = vel.z;
float c = pos.z - FLOOR_Z;
float discriminant = b * b - 4.0f * a * c;
if (discriminant < 0.0f) {
return std::numeric_limits<float>::infinity();
}
float sqrtD = std::sqrt(discriminant);
float t1 = (-b - sqrtD) / (2.0f * a);
float t2 = (-b + sqrtD) / (2.0f * a);
float timeToFloor = std::numeric_limits<float>::infinity();
if (t1 > 0.0f) {
timeToFloor = t1;
}
if (t2 > 0.0f && t2 < timeToFloor) {
timeToFloor = t2;
}
return timeToFloor;
}
static float calculateTimeToReachAnySurface(const PlayerData& player) {
float timeToSideWalls = calculateTimeToSideWalls(player);
float timeToBackWalls = calculateTimeToBackWalls(player);
float timeToFloor = calculateTimeToFloor(player);
float minTimeBetweenBackWallsAndFloor = RS_MIN(timeToBackWalls, timeToFloor);
float minTime = RS_MIN(timeToSideWalls, minTimeBetweenBackWallsAndFloor);
return minTime;
}
static float calculateRotationTime(const PlayerData& player, const Vec& targetPos) {
constexpr float MAX_ANG_VEL = 5.5f * 0.9f;
Vec playerPos = player.phys.pos;
Vec playerForward = player.phys.rotMat.forward;
Vec dirToTarget = (targetPos - playerPos).Normalized();
float angleDiff = std::acos(RS_CLAMP(playerForward.Dot(dirToTarget), -1.0f, 1.0f));
return angleDiff / MAX_ANG_VEL;
}
};