Setting Sound-Emitter position & velocity

Discussion in 'Programming' started by Grille, Jul 5, 2022.

  1. Grille

    Grille
    Expand Collapse

    Joined:
    Aug 17, 2019
    Messages:
    71
    Hi

    Is it possible with Lua scripts to set the position of a sound source, and ideally also set some source velocity value (to get doppler effect right)?

    I would like to add some planes to my map, and having e.g. an Cessna fly 20m over you’re head in complete silence seems a bit anticlimactic xD
     
  2. vulcan-dev

    vulcan-dev
    Expand Collapse

    Joined:
    Jan 28, 2017
    Messages:
    245
    Hello, it is possible :)
    All you need to do is find that object and then set the position (matrix4f)

    Code:
    -- function I use for finding objects
    local function getObject(className, preferredObjName)
        local objNames = scenetree.findClassObjects(className)
        if objNames and tableSize(objNames) > 0 then
            for _, name in pairs(objNames) do
                local obj = scenetree.findObject(name)
                if obj and (name == preferredObjName or not preferredObjName) then -- not sure why I did this
                    return obj
                end
            end
        else
            local obj = scenetree.findObject(className)
            if obj then return obj end
        end
    
        return nil
    end
    
    -- table to matrix
    local function tableToMatrix(tbl)
        local mat = MatrixF(true)
        mat:setColumn(0, tbl.c0)
        mat:setColumn(1, tbl.c1)
        mat:setColumn(2, tbl.c2)
        mat:setColumn(3, tbl.c3)
        return mat
    end
    
    -- getting the sound object
    local soundObj = getObject("something")
    if not soundObj then return end
    
    -- setting the position, you will need to understand matrices
    soundObj.position = tableToMatrix({
        c0 = vec3(0, 0, 0), -- fill all these with your own values, same with the 3 below
        c1 = vec3(0, 0, 0),
        c2 = vec3(0, 0, 0),
        c3 = vec3(0, 0, 0),
    })
    
    -- if you want to get the current position, you can do:
    print(soundObj.position:getColumn(3)) -- this will get "c3", if you want to get the x position of that column, just add ".x" to it
    
    As for velocity, I'm not actually sure
     
  3. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,320
    Setting the position does not influence the sound position, the emitter needs to be restarted after being moved.


    Also, just a heads up, you can easily find elements by their name by doing this:
    Code:
    scenetree.findObject("object name")
    
    You can also find objects by their ID:
    Code:
    scenetree.findObjectById(object id)
    
    I don't see the need to write a custom function like you did.

    Getting/Setting the position is just as simple, and can be done by using vec3:
    Code:
    local pos = scenetree.findObject("object name"):getPosition()
    
    scenetree.findObject("object name"):setPosition(x, y, z)
    
     
  4. vulcan-dev

    vulcan-dev
    Expand Collapse

    Joined:
    Jan 28, 2017
    Messages:
    245
    I created this function quite a while ago because something wasn't working right, whatever it is I can't remember but scenetree.findObject works for me now, thanks.
    Also, since you know a bit about sound emitters, do you know why setting the volume via lua doesn't update it?
    --- Post updated ---
    Oh, I remember why now. It's so I could also find classes at the same time, I'll make it so if it doesn't find the object, search the classes for it instead.
     
  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