Advertisement
Southclaw

For later

Jan 22nd, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.25 KB | None | 0 0
  1. // From: http://gpb.googlecode.com/svn/trunk/includes/VehicleMatrix.inc
  2.  
  3. /*
  4.      Made by Gamer_Z
  5.         Provides vehicle offset functions and vehicle matrix functions for easier workflow with positioning tied to vehicle rotation.
  6.  
  7.     LICENSE:
  8.  
  9.     Copyright (c)  2013, Rafal "Gamer_Z" Grasman
  10.  
  11.     All rights reserved.
  12.  
  13.     Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  14.  
  15.         -Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  16.         -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.
  17.         -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.
  18.  
  19.         Special Clause for SA-MP Servers:
  20.         -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
  21.  
  22.     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23.     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  26.     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR  CONSEQUENTIAL
  27.     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28.     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29.     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30.     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31.     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <a_samp>
  34.  
  35. enum MatrixParts
  36. {
  37.     mp_PITCH,
  38.     mp_ROLL,
  39.     mp_YAW,
  40.     mp_POS
  41. };
  42.  
  43. enum MatrixIndicator
  44. {
  45.     Float:mi_X,
  46.     Float:mi_Y,
  47.     Float:mi_Z
  48. };
  49.  
  50. /*
  51.     Get Corresponding GTA {{Internal}} Vehicle Matrix
  52. */
  53. //aka quaternion to matrix conversion + including position
  54. stock GetVehicleMatrix(vehicleid,Mat4x3[MatrixParts][MatrixIndicator])
  55. {
  56. //initial processing step - gathering information
  57.     new
  58.         Float:x,
  59.         Float:y,
  60.         Float:z,
  61.         Float:w,
  62.         Float:Pos[3];
  63.  
  64.     GetVehicleRotationQuat(vehicleid,w,x,y,z);
  65.     GetVehiclePos(vehicleid,Pos[0],Pos[1],Pos[2]);
  66.  
  67. //initialized math for quaternion to matrix conversion {for sake of simplicity and efficiency}
  68.     new
  69.         Float:x2 = x * x,
  70.         Float:y2 = y * y,
  71.         Float:z2 = z * z,
  72.         Float:xy = x * y,
  73.         Float:xz = x * z,
  74.         Float:yz = y * z,
  75.         Float:wx = w * x,
  76.         Float:wy = w * y,
  77.         Float:wz = w * z;
  78.  
  79. //the maths required to convert a quat to a matrix
  80.     Mat4x3[mp_PITCH][mi_X] = 1.0 - 2.0 * (y2 + z2);
  81.     Mat4x3[mp_PITCH][mi_Y] = 2.0 * (xy - wz);
  82.     Mat4x3[mp_PITCH][mi_Z] = 2.0 * (xz + wy);
  83.     Mat4x3[mp_ROLL][mi_X] = 2.0 * (xy + wz);
  84.     Mat4x3[mp_ROLL][mi_Y] = 1.0 - 2.0 * (x2 + z2);
  85.     Mat4x3[mp_ROLL][mi_Z] = 2.0 * (yz - wx);
  86.     Mat4x3[mp_YAW][mi_X] = 2.0 * (xz - wy);
  87.     Mat4x3[mp_YAW][mi_Y] = 2.0 * (yz + wx);
  88.     Mat4x3[mp_YAW][mi_Z] = 1.0 - 2.0 * (x2 + y2);
  89. //the gta vehicle matrix has the position in it, I want it to keep just like GTA does so I put the position in
  90.     Mat4x3[mp_POS][mi_X] = Pos[0];
  91.     Mat4x3[mp_POS][mi_Y] = Pos[1];
  92.     Mat4x3[mp_POS][mi_Z] = Pos[2];
  93.    
  94.     return 1;
  95. }
  96.  
  97. /*
  98.     Get position offset at position corresponding to the correct vehicle rotation
  99. */
  100. stock PositionFromVehicleOffset(vehicle,Float:offX,Float:offY,Float:offZ,&Float:x,&Float:y,&Float:z)
  101. {
  102. //initial processing step - gather information
  103.     new
  104.         Mat4x3[MatrixParts][MatrixIndicator];
  105.  
  106.     GetVehicleMatrix(vehicle,Mat4x3);
  107.  
  108. //offset calculation math
  109.     x = offX * Mat4x3[mp_PITCH][mi_X] + offY * Mat4x3[mp_ROLL][mi_X] + offZ * Mat4x3[mp_YAW][mi_X] + Mat4x3[mp_POS][mi_X];
  110.     y = offX * Mat4x3[mp_PITCH][mi_Y] + offY * Mat4x3[mp_ROLL][mi_Y] + offZ * Mat4x3[mp_YAW][mi_Y] + Mat4x3[mp_POS][mi_Y];
  111.     z = offX * Mat4x3[mp_PITCH][mi_Z] + offY * Mat4x3[mp_ROLL][mi_Z] + offZ * Mat4x3[mp_YAW][mi_Z] + Mat4x3[mp_POS][mi_Z];
  112.  
  113.     return 1;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement