1. Trouble with the game?
    Try the troubleshooter!

    Dismiss Notice
  2. Issues with the game?
    Check the Known Issues list before reporting!

    Dismiss Notice
  3. Before reporting issues or bugs, please check the up-to-date Bug Reporting Thread for the current version.
    0.30 Bug Reporting thread
    Solutions and more information may already be available.

Bug with groundMarkers.lua code

Discussion in 'Troubleshooting: Bugs, Questions and Support' started by angelo234, Sep 18, 2021.

  1. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    I was trying to use the getPathLength function in groundMarkers.lua for a mod I'm creating but it wasn't working. And I found two bugs with the code (doesn't seem like it was even tested...). One issue is that the sqrt function isn't a global function and should be calling math.sqrt instead, but this doesn't fix the problem and calling this function shouldn't be needed. The biggest issue is that the formula for calculating the path length is wrong since:

    sqrt(a^2 + b^2) != a + b

    So the code should change from:
    Code:
    local function getPathLength()
      local sqrDist = 0
      local path = M.routePlanner.path
      local pathLength = tableSize(path)
      for i = 1, (pathLength - 1) do
        sqrDist = sqrDist + (path[i + 1].pos - path[i].pos):squaredLength()
      end
      return sqrt(sqrDist)
    end
    to:
    Code:
    local function getPathLength()
      local dist = 0
      local path = M.routePlanner.path
      local pathLength = tableSize(path)
      for i = 1, (pathLength - 1) do
        dist = dist + (path[i + 1].pos - path[i].pos):length()
      end
      return dist
    end
     
  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