Looking to make a simple trajectory mod that shows where the vehicle will be a second in the future

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

  1. Ven Kiir

    Ven Kiir
    Expand Collapse

    Joined:
    Feb 24, 2015
    Messages:
    12
    I have no experience making beamng apps or writing lua scripts so I was hoping for some help. Basically what I want is a ui app that has a button to toggle a sphere or cylinder (or ideally an arc) that will show the approximate position of the vehicle one second in the future (doesn't have to specifically be one second, it could be variable). The reason I want a mod like this is because sometimes I like playing in slowmo so that I have superhuman reaction times, but in slow motion it's hard to predict if I'm braking enough for a corner or turning in too early or too late, so I want something to visually aid me.
    As I've said, I have no idea what I'm doing when it comes to beamng modding or lua scripting, so any help at all would be greatly appreciated
     
  2. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Alright so this time I'll try to be helpful lol. So you can find the trajectory/predicted position of the vehicle just by integrating the acceleration and velocity to find the relative position of an object after a given delta time and adding that to the current position. Although as delta time gets bigger, the error of the predicted trajectory gets larger. And to actually do the integration the following code would do just that:

    Code:
    local acc = vec3(obj:getSensorX(), obj:getSensorY(), obj:getSensorZ())
    local vel = vec3(obj:getVelocity())
    local pos = vec3(obj:getPosition())
    
    --Delta time (the time in the future to find the predicted position)
    local dt = 1
    
    --New Predicted Velocity after time dt
    local new_vel = acc * dt + vel
    
    --New Predicted Position after time dt
    local new_pos = new_vel * dt + pos
    And you can use debugDrawProxy:drawSphere(radius, position, color) for example to draw the new position as a sphere.

    Make a global function that contains all this code so that you can call it from the UI app code with the following function:
    bngApi.activeObjectLua('lua_function_name');

    Put this all into a .lua file in your mod directory for example for etk 800: "mods\unpacked\your_mod_name\vehicles\etk800\lua"

    And you want to call this global function on every game update.

    Anyways, you should look at other mods too for reference on making a UI app. But here's a link to creating a UI app that's pretty basic: https://wiki.beamng.com/My_First_App

    Good Luck!
     
    • Like Like x 2
  3. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,062
    Is there any reason why it only works on the ETK 800 specifically? Isn't it possible to just put it in the common LUA folder?
     
  4. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    From looking at other mods, it doesn't seem like you can share a single Lua file across all vehicles and I've tried putting it in a common/lua directory but to no avail. And so you literally have to copy and paste the file across every single vehicle directory if you want the Lua file to be executed by every single vehicle (look at the Torque Converter Clutch Lockup Switch mod for example).
     
  5. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,062
    Have you tried in lua/common instead? That's the directory where things like vehicle controller are
     
  6. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    You don't have to pack every mod file into a vehicle folder like vehicles/the_vehicle/and_so_on..

    You can also pack it like lua/vehicle/and_so_on.. then it should apply in every case a vehicle is spawned (?)
    --- Post updated ---
    vehicle controller is in lua/vehicle/controller/ :)
     
  7. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,062
    Oh, good to know. I'm pretty sure there's something I used in my mods that is in lua/common though, maybe the light bar controller?
     
  8. Ven Kiir

    Ven Kiir
    Expand Collapse

    Joined:
    Feb 24, 2015
    Messages:
    12
    Thanks! I'm definitely going to need it. I don't even know how to run what you posted without throwing an error but I'm determined! I can figure this out! Maybe!
    Again thank you so much @angelo234, you've been really awesome helping me out
     
  9. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    Nope, lightbar.lua also resides in lua/vehicle/controller :cool:

    The particle system (and settings through json file) sit in the common folder though..
     
  10. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,062
    Wait now I remember, the unfinished mod by me is there lol, that's why I remembered the location. The mod for explosion-based jump system for cars. (Unfinished because it requires precise ajustment of thrusters per suspension settings and can be completely different for 2 configs of the same vehicle, so it makes no sense to release it without configs, so it will just be a part of a big mod pack by me and my friend.)
     
  11. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Unfortunately none of those directories make it work. I've tried:
    • lua/common
    • lua/common/controller
    • lua/vehicle/
    • lua/vehicle/controller
    • vehicles/common/lua
    • vehicles/common/lua/controller
    • vehicles/lua/controller
    • vehicles/lua
    What sort of error are you getting?
     
  12. Ven Kiir

    Ven Kiir
    Expand Collapse

    Joined:
    Feb 24, 2015
    Messages:
    12
    A message appears in the in game message box whenever I spawn the ETK800 that says "This is often caused by broken mods or corrupt core files." followed by links to instructions for starting in safe mode and doing a clean install. The ETK won't appear but the nodes are still there if you hold ctrl, trying to spawn it again softlocks the game
     
  13. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Press '~' to open up the console and send me a screenshot of the error message and send me what code you have written up so far.
     
  14. Ven Kiir

    Ven Kiir
    Expand Collapse

    Joined:
    Feb 24, 2015
    Messages:
    12
    upload_2021-2-9_16-7-25.png

    Code:
    local M = {}
    local acc = vec3(obj:getSensorX(), obj:getSensorY(), obj:getSensorZ())
    local vel = vec3(obj:getVelocity())
    local pos = vec3(obj:getPosition())
    
    --Delta time (the time in the future to find the predicted position)
    local dt = 1
    
    --New Predicted Velocity after time dt
    local new_vel = acc * dt + vel
    
    --New Predicted Position after time dt
    local new_pos = new_vel * dt + pos
    debugDrawer:drawSphere(0.25, new_pos, color(0,0,255,255))
    return M
    It runs without the drawsphere bit, I tried the drawSphere you suggested first and then when that didn't work I tried one I found in the ai app, also didn't work.
    Bear in mind that I don't know lua so I expect there's at least 50 things I did wrong/didn't do. I'm trying to learn on the fly but that's really not working out for me. Should probably watch some beginner lua tutorials.......
     
  15. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Try
    Code:
    obj.debugDrawProxy:drawSphere(0.25, new_pos:toFloat3(), color(0,0,255,255))
    instead and see if that works. Also put all that code into a global lua function, so that you can call it multiple times from your UI app:

    Code:
    local M = {}
    
    function drawTrajectory()
        --code goes here
    end
    
    return M
    --- Post updated ---
    But if you just want to test what you got right now if you don't have UI app code, then I guess you can test it with the updateGFX function (basically a function that gets called every time the game updates the graphics I guess).

    Code:
    local M = {}
    
    local function updateGFX(gfx_dt)
        local acc = vec3(obj:getSensorX(), obj:getSensorY(), obj:getSensorZ())
        local vel = vec3(obj:getVelocity())
        local pos = vec3(obj:getPosition())
    
        --Delta time (the time in the future to find the predicted position)
        local dt = 1
    
        --New Predicted Velocity after time dt
        local new_vel = acc * dt + vel
    
        --New Predicted Position after time dt
        local new_pos = new_vel * dt + pos
      
        obj.debugDrawProxy:drawSphere(0.25, new_pos:toFloat3(), color(0,0,255,255))
    end
    
    M.updateGFX = updateGFX
    
    return M

    --- Post updated ---
    Okay so I have finally tested the code and now realize that the acceleration vector is local to the car and not the world so here is the updated code:

    Code:
    local M = {}
    
    local function updateGFX(gfx_dt)
        local debugDrawer = obj.debugDrawProxy
      
        local acc = -vec3(obj:getSensorX(), obj:getSensorY(), obj:getSensorZ())
      
        --Rotate local acc vector by direction of car in global space
        acc = quatFromDir(vec3(obj:getDirectionVector()), vec3(0,0,1)) * acc
      
        local vel = vec3(obj:getVelocity())
        local pos = vec3(obj:getPosition())
    
        --Delta time (the time in the future to find the predicted position)
        local dt = 0.5
    
        --New Predicted Velocity after time dt
        local new_vel = acc * dt + vel
    
        --New Predicted Position after time dt
        local new_pos = new_vel * dt + pos
      
        debugDrawer:drawSphere(0.25, new_pos:toFloat3(), color(0,0,255,255))
    end
    
    M.updateGFX = updateGFX
    
    return M
    --- Post updated ---
    If you are looking for better trajectory predicting, then I would suggest using different integration methods such as "Velocity Verlet" or even the "4th Order Runge Kutta Method (RK4)". But FYI those are a little bit trickier to implement. Here's a website that describes how to implement these if your interested:
    https://jdickinsongames.wordpress.com/2015/01/22/numerical-integration-in-games-development-2/
     
    #15 angelo234, Feb 10, 2021
    Last edited: Feb 10, 2021
    • Like Like x 2
  16. Ven Kiir

    Ven Kiir
    Expand Collapse

    Joined:
    Feb 24, 2015
    Messages:
    12
    Hey, it works! You're seriously the best. I really need to learn how to do little mods like this, any recommendations for lua tutorials with relevant info for beamng modding?
     
  17. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Lol I'm afraid there aren't many tutorials on making mods in BeamNG using Lua nor is the current documentation that great. So how I've learned (kind of) to make mods in BeamNG is just by looking at other people's mods and experimenting. I guess you could try looking at the game's Lua source code or UI app code but it's a bit overwhelming lol.

    Also one tip I've got is that if you want to figure out all the functions/fields of an object/class, use the "dump(object_of_interest)" function. If that gives you the result like "<userdata>" for example, then use "dump(getmetatable(object_of_interest))" and that may work. And if you find a function/field that looks of interest, then you can just search for that in the Lua directory in the installation folder for the game (BeamNG.drive\lua) to find out how to use it, like what parameters it needs and what it returns.

    Anyways, here's a couple of links of BeamNG's documentation:
    https://documentation.beamng.com/programming/index.html
    https://wiki.beamng.com/Lua:Reference
    https://wiki.beamng.com/Streams
     
    • Like Like x 1
  18. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    • lua/common not recommended, or indeed no 'hook' reserved
    • lua/common/controller doesn't exists
    • lua/vehicle/ here you could try to put it in lua/vehicle/extensions (?)
    • lua/vehicle/controller if you want to set up a controller it should be defined
    local M = {}
    M.type = "auxiliary"

    and also defined in the jbeam file(s) under controller section, this would make it vehicle dependent again of course :)

    • vehicles/common/lua probably no 'hook' reserved for lua thru the 'common' folder
    • vehicles/common/lua/controller same as above
    • vehicles/lua/controller doesn't exists
    • vehicles/lua same as above, no 'hooks' for general vehicles folder to lua
     
  19. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    "lua/vehicle/extensions" doesn't work either :(
     
  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