vector_ScriptBinding.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. ConsoleFunctionGroupBegin( VectorMath, "Vector manipulation functions.");
  23. /*! @defgroup VectorMathFunctions Vector Math
  24. @ingroup TorqueScriptFunctions
  25. @{
  26. */
  27. /*! Use the VectorAdd function to add two vectors of up to three elements each to each other
  28. @param vecA A vector of up to three elements.
  29. @param vecB A vector of up to three elements.
  30. @return Returns the result of vecA + vecB.
  31. @sa vectorSub
  32. */
  33. ConsoleFunctionWithDocs( VectorAdd, ConsoleString, 3, 3, ( vecA , vecB ))
  34. {
  35. VectorF v1(0,0,0),v2(0,0,0);
  36. dSscanf(argv[1],"%g %g %g",&v1.x,&v1.y,&v1.z);
  37. dSscanf(argv[2],"%g %g %g",&v2.x,&v2.y,&v2.z);
  38. VectorF v;
  39. v = v1 + v2;
  40. char* returnBuffer = Con::getReturnBuffer(256);
  41. dSprintf(returnBuffer,256,"%g %g %g",v.x,v.y,v.z);
  42. return returnBuffer;
  43. }
  44. /*! Use the VectorSub function to subtract vecB from vecA.
  45. @param vecA Left side vector in subtraction equation.
  46. @param vecB Right side vector in subtraction equation.
  47. @return Returns a new vector equivalent to: \vecA - vecB\.
  48. @sa vectorAdd
  49. */
  50. ConsoleFunctionWithDocs( VectorSub, ConsoleString, 3, 3, ( vecA , vecB ))
  51. {
  52. VectorF v1(0,0,0),v2(0,0,0);
  53. dSscanf(argv[1],"%g %g %g",&v1.x,&v1.y,&v1.z);
  54. dSscanf(argv[2],"%g %g %g",&v2.x,&v2.y,&v2.z);
  55. VectorF v;
  56. v = v1 - v2;
  57. char* returnBuffer = Con::getReturnBuffer(256);
  58. dSprintf(returnBuffer,256,"%g %g %g",v.x,v.y,v.z);
  59. return returnBuffer;
  60. }
  61. /*! Use the VectorScale function to scale the vector vec by the scalar scale.
  62. @param vec A vector of up to three elements.
  63. @param scale A numeric value (integer or floating-point) representing the scaling factor.
  64. @return Returns a scaled version of the vector vec, equivalent to: ( scale * X ) ( scale * Y ) ( scale * Z )\.
  65. @sa VectorNormalize
  66. */
  67. ConsoleFunctionWithDocs( VectorScale, ConsoleString, 3, 3, ( vec , scale ))
  68. {
  69. VectorF v(0,0,0);
  70. dSscanf(argv[1],"%g %g %g",&v.x,&v.y,&v.z);
  71. v *= dAtof(argv[2]);
  72. char* returnBuffer = Con::getReturnBuffer(256);
  73. dSprintf(returnBuffer,256,"%g %g %g",v.x,v.y,v.z);
  74. return returnBuffer;
  75. }
  76. /*! Use the VectorNormalize function to calculated the unit vector equivalent of the vector vec.
  77. @param vec A vector of up to three elements.
  78. @return Returns the unit vector equivalent of the vector vec.
  79. @sa VectorScale
  80. */
  81. ConsoleFunctionWithDocs( VectorNormalize, ConsoleString, 2, 2, ( vec ))
  82. {
  83. VectorF v(0,0,0);
  84. dSscanf(argv[1],"%g %g %g",&v.x,&v.y,&v.z);
  85. if (v.len() != 0)
  86. v.normalize();
  87. char* returnBuffer = Con::getReturnBuffer(256);
  88. dSprintf(returnBuffer,256,"%g %g %g",v.x,v.y,v.z);
  89. return returnBuffer;
  90. }
  91. /*! Use the VectorCross function to calculate the dot product of two unit vectors of up to three elements each. Warning! Be sure to always normalize both vecA and vecB before attempting to find the dot product. Calculating a dot product using un-normalized vectors (non- unit vectors) will result in meaningless results.
  92. If the return value is < 0, the inner-angle between the vectors is > 90 degrees. If the return value is == 0, the inner-angle between the vectors is == 90 degrees. If the return value is > 0, the inner-angle between the vectors is < 90 degrees.
  93. @param vecA A unit-vector of up to three elements.
  94. @param vecB A unit-vector of up to three elements.
  95. @return Returns a scalar value equal to the result of vecA . vecB. This value which will always be a single floating-point value in the range [ -1 , +1 ].
  96. @sa VectorCross
  97. */
  98. ConsoleFunctionWithDocs( VectorDot, ConsoleFloat, 3, 3, ( vecA , vecB ))
  99. {
  100. VectorF v1(0,0,0),v2(0,0,0);
  101. dSscanf(argv[1],"%g %g %g",&v1.x,&v1.y,&v1.z);
  102. dSscanf(argv[2],"%g %g %g",&v2.x,&v2.y,&v2.z);
  103. return mDot(v1,v2);
  104. }
  105. /*! Use the VectorCross function to calculate the cross product of two vectors of up to three elements each.
  106. Remember, the resultant vector will always be an right angles (orthogonal) to both input vectors.
  107. @param vecA A vector of up to three elements.
  108. @param vecB A vector of up to three elements.
  109. @return Returns the result of vecA x vecB.
  110. @sa VectorDot
  111. */
  112. ConsoleFunctionWithDocs(VectorCross, ConsoleString, 3, 3, ( vecA , vecB ))
  113. {
  114. VectorF v1(0,0,0),v2(0,0,0);
  115. dSscanf(argv[1],"%g %g %g",&v1.x,&v1.y,&v1.z);
  116. dSscanf(argv[2],"%g %g %g",&v2.x,&v2.y,&v2.z);
  117. VectorF v;
  118. mCross(v1,v2,&v);
  119. char* returnBuffer = Con::getReturnBuffer(256);
  120. dSprintf(returnBuffer,256,"%g %g %g",v.x,v.y,v.z);
  121. return returnBuffer;
  122. }
  123. /*! Use the VectorDist function to calculate distance between two vectors of up to three elements each.
  124. @param vecA A vector of up to three elements.
  125. @param vecB A vector of up to three elements.
  126. @return Returns the result of \ |Xa - Xb| |Ya - Yb| |Za - Zb| \.
  127. @sa VectorLen
  128. */
  129. ConsoleFunctionWithDocs(VectorDist, ConsoleFloat, 3, 3, ( vecA , vecB ))
  130. {
  131. VectorF v1(0,0,0),v2(0,0,0);
  132. dSscanf(argv[1],"%g %g %g",&v1.x,&v1.y,&v1.z);
  133. dSscanf(argv[2],"%g %g %g",&v2.x,&v2.y,&v2.z);
  134. VectorF v = v2 - v1;
  135. return mSqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z));
  136. }
  137. /*! Use the VectorLen function to calculate the length of vector vec.
  138. @param vec A vector of up to three elements.
  139. @return Returns a scalar representing the length of the vector vec.
  140. @sa VectorDist
  141. */
  142. ConsoleFunctionWithDocs(VectorLen, ConsoleFloat, 2, 2, ( vec ))
  143. {
  144. VectorF v(0,0,0);
  145. dSscanf(argv[1],"%g %g %g",&v.x,&v.y,&v.z);
  146. return mSqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z));
  147. }
  148. /*! Use the VectorOrthoBasis function to calculate a 3x3 Row-Major formated matrix representing the orthogonal basis for the vector vec.
  149. @param vec A four element vector of the form \AxisX AxisY AxisZ theta\, where theta is the angle of rotation about the vector formed by the prior three values.
  150. @return Returns a 3x3 Row-Major formated matrix
  151. */
  152. ConsoleFunctionWithDocs( VectorOrthoBasis, ConsoleString, 2, 2, ( vec ))
  153. {
  154. AngAxisF aa;
  155. dSscanf(argv[1],"%g %g %g %g", &aa.axis.x,&aa.axis.y,&aa.axis.z,&aa.angle);
  156. MatrixF mat;
  157. aa.setMatrix(&mat);
  158. Point3F col0, col1, col2;
  159. mat.getColumn(0, &col0);
  160. mat.getColumn(1, &col1);
  161. mat.getColumn(2, &col2);
  162. char* returnBuffer = Con::getReturnBuffer(256);
  163. dSprintf(returnBuffer,256,"%g %g %g %g %g %g %g %g %g",
  164. col0.x, col0.y, col0.z, col1.x, col1.y, col1.z, col2.x, col2.y, col2.z);
  165. return returnBuffer;
  166. }
  167. ConsoleFunctionGroupEnd(VectorMath);
  168. /*! @} */ // group VectorMathFunctions