1
0

VehicleMatrix.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Made by Gamer_Z
  3. Provides vehicle offset functions and vehicle matrix functions for easier workflow with positioning tied to vehicle rotation.
  4. LICENSE:
  5. Copyright (c) 2013, Rafal "Gamer_Z" Grasman
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  8. -Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  9. -Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  10. -Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  11. Special Clause for SA-MP Servers:
  12. -The authors and or contributors must be credited in a visible and accessible area for the player, including but not limited to: about boxes, welcome messages on the server, commands displaying messages and/or message boxes
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <a_samp>
  25. enum MatrixParts
  26. {
  27. mp_PITCH,
  28. mp_ROLL,
  29. mp_YAW,
  30. mp_POS
  31. };
  32. enum MatrixIndicator
  33. {
  34. Float:mi_X,
  35. Float:mi_Y,
  36. Float:mi_Z
  37. };
  38. /*
  39. Get Corresponding GTA {{Internal}} Vehicle Matrix
  40. */
  41. //aka quaternion to matrix conversion + including position
  42. stock GetVehicleMatrix(vehicleid,Mat4x3[MatrixParts][MatrixIndicator])
  43. {
  44. //initial processing step - gathering information
  45. new
  46. Float:x,
  47. Float:y,
  48. Float:z,
  49. Float:w,
  50. Float:Pos[3];
  51. GetVehicleRotationQuat(vehicleid,w,x,y,z);
  52. GetVehiclePos(vehicleid,Pos[0],Pos[1],Pos[2]);
  53. //initialized math for quaternion to matrix conversion {for sake of simplicity and efficiency}
  54. new
  55. Float:x2 = x * x,
  56. Float:y2 = y * y,
  57. Float:z2 = z * z,
  58. Float:xy = x * y,
  59. Float:xz = x * z,
  60. Float:yz = y * z,
  61. Float:wx = w * x,
  62. Float:wy = w * y,
  63. Float:wz = w * z;
  64. //the maths required to convert a quat to a matrix
  65. Mat4x3[mp_PITCH][mi_X] = 1.0 - 2.0 * (y2 + z2);
  66. Mat4x3[mp_PITCH][mi_Y] = 2.0 * (xy - wz);
  67. Mat4x3[mp_PITCH][mi_Z] = 2.0 * (xz + wy);
  68. Mat4x3[mp_ROLL][mi_X] = 2.0 * (xy + wz);
  69. Mat4x3[mp_ROLL][mi_Y] = 1.0 - 2.0 * (x2 + z2);
  70. Mat4x3[mp_ROLL][mi_Z] = 2.0 * (yz - wx);
  71. Mat4x3[mp_YAW][mi_X] = 2.0 * (xz - wy);
  72. Mat4x3[mp_YAW][mi_Y] = 2.0 * (yz + wx);
  73. Mat4x3[mp_YAW][mi_Z] = 1.0 - 2.0 * (x2 + y2);
  74. //the gta vehicle matrix has the position in it, I want it to keep just like GTA does so I put the position in
  75. Mat4x3[mp_POS][mi_X] = Pos[0];
  76. Mat4x3[mp_POS][mi_Y] = Pos[1];
  77. Mat4x3[mp_POS][mi_Z] = Pos[2];
  78. return 1;
  79. }
  80. /*
  81. Get position offset at position corresponding to the correct vehicle rotation
  82. */
  83. stock PositionFromVehicleOffset(vehicle,Float:offX,Float:offY,Float:offZ,&Float:x,&Float:y,&Float:z)
  84. {
  85. //initial processing step - gather information
  86. new
  87. Mat4x3[MatrixParts][MatrixIndicator];
  88. GetVehicleMatrix(vehicle,Mat4x3);
  89. //offset calculation math
  90. x = offX * Mat4x3[mp_PITCH][mi_X] + offY * Mat4x3[mp_ROLL][mi_X] + offZ * Mat4x3[mp_YAW][mi_X] + Mat4x3[mp_POS][mi_X];
  91. y = offX * Mat4x3[mp_PITCH][mi_Y] + offY * Mat4x3[mp_ROLL][mi_Y] + offZ * Mat4x3[mp_YAW][mi_Y] + Mat4x3[mp_POS][mi_Y];
  92. z = offX * Mat4x3[mp_PITCH][mi_Z] + offY * Mat4x3[mp_ROLL][mi_Z] + offZ * Mat4x3[mp_YAW][mi_Z] + Mat4x3[mp_POS][mi_Z];
  93. return 1;
  94. }