Rat32 Posted November 27, 2016 Share Posted November 27, 2016 (edited) 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 November 27, 2016 by Tasumi Link to comment
Addlibs Posted November 27, 2016 Share Posted November 27, 2016 (edited) 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 November 27, 2016 by MrTasty Link to comment
Rat32 Posted November 27, 2016 Author Share Posted November 27, 2016 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
Addlibs Posted November 27, 2016 Share Posted November 27, 2016 (edited) 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 November 27, 2016 by MrTasty Link to comment
Rat32 Posted November 27, 2016 Author Share Posted November 27, 2016 Okay, working, but how to get a ped bone (head) matrix using getElementMatrix? Link to comment
Mr.Loki Posted November 27, 2016 Share Posted November 27, 2016 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
Rat32 Posted November 28, 2016 Author Share Posted November 28, 2016 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now