Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 15/07/23 in all areas

  1. 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. 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.
    3 points
  2. Hi, welcome to the forums! Your topic has been moved to the Arabic section so you can get better assistance in your native language. Make sure to always use English when posting outside this language-specific section!
    1 point
  3. The problem was the gaps. I wrote it in a single row without tabs, spaces and numbers and now its working so thank you very much
    1 point
  4. Please show the JSON, that is the easiest way of checking why the conversion is failing. Also don't use the following array formatting: { [1] = {}, [2] = {} } Keep it clean, no gaps if possible, if you do have gaps add a false value or an empty table: { {}, {} } The conversion between Lua and JSON is rather sensitive if you do not follow the JavaScript Object Notion rules. Another golden rule: Don't mix keys of different key types, it is either strings or integer each (sub)table. If one of them is a string, all of them become strings. There is no such thing as a table in JavaScript, there is only an default object(key strings) or an array(key integers).
    1 point
×
×
  • Create New...