rlbot::ScopedRenderer renderer("time_to_surface_renderer");
constexpr float MAX_PREDICTION_TIME = 6.0f;
constexpr float TIME_STEP = 1.0f / 120.0f;
constexpr float GRAVITY = -650.0f;
// Get player position and velocity
const PlayerData& player = gs.players[index];
Vec pos = player.phys.pos;
Vec vel = player.phys.vel;
// Extrapolation variables
float time = 0.0f;
Vec extrapolatedPos = pos;
Vec surfacePoint = pos;
bool surfaceHit = false;
// Extrapolate until we hit a surface or reach max time
while (time < MAX_PREDICTION_TIME && !surfaceHit) {
// Update position and velocity
extrapolatedPos.x += vel.x * TIME_STEP;
extrapolatedPos.y += vel.y * TIME_STEP;
extrapolatedPos.z += vel.z * TIME_STEP + 0.5f * GRAVITY * TIME_STEP * TIME_STEP;
vel.z += GRAVITY * TIME_STEP;
// Check for collisions with surfaces
if (checkSurfaceCollision(extrapolatedPos)) {
surfacePoint = extrapolatedPos;
surfaceHit = true;
}
else {
time += TIME_STEP;
}
}
// Define Rocket League colors
rlbot::Color rocketLeagueBlue{ 0, 153, 255, 255 };
rlbot::Color rocketLeagueOrange{ 235, 141, 31, 255 };
rlbot::Color white{ 255, 255, 255, 255 };
// Determine color based on player's team
rlbot::Color playerColor = player.team == Team::BLUE ? rocketLeagueBlue : rocketLeagueOrange;
// Render line from player to surface point
rlbot::flat::Vector3 playerPos(
static_cast<float>(pos.x),
static_cast<float>(pos.y),
static_cast<float>(pos.z)
);
rlbot::flat::Vector3 surfacePos(
static_cast<float>(surfacePoint.x),
static_cast<float>(surfacePoint.y),
static_cast<float>(surfacePoint.z)
);
renderer.DrawLine3D(playerColor, playerPos, surfacePos);
// Render time to surface (higher and white)
std::stringstream timeSS;
timeSS << std::fixed << std::setprecision(2) << time << "s";
rlbot::flat::Vector3 textPos(
surfacePos.x(),
surfacePos.y(),
surfacePos.z() + 100.0f // Raise the text 100 units above the surface point
);
renderer.DrawString3D(timeSS.str(), white, textPos, 2.0f, 2.0f); // Increased size for better visibility
// Render information on screen
std::stringstream infoSS;
infoSS << "Time to surface: " << std::fixed << std::setprecision(2) << time << "s";
renderer.DrawString2D(infoSS.str(), playerColor, rlbot::flat::Vector3(10, 10, 0), 2.0f, 2.0f);
if (surfaceHit) {
std::stringstream surfaceSS;
surfaceSS << "Surface hit at: (" << std::fixed << std::setprecision(0)
<< surfacePoint.x << ", " << surfacePoint.y << ", " << surfacePoint.z << ")";
renderer.DrawString2D(surfaceSS.str(), playerColor, rlbot::flat::Vector3(10, 40, 0), 2.0f, 2.0f);
}
else {
renderer.DrawString2D("No surface hit within time limit", playerColor, rlbot::flat::Vector3(10, 40, 0), 2.0f, 2.0f);
}
}
fix this, when on a wal it thinks the closest exstraption point is the ground but thats false since there shouldent be calculated any exstrapolaiton if allready on a surface
To address the issue where the extrapolation incorrectly identifies the ground as the closest collision point when the player is already on a wall, you need to modify the behavior of your RenderTimeToSurface function. Specifically, you should prevent the extrapolation process from running if the player is already on a surface. This ensures that the bot does not mistakenly calculate a collision with the ground when the player is on a wall.
Here's a step-by-step guide to implement this fix:
Initial Collision Check:
Before starting the extrapolation loop, check if the player's current position is already on a surface using the checkSurfaceCollision function.
If the player is on a surface, skip the extrapolation entirely since there's no need to predict a collision with another surface.
Modify the RenderTimeToSurface Function:
Incorporate the initial collision check at the beginning of the function.
If the player is on a surface, you can choose to either:
Skip rendering the time to surface indicators.
Render a different message indicating that the player is already on a surface.
Ensure Accurate Collision Detection During Extrapolation:
If the player is not on a surface, proceed with the extrapolation as usual.
This prevents the bot from falsely detecting collisions with floors or ceilings when the player is already interacting with a wall.
Updated Code Implementation
Here's how you can modify your RenderTimeToSurface function to incorporate these changes:
cpp
bool checkSurfaceCollision(const Vec& pos) {
// Floor
if (pos.z <= 0) return true;
// Ceiling
if (pos.z >= 2044) return true;
// Side walls
if (abs(pos.x) >= 4096) return true;
// Back walls
if (abs(pos.y) >= 5120) return true;
// Simplified ramp check
if (abs(pos.y) > 5120 - 1152) {
float rampZ = (5120 - abs(pos.y)) * (642.775f / 1152.0f);
if (pos.z <= rampZ) return true;
}
return false;
}
void RLBotBot::RenderTimeToSurface(const RLGSC::GameState& gs) {
rlbot::ScopedRenderer renderer("time_to_surface_renderer");
constexpr float MAX_PREDICTION_TIME = 6.0f;
constexpr float TIME_STEP = 1.0f / 120.0f;
constexpr float GRAVITY = -650.0f;
// Get player position and velocity
const PlayerData& player = gs.players[index];
Vec pos = player.phys.pos;
Vec vel = player.phys.vel;
// Check if the player is already on a surface
if (checkSurfaceCollision(pos)) {
// Optionally, you can render a different message or indicator here
// For example, indicate that the player is on a surface
rlbot::Color white{ 255, 255, 255, 255 };
std::stringstream onSurfaceSS;
onSurfaceSS << "Player is on a surface";
renderer.DrawString2D(onSurfaceSS.str(), white, rlbot::flat::Vector3(10, 10, 0), 2.0f, 2.0f);
return; // Skip extrapolation since the player is already on a surface
}
// Extrapolation variables
float time = 0.0f;
Vec extrapolatedPos = pos;
Vec surfacePoint = pos;
bool surfaceHit = false;
// Extrapolate until we hit a surface or reach max time
while (time < MAX_PREDICTION_TIME && !surfaceHit) {
// Update position and velocity
extrapolatedPos.x += vel.x * TIME_STEP;
extrapolatedPos.y += vel.y * TIME_STEP;
extrapolatedPos.z += vel.z * TIME_STEP + 0.5f * GRAVITY * TIME_STEP * TIME_STEP;
vel.z += GRAVITY * TIME_STEP;
// Check for collisions with surfaces
if (checkSurfaceCollision(extrapolatedPos)) {
surfacePoint = extrapolatedPos;
surfaceHit = true;
}
else {
time += TIME_STEP;
}
}
// Define Rocket League colors
rlbot::Color rocketLeagueBlue{ 0, 153, 255, 255 };
rlbot::Color rocketLeagueOrange{ 235, 141, 31, 255 };
rlbot::Color white{ 255, 255, 255, 255 };
// Determine color based on player's team
rlbot::Color playerColor = player.team == Team::BLUE ? rocketLeagueBlue : rocketLeagueOrange;
// Render line from player to surface point
rlbot::flat::Vector3 playerPos(
static_cast<float>(pos.x),
static_cast<float>(pos.y),
static_cast<float>(pos.z)
);
rlbot::flat::Vector3 surfacePos(
static_cast<float>(surfacePoint.x),
static_cast<float>(surfacePoint.y),
static_cast<float>(surfacePoint.z)
);
renderer.DrawLine3D(playerColor, playerPos, surfacePos);
// Render time to surface (higher and white)
std::stringstream timeSS;
timeSS << std::fixed << std::setprecision(2) << time << "s";
rlbot::flat::Vector3 textPos(
surfacePos.x(),
surfacePos.y(),
surfacePos.z() + 100.0f // Raise the text 100 units above the surface point
);
renderer.DrawString3D(timeSS.str(), white, textPos, 2.0f, 2.0f); // Increased size for better visibility
// Render information on screen
std::stringstream infoSS;
infoSS << "Time to surface: " << std::fixed << std::setprecision(2) << time << "s";
renderer.DrawString2D(infoSS.str(), playerColor, rlbot::flat::Vector3(10, 10, 0), 2.0f, 2.0f);
if (surfaceHit) {
std::stringstream surfaceSS;
surfaceSS << "Surface hit at: (" << std::fixed << std::setprecision(0)
<< surfacePoint.x << ", " << surfacePoint.y << ", " << surfacePoint.z << ")";
renderer.DrawString2D(surfaceSS.str(), playerColor, rlbot::flat::Vector3(10, 40, 0), 2.0f, 2.0f);
}
else {
renderer.DrawString2D("No surface hit within time limit", playerColor, rlbot::flat::Vector3(10, 40, 0), 2.0f, 2.0f);
}
}
Explanation of Changes
Initial Collision Check:
Before performing any extrapolation, the function now checks if the player is already on a surface using checkSurfaceCollision(pos).
If the player is on a surface, the function renders a message indicating this status and returns early, skipping the extrapolation process.
Rendering an Informative Message:
When the player is on a surface, the bot renders a 2D string on the screen indicating that the player is already on a surface. This provides clear feedback without attempting to predict further collisions.
Preventing Incorrect Collision Detection:
By returning early when the player is on a surface, the bot avoids mistakenly identifying the ground as the nearest collision point during extrapolation when it's unnecessary.
Additional Recommendations
Enhanced Surface Identification:
If you need more detailed information about which specific surface the player is on (e.g., floor, ceiling, specific walls), consider modifying the checkSurfaceCollision function to return the type of surface collided with. This can help in rendering more precise indicators or making more informed decisions based on the surface type.
Visualization Enhancements:
Depending on your requirements, you might want to add different visual indicators based on whether the player is on the floor, ceiling, or specific walls. This can improve the usability and effectiveness of the rendering features.
Optimization:
Ensure that the collision checks and rendering operations are optimized to prevent any performance bottlenecks, especially if this function is called frequently during gameplay.
By implementing these changes, your bot will correctly handle scenarios where the player is already on a surface, avoiding incorrect extrapolation and rendering issues.