Satsuma Odometer

Discussion in 'Programming' started by mike94, May 19, 2017.

  1. mike94

    mike94
    Expand Collapse

    Joined:
    Mar 7, 2015
    Messages:
    155
    I managed to make my first piece of Lua code, the new odometer for my Satsuma and it gives distance in meters. It works fine, but I want to make the kilometer indicator to rotate a bit after every 1000 meters. This current code makes it to rotate all the time and it looks very unrealistic. This line makes the kilometer indicator to rotate a bit when the car reaches 1000 meters but then it stops for good:

    if distanceTotalm >= 1000 then distanceTotalkm = distanceTotalkm + 1 end

    I tried to google this problem for hours but couldn't find anything. I don't understand how to make it advance a bit after every 1000 meters.

    Here is the whole Lua:
    local M = {}
    local distancem = 0
    local distanceTotalm = 0
    local distanceTotalkm = 0

    local function onInit()
    electrics.values['odo'] = 0
    electrics.values['odokm'] = 0
    end

    local function reset()
    onInit()
    end

    local function updateGFX(dt)
    distancem = electrics.values.wheelspeed * dt
    distanceTotalm = distancem + distanceTotalm
    if distanceTotalm >= 1000 then distanceTotalkm = distanceTotalkm + 1 end
    electrics.values['odo'] = distanceTotalm
    electrics.values['odokm'] = distanceTotalkm
    end

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

    return M
     
  2. metalmuncher

    metalmuncher
    Expand Collapse

    Joined:
    Aug 6, 2012
    Messages:
    257
    From what I understand, you are trying to make the km part "click" onto the next integer km when it travels over the next 1000m, I guess currently there is some kind of fractional part making it continuously rotate?

    This should give you an integer km number that increments when you rack up over 1000m:
    distanceTotalkm = math.floor(distanceTotalm/1000)
     
  3. mike94

    mike94
    Expand Collapse

    Joined:
    Mar 7, 2015
    Messages:
    155
    Yes the "click" thing was exactly what I was talking about. I'll try your code in the afternoon. That current code won't rotate the km counter constantly, but it turns it one "click" and that's it.
    I'm really a noob writing lua code. I'll see if resetting the meter counter after every 1000m affects how the km counter works.
     
  4. mike94

    mike94
    Expand Collapse

    Joined:
    Mar 7, 2015
    Messages:
    155
    Thanks very much @metalmuncher. The odometer works now as it should.:D I read about the math.floor thing yesterday but I didn't know it can be used for moving the counter in steps.
     
  5. metalmuncher

    metalmuncher
    Expand Collapse

    Joined:
    Aug 6, 2012
    Messages:
    257
    So I've had a bit of a play with the mod, and thought it looked a little weird that the 100m roller has a nice smooth animation and all the others pop into existence, so I made a quick change that makes it look as if the lower count rollers "pull" the next one over when they roll over to zero. To be honest, I don't have much experience with a mechanical odometer, so they might not work like this at all, but I thought it looked cool. :p
    Quick vid to show how this looks:


    Code:
    local M = {}

    local distanceTotalm = 0

    local function onInit()
    electrics.values['odo'] = 0
    electrics.values['odokm'] = 0
    electrics.values['odo10km'] = 0
    electrics.values['odo100km'] = 0
    electrics.values['odo1000km'] = 0
    electrics.values['odo10000km'] = 0
    end

    local function reset()
    onInit()
    end

    local function updateGFX(dt)
    distanceTotalm = distanceTotalm + electrics.values.wheelspeed * dt

    electrics.values['odo'] = distanceTotalm * 0.36 % 360
    electrics.values['odokm'] = (math.floor(distanceTotalm/1000) + math.max((((distanceTotalm % 1000)-900)/100),0)) * 36 % 360
    electrics.values['odo10km'] = (math.floor(distanceTotalm/10000) + math.max((((distanceTotalm % 10000)-9900)/100),0)) * 36 % 360
    electrics.values['odo100km'] = (math.floor(distanceTotalm/100000) + math.max((((distanceTotalm % 100000)-99900)/100),0)) * 36 % 360
    electrics.values['odo1000km'] = (math.floor(distanceTotalm/1000000) + math.max((((distanceTotalm % 1000000)-999900)/100),0)) * 36 % 360
    electrics.values['odo10000k'] = (math.floor(distanceTotalm/10000000) + math.max((((distanceTotalm % 10000000)-9999900)/100),0)) * 36 % 360
    end

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

    return M
    I've also simplified it a bit, according to the perf debug this seems to take about 0.06-0.08ms less lua time, but I am not a programmer, so take this with a pinch of salt. :D
     
  6. mike94

    mike94
    Expand Collapse

    Joined:
    Mar 7, 2015
    Messages:
    155
    Sorry. I didn't have an alert of new message and noticed this only now. Thanks for this. Now the code is perfect and the rotation looks just like in a real odometer. (I know, because I have five 80's/early 90's cars and they all have mechanical odometer)

    I'll update the lua when the the pickup update is ready.
     
  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