Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

void RLBotBot::RenderTimeToReachBall(const RLGSC::GameState& gs) { rlbot::ScopedRenderer…

  1. User

    void RLBotBot::RenderTimeToReachBall(const RLGSC::GameState& gs) { rlbot::ScopedRenderer renderer("time_to_reach_ball");

    const auto& localPlayer = gs.players[index];
    const Vec& ballPos = gs.ball.pos;
    
    auto calculateMinAirTime = [](const Vec& playerPos, const Vec& playerVel, bool hasFlip) {
        constexpr float GRAVITY = 650.0f;
        // Calculate time to reach ground
        float timeToGround = RS_MAX(0.0f, (-playerVel.z + std::sqrt(playerVel.z * playerVel.z + 2 * GRAVITY * playerPos.z)) / GRAVITY);
        // Calculate time to reach side walls
        float timeToSideWall = (playerVel.x != 0) ? RS_MIN(
            std::abs((CommonValues::SIDE_WALL_X - playerPos.x) / playerVel.x),
            std::abs((-CommonValues::SIDE_WALL_X - playerPos.x) / playerVel.x)
        ) : std::numeric_limits<float>::infinity();
        // Calculate time to reach back walls
        float timeToBackWall = (playerVel.y != 0) ? RS_MIN(
            std::abs((CommonValues::BACK_WALL_Y - playerPos.y) / playerVel.y),
            std::abs((-CommonValues::BACK_WALL_Y - playerPos.y) / playerVel.y)
        ) : std::numeric_limits<float>::infinity();
        // Calculate time to reach ceiling
        float timeToCeiling = (playerVel.z > 0) ? (CommonValues::CEILING_Z - playerPos.z) / playerVel.z : std::numeric_limits<float>::infinity();
        // Return the minimum time to reach any surface
        return RS_MIN(RS_MIN(timeToGround, timeToSideWall), RS_MIN(timeToBackWall, timeToCeiling));
        };
    
    auto getTimeToReachBall = [&calculateMinAirTime](const PlayerData& player, const Vec& ballPos, std::vector<Vec>& path) {
        constexpr float MAX_ROTATION_SPEED = 5.5f;
        constexpr float DIST_REACH_THRESH = 10.0f;
        constexpr float STEP_INTERVAL = 1.0f / 60.0f;
        constexpr float FLIP_FORCE = 500.0f;
        constexpr float AIR_ACCEL = 66.66667f;
        constexpr float BOOST_ACCEL_AIR = 1058.33333f;
        constexpr float MAX_TOTAL_SPEED = 2300.0f;
        constexpr float BREAK_ACCEL = 3500.0f;
        constexpr float BOOST_ACCEL_GROUND = 991.66667f;
        constexpr float DRIVE_ACCEL = 1600.0f;
        constexpr float MAX_DRIVE_SPEED = 1410.0f;
    
        const Vec& playerPos = player.phys.pos;
        const Vec& playerForward = player.phys.rotMat.forward;
        const Vec& playerVel = player.phys.vel;
        bool hasFlip = player.carState.HasFlipOrJump();
        float dist = (ballPos - playerPos).Length();
        Vec dirToBall = (ballPos - playerPos) / dist;
        Vec normalizedDirToBall = dirToBall / dirToBall.Length();
        float curVel = playerVel.Dot(normalizedDirToBall);
        float angleDiff = std::acos(RS_CLAMP(playerForward.Dot(normalizedDirToBall), -1.0f, 1.0f));
        float rotTime = angleDiff / MAX_ROTATION_SPEED;
        float t = 0;
        float curDist = dist;
    
        path.push_back(playerPos);
        float nextPointTime = 0.2f;
    
        // Calculate minimum air time
        float minAirTime = calculateMinAirTime(playerPos, playerVel, hasFlip);
    
        // Air movement
        while (t < minAirTime && curDist > DIST_REACH_THRESH) {
            bool hasRotated = t >= rotTime;
            if (hasFlip && t == 0) {
                curVel += FLIP_FORCE;
            }
            float airAccel = AIR_ACCEL + (BOOST_ACCEL_AIR * hasRotated);
            curVel += airAccel * STEP_INTERVAL;
            curVel = RS_MIN(curVel, MAX_TOTAL_SPEED);
            curDist -= curVel * STEP_INTERVAL;
            t += STEP_INTERVAL;
    
            if (t >= nextPointTime) {
                path.push_back(playerPos + normalizedDirToBall * (dist - curDist));
                nextPointTime += 0.2f;
            }
        }
    
        // Ground movement
        while (curDist > DIST_REACH_THRESH) {
            bool hasRotated = t >= rotTime;
            if (curVel < 0) {
                curVel += (BREAK_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;
            }
            else {
                float remainingTime = curDist / curVel;
                t += remainingTime;
                path.push_back(ballPos);
                return t;
            }
            curDist -= curVel * STEP_INTERVAL;
            t += STEP_INTERVAL;
    
            if (t >= nextPointTime) {
                path.push_back(playerPos + normalizedDirToBall * (dist - curDist));
                nextPointTime += 0.2f;
            }
        }
        return t;
        };
    
    std::vector<Vec> path;
    float timeToReach = getTimeToReachBall(localPlayer, ballPos, path);
    
    // Render the time estimate as text above the ball
    std::stringstream ss;
    ss << std::fixed << std::setprecision(2) << "Time to reach: " << timeToReach << "s";
    rlbot::flat::Vector3 textPos = {
        static_cast<float>(ballPos.x),
        static_cast<float>(ballPos.y),
        static_cast<float>(ballPos.z + 200)
    };
    renderer.DrawString3D(ss.str(), rlbot::Color::white, textPos, 1.0f, 1.0f);
    
    // Render points and times along the path
    for (size_t i = 0; i < path.size(); ++i) {
        const Vec& pointPos = path[i];
        float t = i * 0.2f;
    
        rlbot::flat::Vector3 renderPos = {
            static_cast<float>(pointPos.x),
            static_cast<float>(pointPos.y),
            static_cast<float>(pointPos.z)
        };
    
        // Render a small rectangle at this point
        renderer.DrawRect3D(rlbot::Color::yellow, renderPos, 10, 10, true);
    
        // Render the time at this point
        std::stringstream pointSs;
        pointSs << std::fixed << std::setprecision(1) << t << "s";
        rlbot::flat::Vector3 textPos = {
            static_cast<float>(pointPos.x),
            static_cast<float>(pointPos.y),
            static_cast<float>(pointPos.z + 50)
        };
        renderer.DrawString3D(pointSs.str(), rlbot::Color::yellow, textPos, 1.0f, 1.0f);
    }
    
    // Draw the path
    std::vector<rlbot::flat::Vector3> points;
    std::vector<const rlbot::flat::Vector3*> pointPtrs;
    for (const auto& point : path) {
        points.push_back({ static_cast<float>(point.x), static_cast<float>(point.y), static_cast<float>(point.z) });
    }
    for (const auto& point : points) {
        pointPtrs.push_back(&point);
    }
    renderer.DrawPolyLine3D(rlbot::Color::yellow, pointPtrs);
    

    } why is the max time 2 sec fix the code to make the max time the time too the ball then make the 0.2 second points between that

  2. Assistant

    The issue you're facing stems from the way the times t are calculated and associated with the path points. In your current implementation, when rendering the points and times along the path, you're assuming that each point corresponds to t = i * 0.2f. This assumption only holds if the total time to reach the ball is a multiple of 0.2s and if the time intervals between points are constant, which may not be the case.

    Here's how you can fix the code:

    1. Modify the Path to Store Times with Positions: Instead of storing only the positions in the path vector, store pairs of time and position. This way, each point in the path is associated with the actual time it takes to reach that point.

      Change the type of path from std::vector<Vec> to std::vector<std::pair<float, Vec>>.

    2. Update getTimeToReachBall Function: In the getTimeToReachBall function, whenever you add a new point to the path, include the current time t along with the position. Replace all instances where you push back a position to the path with a pair of {t, position}.

      For example, change:

      path.push_back(playerPos);
      

      to

      path.push_back({ t, playerPos });
      

      And similarly for other instances where you modify the path within your air and ground movement loops.

    3. Adjust the Rendering Loop: When you're rendering the points and times along the path, use the stored times from the path instead of calculating t as i * 0.2f. This ensures that the times are accurate and correspond to the actual simulation.

      Update your rendering loop as follows:

      for (size_t i = 0; i < path.size(); ++i) {
          const float t = path[i].first;
          const Vec& pointPos = path[i].second;
      
          // Render a small rectangle at this point
          rlbot::flat::Vector3 renderPos = {
              static_cast<float>(pointPos.x),
              static_cast<float>(pointPos.y),
              static_cast<float>(pointPos.z)
          };
          renderer.DrawRect3D(rlbot::Color::yellow, renderPos, 10, 10, true);
      
          // Render the time at this point
          std::stringstream pointSs;
          pointSs << std::fixed << std::setprecision(1) << t << "s";
          rlbot::flat::Vector3 textPos = {
              static_cast<float>(pointPos.x),
              static_cast<float>(pointPos.y),
              static_cast<float>(pointPos.z + 50)
          };
          renderer.DrawString3D(pointSs.str(), rlbot::Color::yellow, textPos, 1.0f, 1.0f);
      }
      
    4. Ensure Consistency in Polyline Rendering: When you draw the path using renderer.DrawPolyLine3D, make sure you're using the updated path that includes the positions.

      Update the code as follows:

      std::vector<rlbot::flat::Vector3> points;
      std::vector<const rlbot::flat::Vector3*> pointPtrs;
      for (const auto& [t, point] : path) {
          points.push_back({ static_cast<float>(point.x), static_cast<float>(point.y), static_cast<float>(point.z) });
      }
      for (const auto& point : points) {
          pointPtrs.push_back(&point);
      }
      renderer.DrawPolyLine3D(rlbot::Color::yellow, pointPtrs);
      

    By making these changes, you ensure that the maximum time corresponds to the actual time to reach the ball, and the points along the path are correctly spaced according to the simulation timings, not just fixed at 0.2 second intervals.

    Here is the corrected code snippet:

    void RLBotBot::RenderTimeToReachBall(const RLGSC::GameState& gs) {
        rlbot::ScopedRenderer renderer("time_to_reach_ball");
    
        const auto& localPlayer = gs.players[index];
        const Vec& ballPos = gs.ball.pos;
    
        auto calculateMinAirTime = [](const Vec& playerPos, const Vec& playerVel, bool hasFlip) {
            // ... (unchanged code)
        };
    
        auto getTimeToReachBall = [&calculateMinAirTime](const PlayerData& player, const Vec& ballPos, std::vector<std::pair<float, Vec>>& path) {
            // ... (constants definitions)
    
            const Vec& playerPos = player.phys.pos;
            // ... (other variable initializations)
    
            float t = 0;
            float curDist = dist;
    
            path.push_back({ t, playerPos });
            float nextPointTime = 0.2f;
    
            // Calculate minimum air time
            float minAirTime = calculateMinAirTime(playerPos, playerVel, hasFlip);
    
            // Air movement
            while (t < minAirTime && curDist > DIST_REACH_THRESH) {
                // ... (movement calculations)
    
                t += STEP_INTERVAL;
    
                if (t >= nextPointTime) {
                    path.push_back({ t, playerPos + normalizedDirToBall * (dist - curDist) });
                    nextPointTime += 0.2f;
                }
            }
    
            // Ground movement
            while (curDist > DIST_REACH_THRESH) {
                // ... (movement calculations)
    
                t += STEP_INTERVAL;
    
                if (t >= nextPointTime) {
                    path.push_back({ t, playerPos + normalizedDirToBall * (dist - curDist) });
                    nextPointTime += 0.2f;
                }
            }
            return t;
        };
    
        std::vector<std::pair<float, Vec>> path;
        float timeToReach = getTimeToReachBall(localPlayer, ballPos, path);
    
        // Render the time estimate as text above the ball
        std::stringstream ss;
        ss << std::fixed << std::setprecision(2) << "Time to reach: " << timeToReach << "s";
        rlbot::flat::Vector3 textPos = {
            static_cast<float>(ballPos.x),
            static_cast<float>(ballPos.y),
            static_cast<float>(ballPos.z + 200)
        };
        renderer.DrawString3D(ss.str(), rlbot::Color::white, textPos, 1.0f, 1.0f);
    
        // Render points and times along the path
        for (size_t i = 0; i < path.size(); ++i) {
            const float t = path[i].first;
            const Vec& pointPos = path[i].second;
    
            rlbot::flat::Vector3 renderPos = {
                static_cast<float>(pointPos.x),
                static_cast<float>(pointPos.y),
                static_cast<float>(pointPos.z)
            };
    
            // Render a small rectangle at this point
            renderer.DrawRect3D(rlbot::Color::yellow, renderPos, 10, 10, true);
    
            // Render the time at this point
            std::stringstream pointSs;
            pointSs << std::fixed << std::setprecision(1) << t << "s";
            rlbot::flat::Vector3 textPos = {
                static_cast<float>(pointPos.x),
                static_cast<float>(pointPos.y),
                static_cast<float>(pointPos.z + 50)
            };
            renderer.DrawString3D(pointSs.str(), rlbot::Color::yellow, textPos, 1.0f, 1.0f);
        }
    
        // Draw the path
        std::vector<rlbot::flat::Vector3> points;
        std::vector<const rlbot::flat::Vector3*> pointPtrs;
        for (const auto& [t, point] : path) {
            points.push_back({ static_cast<float>(point.x), static_cast<float>(point.y), static_cast<float>(point.z) });
        }
        for (const auto& point : points) {
            pointPtrs.push_back(&point);
        }
        renderer.DrawPolyLine3D(rlbot::Color::yellow, pointPtrs);
    }
    

    By implementing these changes, the maximum time displayed and the points along the path will accurately reflect the actual time it takes for the bot to reach the ball, and the points will be correctly spaced at 0.2 second intervals up to that time.