Intrailturn12345

Pasted Nov 26, 2026, 5:15 AM1 file262 lines

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/intrailturn12345
Intrailturn12345

bool DetectLeaderTurn(
    const RouteComp& route,
    glm::vec3& incoming,
    glm::vec3& outgoing,
    glm::vec3& turnPoint,
    float& turnAngle,
    float& turnDirection)
{
    int index = route.currwaypointindex;

    if (index < 1 ||
        index + 1 >= static_cast<int>(route.waypoints.size()))
    {
        return false;
    }

    glm::vec3 previous =
        route.waypoints[index - 1].position;

    turnPoint =
        route.waypoints[index].position;

    glm::vec3 next =
        route.waypoints[index + 1].position;

    incoming = turnPoint - previous;
    outgoing = next - turnPoint;

    incoming.y = 0.0f;
    outgoing.y = 0.0f;

    if (glm::length(incoming) < 0.001f ||
        glm::length(outgoing) < 0.001f)
        return false;

    incoming = glm::normalize(incoming);
    outgoing = glm::normalize(outgoing);

    turnAngle = CalculateTurnAngle(incoming, outgoing);

    turnDirection = CalculateTurnDirection(
        incoming,
        outgoing
    );

    // Ignore straight segments and near-180 degree reversals.
    if (turnAngle < glm::radians(5.0f) ||
        turnAngle > glm::radians(175.0f))
        return false;

    return true;
}




glm::vec3 CalculateTurnEntryPoint(
    const glm::vec3& turnPoint,
    const glm::vec3& incomingDirection,
    float turnRadius,
    float turnAngle)
{
    float tangentDistance =
        turnRadius * std::tan(turnAngle * 0.5f);

    return turnPoint -
           incomingDirection * tangentDistance;
}

glm::vec3 CalculateTurnExitPoint(
    const glm::vec3& turnPoint,
    const glm::vec3& outgoingDirection,
    float turnRadius,
    float turnAngle)
{
    float tangentDistance =
        turnRadius * std::tan(turnAngle * 0.5f);

    return turnPoint +
           outgoingDirection * tangentDistance;
}




void IntrailTurn(BTContext& ctx)
{
    if (!ctx.agent.has<TransformComp>()) return;
    if (!ctx.agent.has<VelocityComp>()) return;
    if (!ctx.agent.has<LocomotionComp>()) return;
    if (!ctx.agent.has<ActionQueueComp>()) return;
    if (!ctx.agent.has<IntrailTurnComp>()) return;

    if (ctx.agent.parent() == NULL) return;

    auto parent = ctx.agent.parent();

    if (!parent.has<FormationComp>()) return;

    auto& formation = parent.get<FormationComp>();

    if (formation.vecAircrafts.empty()) return;

    auto& world =
        EntityManager::GetInstance()->GetWorld();

    auto& leader =
        world.entity(formation.vecAircrafts[0].flecsId);

    if (!leader.is_alive()) return;

    if (!leader.has<RouteComp>()) return;

    auto& route = leader.get<RouteComp>();

    auto& state =
        ctx.agent.get_mut<IntrailTurnComp>();

    auto& transform =
        ctx.agent.get<TransformComp>();

    auto& velocity =
        ctx.agent.get<VelocityComp>();

    auto& loco =
        ctx.agent.get<LocomotionComp>();

    auto& leaderTransform =
        leader.get<TransformComp>();

    // ------------------------------------------------
    // 1. Detect upcoming turn
    // ------------------------------------------------

    glm::vec3 incoming;
    glm::vec3 outgoing;
    glm::vec3 turnPoint;

    float turnAngle = 0.0f;
    float turnDirection = 0.0f;

    bool turnFound = DetectLeaderTurn(
        route,
        incoming,
        outgoing,
        turnPoint,
        turnAngle,
        turnDirection
    );

    if (!turnFound)
        return;

    // Avoid processing the same waypoint repeatedly.
    if (state.processedWaypointIndex ==
        route.currwaypointindex)
    {
        return;
    }

    // ------------------------------------------------
    // 2. Calculate turn radius
    // ------------------------------------------------

    const float bankAngle =
        glm::radians(30.0f); // Configurable aircraft parameter.

    float speed = velocity.speed;

    float turnRadius =
        CalculateTurnRadius(speed, bankAngle);

    if (turnRadius <= 0.0f)
        return;

    // ------------------------------------------------
    // 3. Calculate turn entry and exit points
    // ------------------------------------------------

    glm::vec3 entryPoint =
        CalculateTurnEntryPoint(
            turnPoint,
            incoming,
            turnRadius,
            turnAngle
        );

    glm::vec3 exitPoint =
        CalculateTurnExitPoint(
            turnPoint,
            outgoing,
            turnRadius,
            turnAngle
        );

    // ------------------------------------------------
    // 4. Store turn information
    // ------------------------------------------------

    state.turnWaypoint = turnPoint;
    state.outgoingDirection = outgoing;
    state.turnAngle = turnAngle;
    state.turnDirection = turnDirection;
    state.turnRadius = turnRadius;

    state.initiationDistance =
        glm::distance(entryPoint, turnPoint);

    state.processedWaypointIndex =
        route.currwaypointindex;

    state.state = IntrailState::TURN_APPROACH;

    state.initialized = true;

    // ------------------------------------------------
    // 5. Check whether wingman reached turn entry
    // ------------------------------------------------

    glm::vec3 aircraftPosition =
        transform.position;

    float distanceToEntry =
        glm::distance(aircraftPosition, entryPoint);

    if (distanceToEntry > 100.0f)
        return;

    // ------------------------------------------------
    // 6. Execute turn
    // ------------------------------------------------

    state.state = IntrailState::TURN_EXECUTION;

    float heading = std::atan2(
        outgoing.x,
        outgoing.z
    );

    Waypoint destination = {
        Waypoint::NAV,
        exitPoint,
        0
    };

    auto& queue =
        ctx.agent.get_mut<ActionQueueComp>().pending;

    queue.push_back(
        new MoveCommand(
            destination,
            heading,
            transform.position.y,
            true,
            false
        )
    );

    state.state = IntrailState::TURN_COMPLETE;
}

5,965 chars · codepastes.com