btCasts.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #ifndef _BULLET_CASTS_H_
  23. #define _BULLET_CASTS_H_
  24. #ifndef _BULLET_H_
  25. #include "T3D/physics/bullet/bt.h"
  26. #endif
  27. #ifndef _MMATRIX_H_
  28. #include "math/mMatrix.h"
  29. #endif
  30. #ifndef _MPOINT3_H_
  31. #include "math/mPoint3.h"
  32. #endif
  33. #ifndef _MQUAT_H_
  34. #include "math/mQuat.h"
  35. #endif
  36. template <class T, class F> inline T btCast( const F &from );
  37. //-------------------------------------------------------------------------
  38. template<>
  39. inline Point3F btCast( const btVector3 &vec )
  40. {
  41. return Point3F( vec.x(), vec.y(), vec.z() );
  42. }
  43. template<>
  44. inline btVector3 btCast( const Point3F &point )
  45. {
  46. return btVector3( point.x, point.y, point.z );
  47. }
  48. template<>
  49. inline QuatF btCast( const btQuaternion &quat )
  50. {
  51. /// The Torque quat has the opposite winding order.
  52. return QuatF( -quat.x(), -quat.y(), -quat.z(), quat.w() );
  53. }
  54. template<>
  55. inline btQuaternion btCast( const QuatF &quat )
  56. {
  57. /// The Torque quat has the opposite winding order.
  58. return btQuaternion( -quat.x, -quat.y, -quat.z, quat.w );
  59. }
  60. template<>
  61. inline btTransform btCast( const MatrixF &xfm )
  62. {
  63. btTransform out;
  64. out.getBasis().setValue( xfm[0], xfm[1], xfm[2],
  65. xfm[4], xfm[5], xfm[6],
  66. xfm[8], xfm[9], xfm[10] );
  67. out.getOrigin().setValue( xfm[3], xfm[7], xfm[11] );
  68. return out;
  69. }
  70. template<>
  71. inline MatrixF btCast( const btTransform &xfm )
  72. {
  73. MatrixF out;
  74. // Set the rotation.
  75. out.setRow( 0, btCast<Point3F>( xfm.getBasis()[0] ) );
  76. out.setRow( 1, btCast<Point3F>( xfm.getBasis()[1] ) );
  77. out.setRow( 2, btCast<Point3F>( xfm.getBasis()[2] ) );
  78. // The position.
  79. out[3] = xfm.getOrigin().x();
  80. out[7] = xfm.getOrigin().y();
  81. out[11] = xfm.getOrigin().z();
  82. // Clear out the rest.
  83. out[12] = out[13] = out[14] = 0.0f;
  84. out[15] = 1.0f;
  85. return out;
  86. }
  87. #endif // _BULLET_CASTS_H_