-- This Source Code Form is subject to the terms of the bCDDL, v. 1.1.
-- If a copy of the bCDDL was not distributed with this
-- file, You can obtain one at http://beamng.com/bCDDL-1.1.txt

local M = {}

local frontNode = nil
local rearNode = nil
local smoother = newExponentialSmoothing(200)

local function onInit()
  for k,v in pairs(v.data.nodes) do
    if v.name == "fusf" then frontNode = v end
    if v.name == "fus15b" then rearNode = v end
  end
end

local function onReset()
	onInit()
end

local function updateGFX(dt)
  local airspeed = obj:getVelocity():length()
  
  -- above 20km/h
  if airspeed > 5.5 then
    local velo = vec3(obj:getVelocity()):normalized()
    local dir = vec3(obj:getDirectionVector()):normalized()
    local slipAmount = math.acos(velo:dot(dir))
    
    if dir.x < 0 then slipAmount = slipAmount * -1 end
    
    electrics.values["slipIndicator"] = smoother:get(slipAmount)
  else
    electrics.values["slipIndicator"] = smoother:get(0)
  end
end

-- public interface
M.onInit      = onInit
M.onReset     = onReset
M.updateGFX = updateGFX

return M