Body.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "StdAfx.h"
  2. #include <ode/ode.h>
  3. #include "Body.h"
  4. namespace ODEManaged
  5. {
  6. //Constructors
  7. Body::Body(void)
  8. {
  9. _id = 0;
  10. }
  11. Body::Body(World &world)
  12. {
  13. _id = dBodyCreate(world.Id());
  14. }
  15. //Destructor
  16. Body::~Body(void)
  17. {
  18. dBodyDestroy(this->_id);
  19. }
  20. //Methods
  21. //Id
  22. dBodyID Body::Id()
  23. {
  24. return _id;
  25. }
  26. //SetData
  27. void Body::SetData(void *data)
  28. {
  29. dBodySetData(this->_id, data);
  30. }
  31. //GetData
  32. void *Body::GetData(void)
  33. {
  34. return dBodyGetData(this->_id);
  35. }
  36. //SetPosition
  37. void Body::SetPosition (double x, double y, double z)
  38. {
  39. dBodySetPosition(this->_id, x, y, z);
  40. }
  41. //Overloaded GetPosition
  42. Vector3 Body::GetPosition(void)
  43. {
  44. Vector3 retVal;
  45. const dReal *temp;
  46. temp = dBodyGetPosition(this->_id);
  47. retVal.x = temp[0];
  48. retVal.y = temp[1];
  49. retVal.z = temp[2];
  50. return retVal;
  51. };
  52. void Body::GetPosition(double position __gc[])
  53. {
  54. const dReal *temp;
  55. temp = dBodyGetPosition(this->_id);
  56. position[0] = temp[0];
  57. position[1] = temp[1];
  58. position[2] = temp[2];
  59. }
  60. //SetRotationIdentity
  61. void Body::SetRotationIdentity(void)
  62. {
  63. dMatrix3 temp;
  64. dRSetIdentity(temp);
  65. dBodySetRotation(this->_id, temp);
  66. }
  67. //SetRotation (left handed system=>transpose)
  68. void Body::SetRotation(Matrix3 rotation)
  69. {
  70. dMatrix3 temp;
  71. temp[0] = rotation.m11;
  72. temp[4] = rotation.m12;
  73. temp[8] = rotation.m13;
  74. temp[1] = rotation.m21;
  75. temp[5] = rotation.m22;
  76. temp[9] = rotation.m23;
  77. temp[2] = rotation.m31;
  78. temp[6] = rotation.m32;
  79. temp[10] = rotation.m33;
  80. dBodySetRotation(this->_id, temp);
  81. }
  82. //GetRotation (left handed system=>transpose)
  83. Matrix3 Body::GetRotation(void)
  84. {
  85. Matrix3 retVal;
  86. //const dMatrix3 *m;
  87. const dReal *temp;
  88. temp = dBodyGetRotation(this->_id);
  89. retVal.m11 = temp[0];
  90. retVal.m12 = temp[4];
  91. retVal.m13 = temp[8];
  92. retVal.m21 = temp[1];
  93. retVal.m22 = temp[5];
  94. retVal.m23 = temp[9];
  95. retVal.m31 = temp[2];
  96. retVal.m32 = temp[6];
  97. retVal.m33 = temp[10];
  98. return retVal;
  99. }
  100. //Overloaded SetMass
  101. void Body::SetMass(double mass, Vector3 centerOfGravity, Matrix3 inertia)
  102. {
  103. dMass *temp = new dMass();
  104. dMassSetParameters(temp, mass,
  105. centerOfGravity.x,
  106. centerOfGravity.y,
  107. centerOfGravity.z,
  108. inertia.m11, inertia.m22,
  109. inertia.m33, inertia.m12,
  110. inertia.m13, inertia.m23);
  111. dBodySetMass(this->_id, temp);
  112. }
  113. //SetMassSphere
  114. void Body::SetMassSphere(double density, double radius)
  115. {
  116. dMass *temp = new dMass();
  117. dMassSetSphere(temp, density, radius);
  118. dBodySetMass(this->_id, temp);
  119. }
  120. //SetMassBox
  121. void Body::SetMassBox(double density, double sideX, double sideY, double sideZ)
  122. {
  123. dMass *temp = new dMass();
  124. dMassSetBox(temp, density, sideX, sideY, sideZ);
  125. dBodySetMass(this->_id, temp);
  126. }
  127. //SetMassCappedCylinder
  128. void Body::SetMassCappedCylinder(double density, int axis, double cylinderRadius, double cylinderLength)
  129. {
  130. dMass *temp = new dMass();
  131. dMassSetCappedCylinder(temp, density, axis,
  132. cylinderRadius,
  133. cylinderLength);
  134. dBodySetMass(this->_id, temp);
  135. }
  136. //AddForce
  137. void Body::AddForce(double fX, double fY, double fZ)
  138. {
  139. dBodyAddForce(this->_id, fX, fY, fZ);
  140. }
  141. //AddRelForce
  142. void Body::AddRelForce(double fX, double fY, double fZ)
  143. {
  144. dBodyAddRelForce(this->_id, fX,fY,fZ);
  145. }
  146. //AddForceAtPos
  147. void Body::AddForceAtPos(double fX, double fY, double fZ, double pX, double pY, double pZ)
  148. {
  149. dBodyAddForceAtPos(this->_id, fX, fY, fZ, pX, pY, pZ);
  150. }
  151. //AddRelForceAtPos
  152. void Body::AddRelForceAtPos(double fX, double fY, double fZ, double pX, double pY, double pZ)
  153. {
  154. dBodyAddRelForceAtPos(this->_id, fX, fY, fZ, pX, pY, pZ);
  155. }
  156. //AddRelForceAtRelPos
  157. void Body::AddRelForceAtRelPos(double fX, double fY, double fZ, double pX, double pY, double pZ)
  158. {
  159. dBodyAddRelForceAtRelPos(this->_id, fX, fY, fZ, pX, pY, pZ);
  160. }
  161. //ApplyLinearVelocityDrag
  162. void Body::ApplyLinearVelocityDrag(double dragCoef)
  163. {
  164. const dReal *temp;
  165. double fX;
  166. double fY;
  167. double fZ;
  168. temp = dBodyGetLinearVel(this->_id);
  169. fX = temp[0]*dragCoef*-1;
  170. fY = temp[1]*dragCoef*-1;
  171. fZ = temp[2]*dragCoef*-1;
  172. dBodyAddForce(this->_id, fX, fY, fZ);
  173. }
  174. //ApplyAngularVelocityDrag
  175. void Body::ApplyAngularVelocityDrag(double dragCoef)
  176. {
  177. const dReal *temp;
  178. double fX;
  179. double fY;
  180. double fZ;
  181. temp = dBodyGetAngularVel(this->_id);
  182. fX = temp[0]*dragCoef*-1;
  183. fY = temp[1]*dragCoef*-1;
  184. fZ = temp[2]*dragCoef*-1;
  185. dBodyAddTorque(this->_id, fX, fY, fZ);
  186. }
  187. //AddTorque
  188. void Body::AddTorque(double fX, double fY, double fZ)
  189. {
  190. dBodyAddTorque(this->_id, fX, fY, fZ);
  191. }
  192. //AddRelTorque
  193. void Body::AddRelTorque(double fX, double fY, double fZ)
  194. {
  195. dBodyAddRelTorque(this->_id, fX,fY,fZ);
  196. }
  197. //SetLinearVelocity
  198. void Body::SetLinearVelocity(double x, double y, double z)
  199. {
  200. dBodySetLinearVel(this->_id, x, y, z);
  201. }
  202. //GetLinearVelocity
  203. Vector3 Body::GetLinearVelocity(void)
  204. {
  205. Vector3 retVal;
  206. const dReal *temp;
  207. temp = dBodyGetLinearVel(this->_id);
  208. retVal.x = temp[0];
  209. retVal.y = temp[1];
  210. retVal.z = temp[2];
  211. return retVal;
  212. }
  213. //SetAngularVelocity
  214. void Body::SetAngularVelocity(double x, double y, double z)
  215. {
  216. dBodySetAngularVel(this->_id, x, y, z);
  217. }
  218. //GetAngularVelocity
  219. Vector3 Body::GetAngularVelocity(void)
  220. {
  221. Vector3 retVal;
  222. const dReal *temp;
  223. temp = dBodyGetAngularVel(this->_id);
  224. retVal.x = temp[0];
  225. retVal.y = temp[1];
  226. retVal.z = temp[2];
  227. return retVal;
  228. }
  229. //GetRelPointPos
  230. Vector3 Body::GetRelPointPos(double pX, double pY, double pZ)
  231. {
  232. Vector3 retVal;
  233. dVector3 temp;
  234. dBodyGetRelPointPos(this->_id, pX, pY, pZ, temp);
  235. retVal.x = temp[0];
  236. retVal.y = temp[1];
  237. retVal.z = temp[2];
  238. return retVal;
  239. }
  240. //GetRelPointVel
  241. Vector3 Body::GetRelPointVel(double pX, double pY, double pZ)
  242. {
  243. Vector3 retVal;
  244. dVector3 temp;
  245. dBodyGetRelPointVel(this->_id, pX, pY, pZ, temp);
  246. retVal.x = temp[0];
  247. retVal.y = temp[1];
  248. retVal.z = temp[2];
  249. return retVal;
  250. }
  251. //ConnectedTo
  252. int Body::ConnectedTo(const Body &b)
  253. {
  254. return dAreConnected(this->_id, b._id);
  255. }
  256. }