Jump to content

the relative coordinates


LLIEPLLIEHb

Recommended Posts

Greetings, programmers. 
Faced with an incomprehensible task for me.
The essence is this - you need to find the x,y,z coordinate of the listener's point on the sound source, but the listener's coordinate does not belong to the vector of the sound source. 

Simply put, we need the relative coordinates of the listener from the point of the sound source, taking into account rotation (sound.rotZ)  
Example:

an example of what I mean

 

 addEventHandler("onClientRender",root,function()
    listener =
        {
            fullPosition = localPlayer.matrix:getPosition(),
            matrix = localPlayer.matrix,
            player = localPlayer,
            x =      localPlayer.matrix:getPosition().x,
            y =      localPlayer.matrix:getPosition().y,
            z =      localPlayer.matrix:getPosition().z,
            rotZ =   localPlayer.matrix:getRotation().z,
            distance = 1.5,
        } --{x,y,z,rotZ}
end)


local sound =
    {
        x =-1788.82507,
        y=-2689.26538,
        z=4.26150,
        vector3 = Vector3(-1788.82507,-2689.26538,4.26150),
        rotZ =180+90,
        distance = 3.5
    }


addEventHandler("onClientRender",root,function() 
    martNewPos =
        {
        soundRotate = math.ceil(sound.rotZ),
        Pos = {x = 0, y = 0,z = 0} -- как тут найти позицию слушателя на векторе источника звука при этом учитывая rotZ источника звука
        }
    local dist  = getDistanceBetweenPoints2D ( sound.x,sound.y,listener.x,listener.y ) -- дистанция между двумя точками
    local coords = { getScreenFromWorldPosition ( listener.x,listener.y,listener.z ) }
    if coords[1] and coords[2] then
        dxDrawText ("\n"..inspect(martNewPos), coords[1], coords[2], coords[1], coords[2], tocolor(255,255,255), 2, "unifont" )
    end
end)

addEventHandler("onClientRender",root,function() -- рендер графики точки звука
    local coords = { getScreenFromWorldPosition ( sound.x,sound.y,sound.z ) }
    if coords[1] and coords[2] then
        dxDrawText ( "Источник звука", coords[1], coords[2], coords[1], coords[2], tocolor(255,255,255), 2, "unifont" )
    end
    local vehicleUpwardsVector, vehicleForwardVector = listener.matrix:getUp(), listener.matrix:getForward()
    local crossVector = {Vector3(0,0,0)}
    dxDrawLine3D(sound.vector3, sound.x-math.sin(math.rad(sound.rotZ))*sound.distance,sound.y+math.cos(math.rad(sound.rotZ))*sound.distance,sound.z, tocolor(255, 0, 0))
    local sx, sy = getScreenFromWorldPosition(sound.x-math.sin(math.rad(sound.rotZ))*sound.distance,sound.y+math.cos(math.rad(sound.rotZ))*sound.distance,sound.z)
    if sx then
        dxDrawText("Z", sx, sy)
    end
    local vehicleUpwardsVectorPoint = sound.vector3 + vehicleUpwardsVector*sound.distance
    dxDrawLine3D(sound.vector3, vehicleUpwardsVectorPoint, tocolor(0, 255, 0))
    local sx, sy = getScreenFromWorldPosition(vehicleUpwardsVectorPoint)
    if sx then
        dxDrawText("Y", sx, sy)
    end
    dxDrawLine3D(sound.vector3, sound.x-math.sin(math.rad(sound.rotZ-90))*sound.distance,sound.y+math.cos(math.rad(sound.rotZ-90))*sound.distance,sound.z, tocolor(0, 0, 255))
    local sx, sy = getScreenFromWorldPosition(sound.x-math.sin(math.rad(sound.rotZ-90))*sound.distance,sound.y+math.cos(math.rad(sound.rotZ-90))*sound.distance,sound.z)
    if sx then
        dxDrawText("X", sx, sy)
    end
end)

addEventHandler("onClientRender",root,function() -- рендер графики точки Слушателя
    local coords = { getScreenFromWorldPosition ( listener.x,listener.y,listener.z ) }
    if coords[1] and coords[2] then
        dxDrawText ( "Слушатель", coords[1], coords[2], coords[1], coords[2], tocolor(255,255,255), 2, "unifont" )
    end
    local vehicleUpwardsVector, vehicleForwardVector = listener.matrix:getUp(), listener.matrix:getForward()
    local crossVector = vehicleForwardVector:cross(vehicleUpwardsVector):getNormalized()
    dxDrawLine3D(listener.fullPosition, listener.x-math.sin(math.rad(listener.rotZ))*listener.distance,listener.y+math.cos(math.rad(listener.rotZ))*listener.distance,listener.z, tocolor(255, 0, 0))
    local sx, sy = getScreenFromWorldPosition(listener.x-math.sin(math.rad(listener.rotZ))*listener.distance,listener.y+math.cos(math.rad(listener.rotZ))*listener.distance,listener.z)
    if sx then
        dxDrawText("Z", sx, sy)
    end
    local vehicleUpwardsVectorPoint = listener.fullPosition + vehicleUpwardsVector*listener.distance
    dxDrawLine3D(listener.fullPosition, vehicleUpwardsVectorPoint, tocolor(0, 255, 0))
    local sx, sy = getScreenFromWorldPosition(vehicleUpwardsVectorPoint)
    if sx then
        dxDrawText("Y", sx, sy)
    end
    dxDrawLine3D(listener.fullPosition, listener.x-math.sin(math.rad(listener.rotZ-90))*listener.distance,listener.y+math.cos(math.rad(listener.rotZ-90))*listener.distance,listener.z, tocolor(0, 0, 255))
    local sx, sy = getScreenFromWorldPosition(listener.x-math.sin(math.rad(listener.rotZ-90))*listener.distance,listener.y+math.cos(math.rad(listener.rotZ-90))*listener.distance,listener.z)
    if sx then
        dxDrawText("X", sx, sy)
    end
end)

-- setTimer ( function() -- вращение источника звука
--     if sound.rotZ >= 360 then
--         sound.rotZ = 0
--     end
--     sound.rotZ = sound.rotZ+.25
-- end, 1, 0 )

 

Link to comment

To find the relative coordinates of the listener from the point of the sound source, you can use the following steps:

 

Calculate the distance between the listener's position and the sound source's position using the getDistanceBetweenPoints2D function.

Calculate the angle between the listener's position and the sound source's position by getting the angle between the two points using math.atan2(y2-y1, x2-x1) 

Rotate the angle by the sound source's rotation (sound.rotZ) using math.rad(sound.rotZ)

Use this angle and the distance to calculate the relative x, y, and z coordinates of the listener from the sound source using math.cos(angle)*distance for the x coordinate and math.sin(angle)*distance for the y coordinate.

Use the resulting x, y, and z coordinates to position the listener on the sound source's vector.

 

here is an example of how the relative coordinates of the listener can be calculated using the steps I outlined above

local listener = {x = localPlayer.matrix:getPosition().x, y = localPlayer.matrix:getPosition().y, rotZ = localPlayer.matrix:getRotation().z}
local sound = {x = -1788.82507, y = -2689.26538, z = 4.26150, rotZ = 180 + 90}
local distance = getDistanceBetweenPoints2D ( sound.x, sound.y, listener.x, listener.y )
local angle = math.atan2(listener.y - sound.y, listener.x - sound.x) + math.rad(sound.rotZ)
local listenerRelativeX = math.cos(angle) * distance
local listenerRelativeY = math.sin(angle) * distance
local listenerRelativePos = {x = sound.x + listenerRelativeX, y = sound.y + listenerRelativeY, z = listener.z}

 

This code calculates the relative position of the listener from the sound source using the distance and angle between the listener and sound source, and the rotation of the sound source. The resulting position is stored in the listenerRelativePos variable, which contains the x, y, and z coordinates of the listener relative to the sound source.

Note that this code is missing the calculation of the z coordinate, you need to add the calculation of the z coordinate based on the requirement of your game or application.

  • Thanks 1
Link to comment

Thank you very much for your knowledge. I applied your code and it really works flawlessly. But I made a fatal mistake on my part - by the fact that I need to get coordinates relative to the listener itself, and not the sound source.

I'll attach the video below. where is the relative calculation from the sound source :) In the future I will redo and make the correct calculations

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...