Jump to content

getOffsetFromXYZ help


Rat32

Recommended Posts

Hello!

 

I have this code.

function getOffsetFromXYZ( mat, vec )
    -- make sure our matrix is setup correctly 'cos MTA used to set all of these to 1.
    mat[1][4] = 0
    mat[2][4] = 0
    mat[3][4] = 0
    mat[4][4] = 1
    mat = matrix.invert( mat )
    local offX = vec[1] * mat[1][1] + vec[2] * mat[2][1] + vec[3] * mat[3][1] + mat[4][1]
    local offY = vec[1] * mat[1][2] + vec[2] * mat[2][2] + vec[3] * mat[3][2] + mat[4][2]
    local offZ = vec[1] * mat[1][3] + vec[2] * mat[2][3] + vec[3] * mat[3][3] + mat[4][3]
    return {offX, offY, offZ}
end

addCommandHandler("ad",function()

        hx, hy, hz = getPedBonePosition(localPlayer, 
        playerMatrix = localPlayer:getMatrix()
        newPlayerMatrix = Matrix(Vector3(hx, hy, hz), playerMatrix.rotation)
        local matrix = localPlayer.matrix
        outputChatBox(tostring(matrix[1]))
        local offset = getOffsetFromXYZ(matrix, Vector3(getElementPosition(getPedOccupiedVehicle(localPlayer))))
        outputChatBox(tostring(offset.x))

end)

But when I type /ad i have error:

 

3: attempt to index field '?' (a nil value)

Edited by Tasumi
Link to comment

I'm pretty sure that that function takes in a different kind of matrix. Specifically, it expects a normal table of matrix data, retrievable via getElementMatrix, different from element:getMatrix(). The latter uses the new matrices class introduced with OOP.

Other than that, I would also like to state the fact that the calculation you're doing through this function will find coordinates pretty far away from the original coordinates. More specifically, the resultant position will be more or less twice as far from the centre of the map as hx, hy, hz.

Edited by MrTasty
Link to comment

I changed element:getMatrix() to getElementMatrix(). I have error in line 9.

ERROR:

 

9: attempt to perform arithmetic on field "?" (a nil value)

function getOffsetFromXYZ( mat, vec )
    -- make sure our matrix is setup correctly 'cos MTA used to set all of these to 1.
    mat[1][4] = 0
    mat[2][4] = 0
    mat[3][4] = 0
    mat[4][4] = 1
    mat = matrix.invert( mat )
    local offX = vec[1] * mat[1][1] + vec[2] * mat[2][1] + vec[3] * mat[3][1] + mat[4][1]
    local offY = vec[1] * mat[1][2] + vec[2] * mat[2][2] + vec[3] * mat[3][2] + mat[4][2]
    local offZ = vec[1] * mat[1][3] + vec[2] * mat[2][3] + vec[3] * mat[3][3] + mat[4][3]
    return {offX, offY, offZ}
end

addCommandHandler("ad",function()

		hx, hy, hz = getPedBonePosition(localPlayer, 8)
		playerMatrix = localPlayer:getMatrix()
		newPlayerMatrix = Matrix(Vector3(hx, hy, hz), playerMatrix.rotation)
		local matrix = localPlayer.matrix
		outputChatBox(tostring(matrix[1]))
		local offset = getOffsetFromXYZ(getElementMatrix(localPlayer), Vector3(getElementPosition(getPedOccupiedVehicle(localPlayer))))
		outputChatBox(tostring(offset.x))

end)

 

8 minutes ago, MrTasty said:

I'm pretty sure that that function takes in a different kind of matrix. Specifically, it expects a normal table of matrix data, retrievable via getElementMatrix, different from element:getMatrix(). The latter uses the new matrices class introduced with OOP.

Other than that, I would also like to state the fact that the calculation you're doing through this function will find coordinates pretty far away from the original coordinates. More specifically, the resultant position will be more or less twice as far from the centre of the map as hx, hy, hz.

 

Link to comment

Ah. Seems like the function requires a vector in the form of a table as well, not a Vector proper. You can change the function to the following and it should work with proper vectors, that is, those introduced with OOP.

function getOffsetFromXYZ( mat, vec )
    -- make sure our matrix is setup correctly 'cos MTA used to set all of these to 1.
    mat[1][4] = 0
    mat[2][4] = 0
    mat[3][4] = 0
    mat[4][4] = 1
    mat = matrix.invert( mat )
    local offX = vec.x * mat[1][1] + vec.y * mat[2][1] + vec.z * mat[3][1] + mat[4][1] -- }
    local offY = vec.x * mat[1][2] + vec.y * mat[2][2] + vec.z * mat[3][2] + mat[4][2] -- } changed vec[1] to vec.x, vec[2] to vec.y, vec[3] to vec.z
    local offZ = vec.x * mat[1][3] + vec.y * mat[2][3] + vec.z * mat[3][3] + mat[4][3] -- }
    return {offX, offY, offZ}
end

 

Edited by MrTasty
Link to comment

Just pointing this out...

Quote

Why not stick to the good ol' tables?

Say you'd like to get find the position underneath a vehicle. This is the position at any rotation. So if it was upside down, the Z value would be higher than the vehicle Z value. If the vehicle was the right way round, the Z value for underneath car would be less than the Z value for the car.


--
-- Instead of:
--
local matrix = getElementMatrix(vehicle)
local offX = 0 * matrix[1][1] + 0 * matrix[2][1] - 1 * matrix[3][1] + matrix[4][1]
local offY = 0 * matrix[1][2] + 0 * matrix[2][2] - 1 * matrix[3][2] + matrix[4][2]
local offZ = 0 * matrix[1][3] + 0 * matrix[2][3] - 1 * matrix[3][3] + matrix[4][3]

local pX, pY, pZ = getElementPosition(vehicle)
local positionBelow = {offX-pX, offY-pY, offZ-pZ}

--
-- You only have to do:
--
local positionBelow = vehicle.position - vehicle.matrix.up

 

Link to comment
21 hours ago, MrTasty said:

Ah. Seems like the function requires a vector in the form of a table as well, not a Vector proper. You can change the function to the following and it should work with proper vectors, that is, those introduced with OOP.


function getOffsetFromXYZ( mat, vec )
    -- make sure our matrix is setup correctly 'cos MTA used to set all of these to 1.
    mat[1][4] = 0
    mat[2][4] = 0
    mat[3][4] = 0
    mat[4][4] = 1
    mat = matrix.invert( mat )
    local offX = vec.x * mat[1][1] + vec.y * mat[2][1] + vec.z * mat[3][1] + mat[4][1] -- }
    local offY = vec.x * mat[1][2] + vec.y * mat[2][2] + vec.z * mat[3][2] + mat[4][2] -- } changed vec[1] to vec.x, vec[2] to vec.y, vec[3] to vec.z
    local offZ = vec.x * mat[1][3] + vec.y * mat[2][3] + vec.z * mat[3][3] + mat[4][3] -- }
    return {offX, offY, offZ}
end

 

 

It works, but returning...

 

-2.2737367544323e-13
0
0

 

Offset is bad.

 

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...