Jump to content

[EN/BR] How to calculate coordinates in front of an element


vicisdev

Recommended Posts

ENGLISH

Let's say I have a player and I want to spawn a car in front of it with a specific distance away from the player. It must considers the player's rotation. It involves some math calculations, but I can't formulate it.

BRASIL

Vamos supor que eu tenha um player e eu queira spawnar um carro na frente dele com uma distância específica. Para fazer isso, deve levar em consideração a rotação atual do player. Eu sei que isso envolve cálculos matemáticos, mas não consegui formulá-los.

Thanks all

Link to comment

 

https://wiki.multitheftauto.com/wiki/GetElementMatrix

function getPositionFromElementOffset(element,offX,offY,offZ)
    local m = getElementMatrix ( element )  -- Get the matrix
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  -- Apply transform
    local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2]
    local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3]
    return x, y, z                               -- Return the transformed point
end

 

  • Thanks 1
Link to comment
1 hour ago, Space_Unicorn said:

 

https://wiki.multitheftauto.com/wiki/GetElementMatrix


function getPositionFromElementOffset(element,offX,offY,offZ)
    local m = getElementMatrix ( element )  -- Get the matrix
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  -- Apply transform
    local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2]
    local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3]
    return x, y, z                               -- Return the transformed point
end

 

Uuuuh wee! It works like a charm! Thank you!

  • Like 1
Link to comment
function getFrontPosition(element,dist)
    local x, y, z = getElementPosition(element)
    local _,_,r = getElementRotation(element)
    local tx = x + - ( dist ) * math.sin( math.rad( r ) )
    local ty = y + dist * math.cos( math.rad( r ) )
    local tz = z
    return tx,ty,tz
end

 

Edited by Avival
  • Like 2
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...