Trying to extract the trajectory ball from the AIControl app to a standalone UI app

Discussion in 'Programming' started by Ven Kiir, Feb 7, 2021.

  1. Ven Kiir

    Ven Kiir
    Expand Collapse

    Joined:
    Feb 24, 2015
    Messages:
    12
    In the AIControl UI app there's a debug mode that shows the trajectory of the vehicle, but the trajectory ball is only visible when the AI is controlling the vehicle. I want to be able to see the trajectory when I'm driving. I can't find the code for the AIControl app though, and once I do find it, would I be able to just extract the code for the trajectory ball into its own app?
    Disclaimer: I have no idea what I'm doing, I have some coding knowledge but no lua knowledge.
     
  2. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Edit: That trajectory ball that you want to draw can only come from AI data as that is what the AI calculated where it wants to go to.

    The code that draws the trajectory of the AI is located in BeamNG.drive\lua\vehicle\ai.lua and not in the app code, and so the app code calls functions from ai.lua. Here is the code (line 2171)

    Code:
    -- Debug Throttle brake application
    trajecRec = {last = 0} -- place this as a global variable
    
    local maxLen = 175
    local len = min(#trajecRec, maxLen)
    local last = trajecRec.last
    if len == 0 or (trajecRec[last][1] - aiPos:toFloat3()):length() > 0.25 then
    [INDENT]last = 1 + last % maxLen
    trajecRec[last] = {aiPos:toFloat3(), lastCommand.throttle, lastCommand.brake}
    len = min(len+1, maxLen)
    trajecRec.last = last[/INDENT]
    end
    
    local fl3 = float3(0.7, aiWidth, 0.7)
    for i = 1, len-1 do
    [INDENT]local n = trajecRec[1+(last+i)%len]
    debugDrawer:drawSquarePrism(trajecRec[1+(last+i-1)%len][1], n[1], fl3, fl3, color(255*sqrt(abs(n[3])), 255*sqrt(n[2]), 0, 100))[/INDENT]
    end
    You wouldn't be able to copy and paste this straight into a Lua file and expect it to work for your own non-AI vehicle though, since some of the functions only work with the AI. For example, you would need to change "aiPos:toFloat3()" to "obj:getFrontPosition()". The "lastCommand.throttle" and "lastCommand.brake" need to be changed to "electrics.values.throttle" and "electrics.values.brake" respectively. So here's my modified version of this to make it work:

    Code:
    local M = {}
    
    local max = math.max
    local min = math.min
    local sin = math.sin
    local asin = math.asin
    local pi = math.pi
    local abs = math.abs
    local sqrt = math.sqrt
    local floor = math.floor
    local tableInsert = table.insert
    local tableRemove = table.remove
    local strFormat = string.format
    
    local trajecRec = {last = 0}
    
    local function updateGFX(dt)
        local debugDrawer = obj.debugDrawProxy
      
        local pos = (vec3(obj:getFrontPosition()) - vec3(0,0,0.8)):toFloat3()
        local maxLen = 175
        local len = min(#trajecRec, maxLen)
        local last = trajecRec.last
        if len == 0 or (trajecRec[last][1] - pos):length() > 0.25 then
            last = 1 + last % maxLen
            trajecRec[last] = {pos, electrics.values.throttle, electrics.values.brake}
            len = min(len+1, maxLen)
            trajecRec.last = last
        end
    
        local width = 2
    
        local fl3 = float3(0.7, width, 0.7)
        for i = 1, len-1 do
            local n = trajecRec[1+(last+i)%len]
            debugDrawer:drawSquarePrism(trajecRec[1+(last+i-1)%len][1], n[1], fl3, fl3, color(255*sqrt(abs(n[3])), 255*sqrt(n[2]), 0, 100))
        end
    end
    
    M.updateGFX = updateGFX
     
    return M
    So paste this code into an empty Lua file and name it whatever. Then stick this file in your mod directory "mods\unpacked\your_mod_name\vehicles\etk800\lua" (this only shows trajectories for the etk800 vehicle and I'm not sure how to apply this to all without copying and pasting this file to all the vehicle folders) You won't need to call anything from other files to see this in action. Hope this helps :)
     
    #2 angelo234, Feb 8, 2021
    Last edited: Feb 8, 2021
  3. Ven Kiir

    Ven Kiir
    Expand Collapse

    Joined:
    Feb 24, 2015
    Messages:
    12
    Your code works great and I really appreciate you taking the time to do it, you're awesome, but that's not what I was looking for, and I'm now realizing what I'm looking for didn't actually exist to begin with. I was talking about the red ball that is out ahead of the vehicle when the trajectory debug is active. I had thought it was dynamic and showed where the car would be in the future, but I realized it's actually just following the ai path. I'm assuming the ball is the braking reference point.
    What I want is a ball that shows where the car would be a second in the future if all things stay the same, but I'm not that good at math let alone doing math in lua. I'll poke around in different bits of code and see if I can piece it together myself, but if anyone could help I would be extremely grateful! And thank you so much again @angelo234!
     
  4. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Lol at first I didn't know what "trajectory" ball you were talking about since it was being hidden by the car from my viewing perspective so I just assumed you were talking about the trajectory boxes/lines. But later I looked around more and saw it was right in front of the car and I just facepalmed since I could have easily answered your question. :oops:
     
  5. Ven Kiir

    Ven Kiir
    Expand Collapse

    Joined:
    Feb 24, 2015
    Messages:
    12
    Going to create a new thread to ask a more accurate question.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice