A Lua script to decrease sound depends on distance

  • #1, by MachtnixThursday, 31. December 2015, 18:21 9 years ago

    proudly present my first working Lua script, it's only a "test balloon" to learn how this f... Lua works :-)

    It's not very "elegant". Now - like a novel - I have to short and summarize.

    I have a huge scrolling scene with my fairy walking and flying in the maze. Somewhere is a waterfall making noise. Usually the noise's loudness is the same at every scene point even the waterfall couldn't be seen, and I don't like this. How to decrease the sound if the fairy is far away?

    At first I need two points: the position of the fairy and the position of the waterfall. At the beginning I tried this with sinus functions, but it doesn't work. Not mine... ;-) I remembered this old school thing called "Pythagoras". Yeah! That's it. Very complicated, but fine. I can offer a detailed scribble later.

    After hours I get out the right orders to read the positions. It should be more comfortable to have a better dokumentation... ;-)

    Now I have the distance as the crow flies between the fairy and the waterfall checked every 100 ms. Less than a 100ms loop the player wants 100% CPU power and freezes...

    Then I need to control the sound volume. That's tricky and I haven't understood yet, which order is the correct one and why. I can't get the values of the scene sound to show or print out, but I can manipulate them: setValue(VSceneMusicVolume). If the distance is zero or nearby (fairy at the same point as the waterfall) the noise get the highest value, depends on the volume of the sound file. If the distance exceeds a defined mark the sound volume is zero.

    The shorter the distance the louder the sound. That's all.

    All this control prints will be delete later. After minutes the log file has a huge size of 200 pages....

    -- My first sript ever :-)
    -- Das Script wird beim Beginn der Kulisse aufgerufen, danach 100ms Pause und schließlich ununterbrochen geloopt (Aktion: zwei Schritte zurück)
    --Test: Lautstärken? Sind alle 100% 
    Musik_Laut = getVolume(eMusicVolume)
    Szene_Musik = getVolume(eSoundVolume)
    Global_Laut = getVolume(eGlobalVolume)
    -- Kontrollausgaben
    print ("Musiklautstärke", Musik_Laut)
    print ("Szene-Musik", Szene_Musik)
    print ("Global_Laut", Global_Laut)
    -- Kulissenobjekt holen
    Kulisse = getObject("Scenes[Irrgarten]")
    -- Objekte "Elfe" und "Wasserfall" holen
    local Obj_Wasserfall = getObject("Objects[Wasserfall]")
    local Obj_Elfe = getObject("Characters[Elfe]")
    -- Positionen von "Elfe" und "Wasserfall" holen
    local Pos_Elfe = getObject("Characters[Elfe]"):getPoint(VCharacterPosition)
    local Pos_Wasserfall = getObject("Objects[Wasserfall]"):getPoint(VObjectPosition)
    -- Kontrollausgaben 
    print ("Position der Elfe", Pos_Elfe.x, Pos_Elfe.y) 
    print ("Position des Wasserfalls", Pos_Wasserfall.x, Pos_Wasserfall.y) 
    -- Variablenzuweisung (muss nicht sein, ist aber kürzer)
    x1 = Pos_Elfe.x
    y1 = Pos_Elfe.y
    x2 = Pos_Wasserfall.x
    y2 = Pos_Wasserfall.y
    -- Abfrage der vier Quadranten im Koordinatensystem (Vergleich der x- und y-Werte von "Elfe" und "Wasserfall")
    -- das geht sicher einfacher, aber so klappt es wenigstens
    -- hey, den ollen Pythagoras aus der Schule braucht man tatsächlich mal! :-)
    if x1 >= x2 then
    	if y1 <= y2 then
    		h = math.sqrt (((y2 - y1) ^2) + ((x1-x2)^2))
    		print ("Wegstrecke: ", h) --Kontrolle
    	if y1 > y2 then
    		h = math.sqrt (((y1 - y2) ^2) + ((x1-x2)^2))
    		print (h) -- Kontrolle
    if x1 < x2 then
    	if  y1 <= y2 then
    		h = math.sqrt (((y2 - y1) ^2) + ((x2-x1)^2))
    		print (h) -- Kontrolle
    	if y1 > y2 then
    		h = math.sqrt (((y1 - y2) ^2) + ((x2-x1)^2))
    		print (h) --Kontrolle
    -- empirische Bereichswerte für diese Kulissengröße, abhängig von der Lautstärke des Files. Besser wäre eine nichtlineare Funktion o.ä.. 
    local s = math.floor (800 / h)  -- Integer-Wert
    print ("Soundstärke", s) -- Kontrolle
    -- Hier wird die Soundlautstärke s abhängig von der Distanz h gesetzt
    Kulisse:setValue(VSceneMusicVolume, s)
    -- klappt! Je weiter weg die Figur, desto leiser der Sound
    -- Warum manchmal ein V oder ein e vor die Funktion gehört, muss ich noch herausfinden.


    Happy New Year!

    Thread Captain

    1097 Posts

  • #2, by tristan-kangThursday, 31. December 2015, 21:33 9 years ago
    I've been trying this one but my method was just set different sound by character's position via action areas.

    Well... it was just object, not moving character.

    Happy New Year as well.

    Great Poster

    267 Posts