Salchy Posted July 14, 2023 Share Posted July 14, 2023 Hello writers, good morning. I would like to know more about getElementMatrix, how does it work? What does it mean the data is what is returned to me? According to what I read on the wiki, it returns 4 'tables', with coordinates, but what does each of them mean? Might be interesting for more of an explanation on how matrices work in this game. We can take as an example, a vehicle. local vehicle = createVehicle(411, 1892, -2418, 13.5) setElementFrozen(vehicle, true) for k, v in pairs(getElementMatrix(vehicle)) do outputChatBox("index "..k.." = "..v[1]..", "..v[2]..", "..v[3]..", "..v[4]) end --[[ Return: index 1 = 1, -1.7484555314695e-07, 1.7484555314695e-07, 1 index 2 = 1.7484558156866e-07, 1, -1.7484555314695e-07, 1 index 3 = -1.7484552472524e-07, 1.7484558156866e-07, 1, 1 index 4 = 1892, -2418, 13.5, 1 ]] index 4 by deduction gives me the proper position of the vehicle, and what are the rest of the values? For more information, what I'm trying to do, is to always get the underbody of a vehicle. They told me that I could get it with this function, but without saying anything else. I'm doing math with getElementBoundingBox and getGroundPosition, but I can't get much of it right now, since my coords are getting out of phase when the vehicle is rotating. Perhaps with an explanation of what those values are, I can get closer to the result I'm looking for. Sorry for the bad grammar, I'm using google translate. Link to comment
Moderators IIYAMA Posted July 14, 2023 Moderators Share Posted July 14, 2023 1 hour ago, Salchy said: but what does each of them mean? A matrix is a mixture of a position and orientation(rotation). With as main purpose able to calculating offset. For example you want to place a ramp in front of a vehicle, a matix helps you with that. But you do NOT need to know how a matrix works, don't waste your time XD. I prefer to use the Matrix class(without oop), instead of getElementMatrix: local x, y, z = getElementPosition(vehicle) local xr, yr, zr = getElementRotation(vehicle) local vehicleMatix = Matrix (Vector3(x, y, z), Vector3(xr, yr, zr)) -- build your matrix with position and rotation local offset = Vector3(0, 0, 0) -- fill in! local newPosition = matrix:transformPosition(offset) iprint(newPosition.x, newPosition.y, newPosition.z) See docs: https://wiki.multitheftauto.com/wiki/Matrix Utility function: function getMatrixFromElement (element) return Matrix (Vector3(getElementPosition(element)), Vector3(getElementRotation(element))) end Link to comment
Popular Post DiSaMe Posted July 15, 2023 Popular Post Share Posted July 15, 2023 13 hours ago, IIYAMA said: But you do NOT need to know how a matrix works, don't waste your time XD. I disagree. I needed to understand matrices, figuring out how they work (examples in getElementMatrix wiki page were an important part of that) was one of my major achievements because it enabled me to do calculations in 3D that I hadn't been able to before then. It's up to each person to decide if they need it or not. It was definitely not a waste of time for me so I wouldn't make such strong assumptions. 16 hours ago, Salchy said: According to what I read on the wiki, it returns 4 'tables', with coordinates, but what does each of them mean? Might be interesting for more of an explanation on how matrices work in this game. As you have noticed, the 4th table represents the position. As for the others - they represent rotation and scale. More specifically, the 3x3 part of the matrix (that is, from matrix[1][1] to matrix[3][3]) is what you would call a "rotation matrix", which tells the directions of the object's coordinate system with respect to the world's coordinate system. matrix[1] tells where the object's X direction is pointing to, matrix[2] tells Y and matrix[3] tells Z. This allows you to do transformation of points from one coordinate system to another, using addition/subtraction and multiplication operations. For example, if you want to calculate the absolute coordinates of a point that's 0.5 units to the right (X) and 2 units to the front (Y) from the car, the calculations would go like this: absolute_position = 0.5*car_x_direction + 2*car_y_direction + car_position Or, by including the Z direction for completeness, we can also express it like this: absolute_position = 0.5*car_x_direction + 2*car_y_direction + 0*car_z_direction + car_position Basically the relative coordinates are (0.5, 2, 0), and by multiplying each component by vector of respective direction of the car and putting them together, you get the offset from the car in world's coordinate system, then by adding the car's position you get the absolute position. Written in plain Lua, with calculations done separately on each component, it would look like this: local absolute_position = { 0.5*matrix[1][1] + 2*matrix[2][1] + 0*matrix[3][1] + matrix[4][1], 0.5*matrix[1][2] + 2*matrix[2][2] + 0*matrix[3][2] + matrix[4][2], 0.5*matrix[1][3] + 2*matrix[2][3] + 0*matrix[3][3] + matrix[4][3] } That's the kind of calculation that happens inside matrix:transformPosition when using MTA's Matrix and Vector3 classes. It's up to you which one you use. Using MTA's classes helps you keep your code concise, while doing the calculations yourself allows you to reuse the math code anywhere else you use Lua, not just in MTA. As for your specific case - getElementBoundingBox returns the bounds relative to the element. So you can use them to choose a point in car's coordinate system and then transform it using the matrix. To elaborate a bit more regarding the matrix values in your example - notice how, if we round the small values to zero (they are non-zero because of some numerical inaccuracies to begin with), the 3x3 part looks like this: matrix[1] = {1, 0, 0} -- X axis (the right side), matches the world's X direction (east) matrix[2] = {0, 1, 0} -- Y axis (the front), matches the world's Y direction (north) matrix[3] = {0, 0, 1} -- Z axis (the top), matches the world's Z direction (top) Which demonstrates that since your car is not rotated, each of your car's X, Y and Z direction points to the respective direction in the world's coordinates. If you rotated the car clockwise by 90 degrees, the values would be different: matrix[1] = {0, -1, 0} -- X axis (the right side), matches the world's -Y direction (south) matrix[2] = {1, 0, 0} -- Y axis (the front), matches the world's X direction (east) matrix[3] = {0, 0, 1} -- Z axis (the top), matches the world's Z direction (top) I hope this explanation helps you in case you want to understand better how matrices work. And if you don't care what happens under the hood, then it's okay too, you can just use Matrix and Vector3 classes with transformPosition method. 4 Link to comment
Salchy Posted July 16, 2023 Author Share Posted July 16, 2023 Thanks a lot for your answers guys. Yes, the idea was to understand how it works and what those values are, to know what can be done with it. I see this opens up a world of immense possibilities, makes it much easier to do offset calculations. Thank you very much DiSaMe, I appreciate your time and your explanation, very clear. You can close the topic. Link to comment
DiSaMe Posted July 16, 2023 Share Posted July 16, 2023 You're welcome, I'm glad that was helpful. Link to comment
Moderators Vinyard Posted July 16, 2023 Moderators Share Posted July 16, 2023 Closing based on OP's request. Link to comment
Recommended Posts