pr.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. //234567890123456789012345678901234567890123456789012345678901234567890123456789
  23. // 1 2 3 4 5 6 7
  24. ////////////////////////////////////////////////////////////////////////////////
  25. // This file create unit test for some of the functions found in:
  26. // ode/src/joinst/pr.cpp
  27. //
  28. //
  29. ////////////////////////////////////////////////////////////////////////////////
  30. #include <UnitTest++.h>
  31. #include <ode/ode.h>
  32. #include "../../ode/src/joints/pr.h"
  33. SUITE (TestdxJointPR)
  34. {
  35. // The 2 bodies are positionned at (0, 0, 0), with no rotation
  36. // The joint is a PR Joint
  37. // Axis is along the X axis
  38. // Anchor at (0, 0, 0)
  39. struct Fixture_dxJointPR_B1_and_B2_At_Zero_Axis_Along_X
  40. {
  41. Fixture_dxJointPR_B1_and_B2_At_Zero_Axis_Along_X()
  42. {
  43. wId = dWorldCreate();
  44. bId1 = dBodyCreate (wId);
  45. dBodySetPosition (bId1, 0, 0, 0);
  46. bId2 = dBodyCreate (wId);
  47. dBodySetPosition (bId2, 0, 0, 0);
  48. jId = dJointCreatePR (wId, 0);
  49. joint = (dxJointPR*) jId;
  50. dJointAttach (jId, bId1, bId2);
  51. dJointSetPRAxis1 (jId, axis[0], axis[1], axis[2]);
  52. }
  53. ~Fixture_dxJointPR_B1_and_B2_At_Zero_Axis_Along_X()
  54. {
  55. dWorldDestroy (wId);
  56. }
  57. dWorldID wId;
  58. dBodyID bId1;
  59. dBodyID bId2;
  60. dJointID jId;
  61. dxJointPR* joint;
  62. static const dVector3 axis;
  63. static const dReal offset;
  64. };
  65. const dVector3 Fixture_dxJointPR_B1_and_B2_At_Zero_Axis_Along_X::axis =
  66. {
  67. 1, 0, 0
  68. };
  69. const dReal Fixture_dxJointPR_B1_and_B2_At_Zero_Axis_Along_X::offset = REAL (3.1);
  70. // Move 1st body offset unit in the X direction
  71. //
  72. // X-------> X---------> Axis -->
  73. // B1 => B1
  74. // B2 B2
  75. //
  76. // Start with a Offset of offset unit
  77. //
  78. // X-------> X---------> Axis -->
  79. // B1 => B1
  80. // B2 B2
  81. TEST_FIXTURE (Fixture_dxJointPR_B1_and_B2_At_Zero_Axis_Along_X,
  82. test_dJointSetPRAxisOffset_B1_3Unit)
  83. {
  84. dJointSetPRAnchor (jId, 0, 0, 0);
  85. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  86. dBodySetPosition (bId1, offset, 0, 0);
  87. CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  88. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  89. // offset*axis[0],offset*axis[1],offset*axis[2]);
  90. // CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  91. // dBodySetPosition (bId1, 0, 0, 0);
  92. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  93. // // Only here to test a deprecated warning
  94. // dJointSetPRAxisDelta (jId, 1, 0, 0, 0, 0, 0);
  95. }
  96. // Move 1st body offset unit in the opposite X direction
  97. //
  98. // X-------> X---------> Axis -->
  99. // B1 => B1
  100. // B2 B2
  101. //
  102. // Start with a Offset of -offset unit
  103. //
  104. // X-------> X---------> Axis -->
  105. // B1 => B1
  106. // B2 B2
  107. TEST_FIXTURE (Fixture_dxJointPR_B1_and_B2_At_Zero_Axis_Along_X,
  108. test_dJointSetPRAxisOffset_B1_Minus_3Unit)
  109. {
  110. dJointSetPRAnchor (jId, 0, 0, 0);
  111. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  112. dBodySetPosition (bId1, -offset, 0, 0);
  113. CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  114. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  115. // -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  116. // CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  117. // dBodySetPosition (bId1, 0, 0, 0);
  118. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  119. }
  120. // Move 2nd body offset unit in the X direction
  121. //
  122. // X-------> X---------> Axis -->
  123. // B1 => B1
  124. // B2 B2
  125. //
  126. // Start with a Offset of offset unit
  127. //
  128. // X-------> X---------> Axis -->
  129. // B1 => B1
  130. // B2 B2
  131. TEST_FIXTURE (Fixture_dxJointPR_B1_and_B2_At_Zero_Axis_Along_X,
  132. test_dJointSetPRAxisOffset_B2_3Unit)
  133. {
  134. dJointSetPRAnchor (jId, 0, 0, 0);
  135. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  136. dBodySetPosition (bId2, offset, 0, 0);
  137. CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  138. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  139. // -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  140. // CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  141. // dBodySetPosition (bId2, 0, 0, 0);
  142. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  143. }
  144. // Move 2nd body offset unit in the opposite X direction
  145. //
  146. // X-------> X---------> Axis -->
  147. // B1 => B1
  148. // B2 B2
  149. //
  150. // Start with a Offset of -offset unit
  151. //
  152. // X-------> X---------> Axis -->
  153. // B1 => B1
  154. // B2 B2
  155. TEST_FIXTURE (Fixture_dxJointPR_B1_and_B2_At_Zero_Axis_Along_X,
  156. test_dJointSetPRAxisOffset_B2_Minus_3Unit)
  157. {
  158. dJointSetPRAnchor (jId, 0, 0, 0);
  159. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  160. dBodySetPosition (bId2, -offset, 0, 0);
  161. CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  162. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  163. // offset*axis[0],offset*axis[1],offset*axis[2]);
  164. // CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  165. // dBodySetPosition (bId2, 0, 0, 0);
  166. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  167. }
  168. // Only body 1
  169. // The body are positionned at (0, 0, 0), with no rotation
  170. // The joint is a PR Joint
  171. // Axis is along the X axis
  172. // Anchor at (0, 0, 0)
  173. struct Fixture_dxJointPR_B1_At_Zero_Axis_Along_X
  174. {
  175. Fixture_dxJointPR_B1_At_Zero_Axis_Along_X()
  176. {
  177. wId = dWorldCreate();
  178. bId1 = dBodyCreate (wId);
  179. dBodySetPosition (bId1, 0, 0, 0);
  180. jId = dJointCreatePR (wId, 0);
  181. joint = (dxJointPR*) jId;
  182. dJointAttach (jId, bId1, NULL);
  183. dJointSetPRAxis1 (jId, axis[0], axis[1], axis[2]);
  184. }
  185. ~Fixture_dxJointPR_B1_At_Zero_Axis_Along_X()
  186. {
  187. dWorldDestroy (wId);
  188. }
  189. dWorldID wId;
  190. dBodyID bId1;
  191. dJointID jId;
  192. dxJointPR* joint;
  193. static const dVector3 axis;
  194. static const dReal offset;
  195. };
  196. const dVector3 Fixture_dxJointPR_B1_At_Zero_Axis_Along_X::axis =
  197. {
  198. 1, 0, 0
  199. };
  200. const dReal Fixture_dxJointPR_B1_At_Zero_Axis_Along_X::offset = REAL (3.1);
  201. // Move 1st body offset unit in the X direction
  202. //
  203. // X-------> X---------> Axis -->
  204. // B1 => B1
  205. //
  206. // Start with a Offset of offset unit
  207. //
  208. // X-------> X---------> Axis -->
  209. // B1 => B1
  210. TEST_FIXTURE (Fixture_dxJointPR_B1_At_Zero_Axis_Along_X,
  211. test_dJointSetPRAxisOffset_B1_OffsetUnit)
  212. {
  213. dJointSetPRAnchor (jId, 0, 0, 0);
  214. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  215. dBodySetPosition (bId1, offset, 0, 0);
  216. CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  217. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  218. // offset*axis[0],offset*axis[1],offset*axis[2]);
  219. // CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  220. // dBodySetPosition (bId1, 0, 0, 0);
  221. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  222. }
  223. // Move 1st body offset unit in the opposite X direction
  224. //
  225. // X-------> X---------> Axis -->
  226. // B1 => B1
  227. //
  228. // Start with a Offset of -offset unit
  229. //
  230. // X-------> X---------> Axis -->
  231. // B1 => B1
  232. TEST_FIXTURE (Fixture_dxJointPR_B1_At_Zero_Axis_Along_X,
  233. test_dJointSetPRAxisOffset_B1_Minus_OffsetUnit)
  234. {
  235. dJointSetPRAnchor (jId, 0, 0, 0);
  236. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  237. dBodySetPosition (bId1, -offset, 0, 0);
  238. CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  239. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  240. // -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  241. // CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  242. // dBodySetPosition (bId1, 0, 0, 0);
  243. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  244. }
  245. // Only body 1
  246. // The body are positionned at (0, 0, 0), with no rotation
  247. // The joint is a PR Joint
  248. // Axis is in the oppsite X axis
  249. // Anchor at (0, 0, 0)
  250. struct Fixture_dxJointPR_B1_At_Zero_Axis_Inverse_of_X
  251. {
  252. Fixture_dxJointPR_B1_At_Zero_Axis_Inverse_of_X()
  253. {
  254. wId = dWorldCreate();
  255. bId1 = dBodyCreate (wId);
  256. dBodySetPosition (bId1, 0, 0, 0);
  257. jId = dJointCreatePR (wId, 0);
  258. joint = (dxJointPR*) jId;
  259. dJointAttach (jId, bId1, NULL);
  260. dJointSetPRAxis1 (jId, axis[0], axis[1], axis[2]);
  261. }
  262. ~Fixture_dxJointPR_B1_At_Zero_Axis_Inverse_of_X()
  263. {
  264. dWorldDestroy (wId);
  265. }
  266. dWorldID wId;
  267. dBodyID bId1;
  268. dJointID jId;
  269. dxJointPR* joint;
  270. static const dVector3 axis;
  271. static const dReal offset;
  272. };
  273. const dVector3 Fixture_dxJointPR_B1_At_Zero_Axis_Inverse_of_X::axis =
  274. {
  275. -1, 0, 0
  276. };
  277. const dReal Fixture_dxJointPR_B1_At_Zero_Axis_Inverse_of_X::offset = REAL (3.1);
  278. // Move 1st body offset unit in the X direction
  279. //
  280. // X-------> X---------> <--- Axis
  281. // B1 => B1
  282. //
  283. // Start with a Offset of offset unit
  284. //
  285. // X-------> X---------> <--- Axis
  286. // B1 => B1
  287. TEST_FIXTURE (Fixture_dxJointPR_B1_At_Zero_Axis_Inverse_of_X,
  288. test_dJointSetPRAxisOffset_B1_OffsetUnit)
  289. {
  290. dJointSetPRAnchor (jId, 0, 0, 0);
  291. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  292. dBodySetPosition (bId1, offset, 0, 0);
  293. CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  294. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  295. // -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  296. // CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  297. // dBodySetPosition (bId1, 0, 0, 0);
  298. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  299. }
  300. // Move 1st body offset unit in the opposite X direction
  301. //
  302. // X-------> X---------> <--- Axis
  303. // B1 => B1
  304. //
  305. // Start with a Offset of -offset unit
  306. //
  307. // X-------> X---------> <--- Axis
  308. // B1 => B1
  309. TEST_FIXTURE (Fixture_dxJointPR_B1_At_Zero_Axis_Inverse_of_X,
  310. test_dJointSetPRAxisOffset_B1_Minus_OffsetUnit)
  311. {
  312. dJointSetPRAnchor (jId, 0, 0, 0);
  313. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  314. dBodySetPosition (bId1, -offset, 0, 0);
  315. CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  316. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  317. // offset*axis[0],offset*axis[1],offset*axis[2]);
  318. // CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  319. // dBodySetPosition (bId1, 0, 0, 0);
  320. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  321. }
  322. // Compare only one body to 2 bodies with one fixed.
  323. //
  324. // The body are positionned at (0, 0, 0), with no rotation
  325. // The joint is a PR Joint
  326. // Axis is along the X axis
  327. // Anchor at (0, 0, 0)
  328. struct Fixture_dxJointPR_Compare_Body_At_Zero_AxisP_Along_Y
  329. {
  330. Fixture_dxJointPR_Compare_Body_At_Zero_AxisP_Along_Y()
  331. {
  332. wId = dWorldCreate();
  333. bId1_12 = dBodyCreate (wId);
  334. dBodySetPosition (bId1_12, 0, 0, 0);
  335. bId2_12 = dBodyCreate (wId);
  336. dBodySetPosition (bId2_12, 0, 0, 0);
  337. // The force will be added in the function since it is not
  338. // always on the same body
  339. jId_12 = dJointCreatePR (wId, 0);
  340. dJointAttach(jId_12, bId1_12, bId2_12);
  341. fixed = dJointCreateFixed (wId, 0);
  342. jId = dJointCreatePR (wId, 0);
  343. bId = dBodyCreate (wId);
  344. dBodySetPosition (bId, 0, 0, 0);
  345. // Linear velocity along the prismatic axis;
  346. dVector3 axis;
  347. dJointGetPRAxis1(jId_12, axis);
  348. dJointSetPRAxis1(jId, axis[0], axis[1], axis[2]);
  349. dBodySetLinearVel (bId, 4*axis[0], 4*axis[1], 4*axis[2]);
  350. }
  351. ~Fixture_dxJointPR_Compare_Body_At_Zero_AxisP_Along_Y()
  352. {
  353. dWorldDestroy (wId);
  354. }
  355. dWorldID wId;
  356. dBodyID bId1_12;
  357. dBodyID bId2_12;
  358. dJointID jId_12; // Joint with 2 bodies
  359. dJointID fixed;
  360. dBodyID bId;
  361. dJointID jId; // Joint with one body
  362. };
  363. TEST_FIXTURE (Fixture_dxJointPR_Compare_Body_At_Zero_AxisP_Along_Y,
  364. test_dJointSetPRPositionRate_Only_B1)
  365. {
  366. // Linear velocity along the prismatic axis;
  367. dVector3 axis;
  368. dJointGetPRAxis1(jId_12, axis);
  369. dBodySetLinearVel (bId1_12, 4*axis[0], 4*axis[1], 4*axis[2]);
  370. dJointAttach(jId_12, bId1_12, bId2_12);
  371. dJointAttach(fixed, 0, bId2_12);
  372. dJointSetFixed(fixed);
  373. dJointAttach(jId, bId, 0);
  374. CHECK_CLOSE(dJointGetPRPositionRate(jId_12), dJointGetPRPositionRate(jId), 1e-2);
  375. CHECK_CLOSE(dJointGetPRAngleRate(jId_12), dJointGetPRAngleRate(jId), 1e-2);
  376. }
  377. TEST_FIXTURE (Fixture_dxJointPR_Compare_Body_At_Zero_AxisP_Along_Y,
  378. test_dJointSetPRPositionRate_Only_B2)
  379. {
  380. // Linear velocity along the prismatic axis;
  381. dVector3 axis;
  382. dJointGetPRAxis1(jId_12, axis);
  383. dBodySetLinearVel (bId2_12, 4*axis[0], 4*axis[1], 4*axis[2]);
  384. dJointAttach(jId_12, bId1_12, bId2_12);
  385. dJointAttach(fixed, bId1_12, 0);
  386. dJointSetFixed(fixed);
  387. dJointAttach(jId, 0, bId);
  388. CHECK_CLOSE(dJointGetPRPositionRate(jId_12), dJointGetPRPositionRate(jId), 1e-2);
  389. CHECK_CLOSE(dJointGetPRAngleRate(jId_12), dJointGetPRAngleRate(jId), 1e-2);
  390. }
  391. // Only body 2
  392. // The body are positionned at (0, 0, 0), with no rotation
  393. // The joint is a PR Joint
  394. // Axis is along the X axis
  395. // Anchor at (0, 0, 0)
  396. struct Fixture_dxJointPR_B2_At_Zero_Axis_Along_X
  397. {
  398. Fixture_dxJointPR_B2_At_Zero_Axis_Along_X()
  399. {
  400. wId = dWorldCreate();
  401. bId2 = dBodyCreate (wId);
  402. dBodySetPosition (bId2, 0, 0, 0);
  403. jId = dJointCreatePR (wId, 0);
  404. joint = (dxJointPR*) jId;
  405. dJointAttach (jId, NULL, bId2);
  406. dJointSetPRAxis1 (jId, axis[0], axis[1], axis[2]);
  407. }
  408. ~Fixture_dxJointPR_B2_At_Zero_Axis_Along_X()
  409. {
  410. dWorldDestroy (wId);
  411. }
  412. dWorldID wId;
  413. dBodyID bId2;
  414. dJointID jId;
  415. dxJointPR* joint;
  416. static const dVector3 axis;
  417. static const dReal offset;
  418. };
  419. const dVector3 Fixture_dxJointPR_B2_At_Zero_Axis_Along_X::axis =
  420. {
  421. 1, 0, 0
  422. };
  423. const dReal Fixture_dxJointPR_B2_At_Zero_Axis_Along_X::offset = REAL (3.1);
  424. // Move 2nd body offset unit in the X direction
  425. //
  426. // X-------> X---------> Axis -->
  427. // B2 => B2
  428. //
  429. // Start with a Offset of offset unit
  430. //
  431. // X-------> X---------> Axis -->
  432. // B2 => B2
  433. TEST_FIXTURE (Fixture_dxJointPR_B2_At_Zero_Axis_Along_X,
  434. test_dJointSetPRAxisOffset_B2_OffsetUnit)
  435. {
  436. dJointSetPRAnchor (jId, 0, 0, 0);
  437. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  438. dBodySetPosition (bId2, offset, 0, 0);
  439. CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  440. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  441. // -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  442. // CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  443. // dBodySetPosition (bId2, 0, 0, 0);
  444. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  445. }
  446. // Move 2nd body offset unit in the opposite X direction
  447. //
  448. // X-------> X---------> Axis -->
  449. // B2 => B2
  450. //
  451. // Start with a Offset of -offset unit
  452. //
  453. // X-------> X---------> Axis -->
  454. // B2 => B2
  455. TEST_FIXTURE (Fixture_dxJointPR_B2_At_Zero_Axis_Along_X,
  456. test_dJointSetPRAxisOffset_B2_Minus_OffsetUnit)
  457. {
  458. dJointSetPRAnchor (jId, 0, 0, 0);
  459. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  460. dBodySetPosition (bId2, -offset, 0, 0);
  461. CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  462. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  463. // offset*axis[0],offset*axis[1],offset*axis[2]);
  464. // CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  465. // dBodySetPosition (bId2, 0, 0, 0);
  466. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  467. }
  468. // Only body 2
  469. // The body are positionned at (0, 0, 0), with no rotation
  470. // The joint is a PR Joint
  471. // Axis is in the opposite X axis
  472. // Anchor at (0, 0, 0)
  473. struct Fixture_dxJointPR_B2_At_Zero_Axis_Inverse_of_X
  474. {
  475. Fixture_dxJointPR_B2_At_Zero_Axis_Inverse_of_X()
  476. {
  477. wId = dWorldCreate();
  478. bId2 = dBodyCreate (wId);
  479. dBodySetPosition (bId2, 0, 0, 0);
  480. jId = dJointCreatePR (wId, 0);
  481. joint = (dxJointPR*) jId;
  482. dJointAttach (jId, NULL, bId2);
  483. dJointSetPRAxis1 (jId, axis[0], axis[1], axis[2]);
  484. }
  485. ~Fixture_dxJointPR_B2_At_Zero_Axis_Inverse_of_X()
  486. {
  487. dWorldDestroy (wId);
  488. }
  489. dWorldID wId;
  490. dBodyID bId2;
  491. dJointID jId;
  492. dxJointPR* joint;
  493. static const dVector3 axis;
  494. static const dReal offset;
  495. };
  496. const dVector3 Fixture_dxJointPR_B2_At_Zero_Axis_Inverse_of_X::axis =
  497. {
  498. -1, 0, 0
  499. };
  500. const dReal Fixture_dxJointPR_B2_At_Zero_Axis_Inverse_of_X::offset = REAL (3.1);
  501. // Move 2nd body offset unit in the X direction
  502. //
  503. // X-------> X---------> <--- Axis
  504. // B2 => B2
  505. //
  506. // Start with a Offset of offset unit
  507. //
  508. // X-------> X---------> <--- Axis
  509. // B2 => B2
  510. TEST_FIXTURE (Fixture_dxJointPR_B2_At_Zero_Axis_Inverse_of_X,
  511. test_dJointSetPRAxisOffset_B2_OffsetUnit)
  512. {
  513. dJointSetPRAnchor (jId, 0, 0, 0);
  514. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  515. dBodySetPosition (bId2, offset, 0, 0);
  516. CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  517. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  518. // offset*axis[0],offset*axis[1],offset*axis[2]);
  519. // CHECK_CLOSE (offset, dJointGetPRPosition (jId), 1e-4);
  520. // dBodySetPosition (bId2, 0, 0, 0);
  521. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  522. // dJointSetPRAxisDelta (jId, 1, 0, 0, 0, 0, 0);
  523. }
  524. // Move 1st body offset unit in the opposite X direction
  525. //
  526. // X-------> X---------> <--- Axis
  527. // B2 => B2
  528. //
  529. // Start with a Offset of -offset unit
  530. //
  531. // X-------> X---------> <--- Axis
  532. // B2 => B2
  533. TEST_FIXTURE (Fixture_dxJointPR_B2_At_Zero_Axis_Inverse_of_X,
  534. test_dJointSetPRAxisOffset_B2_Minus_OffsetUnit)
  535. {
  536. dJointSetPRAnchor (jId, 0, 0, 0);
  537. CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  538. dBodySetPosition (bId2, -offset, 0, 0);
  539. CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  540. // dJointSetPRAnchorOffset (jId, 0, 0, 0,
  541. // -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  542. // CHECK_CLOSE (-offset, dJointGetPRPosition (jId), 1e-4);
  543. // dBodySetPosition (bId2, 0, 0, 0);
  544. // CHECK_CLOSE (0.0, dJointGetPRPosition (jId), 1e-4);
  545. }
  546. // The 2 bodies are positionned at (0, 0, 0), and (0, 0, 0)
  547. // The bodis have rotation of 27deg around some axis.
  548. // The joint is a PR Joint
  549. // Axis is along the X axis
  550. // Anchor at (0, 0, 0)
  551. struct Fixture_dxJointPR_B1_and_B2_Random_Orientation_At_Zero_Axis_Along_X
  552. {
  553. Fixture_dxJointPR_B1_and_B2_Random_Orientation_At_Zero_Axis_Along_X()
  554. {
  555. wId = dWorldCreate();
  556. bId1 = dBodyCreate (wId);
  557. dBodySetPosition (bId1, 0, 0, 0);
  558. bId2 = dBodyCreate (wId);
  559. dBodySetPosition (bId2, 0, 0, 0);
  560. dMatrix3 R;
  561. dVector3 axis; // Random axis
  562. axis[0] = REAL(0.53);
  563. axis[1] = -REAL(0.71);
  564. axis[2] = REAL(0.43);
  565. dNormalize3(axis);
  566. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  567. REAL(0.47123)); // 27deg
  568. dBodySetRotation (bId1, R);
  569. axis[0] = REAL(1.2);
  570. axis[1] = REAL(0.87);
  571. axis[2] = -REAL(0.33);
  572. dNormalize3(axis);
  573. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  574. REAL(0.47123)); // 27deg
  575. dBodySetRotation (bId2, R);
  576. jId = dJointCreatePR (wId, 0);
  577. joint = (dxJointPR*) jId;
  578. dJointAttach (jId, bId1, bId2);
  579. }
  580. ~Fixture_dxJointPR_B1_and_B2_Random_Orientation_At_Zero_Axis_Along_X()
  581. {
  582. dWorldDestroy (wId);
  583. }
  584. dWorldID wId;
  585. dBodyID bId1;
  586. dBodyID bId2;
  587. dJointID jId;
  588. dxJointPR* joint;
  589. };
  590. // Test is dJointSetPRAxis and dJointGetPRAxis return same value
  591. TEST_FIXTURE (Fixture_dxJointPR_B1_and_B2_Random_Orientation_At_Zero_Axis_Along_X,
  592. test_dJointSetGetPRAxis)
  593. {
  594. dVector3 axisOrig, axis;
  595. dJointGetPRAxis1 (jId, axisOrig);
  596. dJointGetPRAxis1 (jId, axis);
  597. dJointSetPRAxis1 (jId, axis[0], axis[1], axis[2]);
  598. dJointGetPRAxis1 (jId, axis);
  599. CHECK_CLOSE (axis[0], axisOrig[0] , 1e-4);
  600. CHECK_CLOSE (axis[1], axisOrig[1] , 1e-4);
  601. CHECK_CLOSE (axis[2], axisOrig[2] , 1e-4);
  602. dJointGetPRAxis2 (jId, axisOrig);
  603. dJointGetPRAxis2(jId, axis);
  604. dJointSetPRAxis2 (jId, axis[0], axis[1], axis[2]);
  605. dJointGetPRAxis2 (jId, axis);
  606. CHECK_CLOSE (axis[0], axisOrig[0] , 1e-4);
  607. CHECK_CLOSE (axis[1], axisOrig[1] , 1e-4);
  608. CHECK_CLOSE (axis[2], axisOrig[2] , 1e-4);
  609. }
  610. // Create 2 bodies attached by a PR joint
  611. // Axis is along the X axis (Default value
  612. // Anchor at (0, 0, 0) (Default value)
  613. //
  614. // ^Y
  615. // |
  616. // * Body2
  617. // |
  618. // |
  619. // Body1 |
  620. // * Z-------->
  621. struct dxJointPR_Test_Initialization
  622. {
  623. dxJointPR_Test_Initialization()
  624. {
  625. wId = dWorldCreate();
  626. // Remove gravity to have the only force be the force of the joint
  627. dWorldSetGravity(wId, 0,0,0);
  628. for (int j=0; j<2; ++j)
  629. {
  630. bId[j][0] = dBodyCreate (wId);
  631. dBodySetPosition (bId[j][0], -1, -2, -3);
  632. bId[j][1] = dBodyCreate (wId);
  633. dBodySetPosition (bId[j][1], 11, 22, 33);
  634. dMatrix3 R;
  635. dVector3 axis; // Random axis
  636. axis[0] = REAL(0.53);
  637. axis[1] = -REAL(0.71);
  638. axis[2] = REAL(0.43);
  639. dNormalize3(axis);
  640. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  641. REAL(0.47123)); // 27deg
  642. dBodySetRotation (bId[j][0], R);
  643. axis[0] = REAL(1.2);
  644. axis[1] = REAL(0.87);
  645. axis[2] = -REAL(0.33);
  646. dNormalize3(axis);
  647. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  648. REAL(0.47123)); // 27deg
  649. dBodySetRotation (bId[j][1], R);
  650. jId[j] = dJointCreatePR (wId, 0);
  651. dJointAttach (jId[j], bId[j][0], bId[j][1]);
  652. }
  653. }
  654. ~dxJointPR_Test_Initialization()
  655. {
  656. dWorldDestroy (wId);
  657. }
  658. dWorldID wId;
  659. dBodyID bId[2][2];
  660. dJointID jId[2];
  661. };
  662. // Test if setting a PR with its default values
  663. // will behave the same as a default PR joint
  664. TEST_FIXTURE (dxJointPR_Test_Initialization,
  665. test_PR_Initialization)
  666. {
  667. using namespace std;
  668. dVector3 axis;
  669. dJointGetPRAxis1(jId[1], axis);
  670. dJointSetPRAxis1(jId[1], axis[0], axis[1], axis[2]);
  671. dJointGetPRAxis2(jId[1], axis);
  672. dJointSetPRAxis2(jId[1], axis[0], axis[1], axis[2]);
  673. dVector3 anchor;
  674. dJointGetPRAnchor(jId[1], anchor);
  675. dJointSetPRAnchor(jId[1], anchor[0], anchor[1], anchor[2]);
  676. for (int b=0; b<2; ++b)
  677. {
  678. // Compare body b of the first joint with its equivalent on the
  679. // second joint
  680. const dReal *qA = dBodyGetQuaternion(bId[0][b]);
  681. const dReal *qB = dBodyGetQuaternion(bId[1][b]);
  682. CHECK_CLOSE (qA[0], qB[0], 1e-4);
  683. CHECK_CLOSE (qA[1], qB[1], 1e-4);
  684. CHECK_CLOSE (qA[2], qB[2], 1e-4);
  685. CHECK_CLOSE (qA[3], qB[3], 1e-4);
  686. }
  687. dWorldStep (wId,0.5);
  688. dWorldStep (wId,0.5);
  689. dWorldStep (wId,0.5);
  690. dWorldStep (wId,0.5);
  691. for (int b=0; b<2; ++b)
  692. {
  693. // Compare body b of the first joint with its equivalent on the
  694. // second joint
  695. const dReal *qA = dBodyGetQuaternion(bId[0][b]);
  696. const dReal *qB = dBodyGetQuaternion(bId[1][b]);
  697. CHECK_CLOSE (qA[0], qB[0], 1e-4);
  698. CHECK_CLOSE (qA[1], qB[1], 1e-4);
  699. CHECK_CLOSE (qA[2], qB[2], 1e-4);
  700. CHECK_CLOSE (qA[3], qB[3], 1e-4);
  701. const dReal *posA = dBodyGetPosition(bId[0][b]);
  702. const dReal *posB = dBodyGetPosition(bId[1][b]);
  703. CHECK_CLOSE (posA[0], posB[0], 1e-4);
  704. CHECK_CLOSE (posA[1], posB[1], 1e-4);
  705. CHECK_CLOSE (posA[2], posB[2], 1e-4);
  706. CHECK_CLOSE (posA[3], posB[3], 1e-4);
  707. }
  708. }
  709. // This test compare the result of a slider with 2 bodies where body body 2 is
  710. // fixed to the world to a slider with only one body at position 1.
  711. //
  712. // Test the limits [-1, 0.25] when only one body at is attached to the joint
  713. // using dJointAttache(jId, bId, 0);
  714. //
  715. TEST_FIXTURE(Fixture_dxJointPR_Compare_Body_At_Zero_AxisP_Along_Y,
  716. test_Limit_minus1_025_One_Body_on_left)
  717. {
  718. // Linear velocity along the prismatic axis;
  719. dVector3 axis;
  720. dJointGetPRAxis1(jId_12, axis);
  721. dBodySetLinearVel (bId1_12, 4*axis[0], 4*axis[1], 4*axis[2]);
  722. dJointAttach(jId_12, bId1_12, bId2_12);
  723. dJointSetPRParam(jId_12, dParamLoStop, -1);
  724. dJointSetPRParam(jId_12, dParamHiStop, 0.25);
  725. dJointAttach(fixed, 0, bId2_12);
  726. dJointSetFixed(fixed);
  727. dJointAttach(jId, bId, 0);
  728. dJointSetPRParam(jId, dParamLoStop, -1);
  729. dJointSetPRParam(jId, dParamHiStop, 0.25);
  730. for (int i=0; i<50; ++i)
  731. dWorldStep(wId, 1.0);
  732. const dReal *pos1_12 = dBodyGetPosition(bId1_12);
  733. const dReal *pos = dBodyGetPosition(bId);
  734. CHECK_CLOSE (pos1_12[0], pos[0], 1e-2);
  735. CHECK_CLOSE (pos1_12[1], pos[1], 1e-2);
  736. CHECK_CLOSE (pos1_12[2], pos[2], 1e-2);
  737. const dReal *q1_12 = dBodyGetQuaternion(bId1_12);
  738. const dReal *q = dBodyGetQuaternion(bId);
  739. CHECK_CLOSE (q1_12[0], q[0], 1e-4);
  740. CHECK_CLOSE (q1_12[1], q[1], 1e-4);
  741. CHECK_CLOSE (q1_12[2], q[2], 1e-4);
  742. CHECK_CLOSE (q1_12[3], q[3], 1e-4);
  743. }
  744. // This test compare the result of a slider with 2 bodies where body body 1 is
  745. // fixed to the world to a slider with only one body at position 2.
  746. //
  747. // Test the limits [-1, 0.25] when only one body at is attached to the joint
  748. // using dJointAttache(jId, 0, bId);
  749. //
  750. TEST_FIXTURE(Fixture_dxJointPR_Compare_Body_At_Zero_AxisP_Along_Y,
  751. test_Limit_minus1_025_One_Body_on_right)
  752. {
  753. // Linear velocity along the prismatic axis;
  754. dVector3 axis;
  755. dJointGetPRAxis1(jId_12, axis);
  756. dBodySetLinearVel (bId2_12, 4*axis[0], 4*axis[1], 4*axis[2]);
  757. dJointAttach(jId_12, bId1_12, bId2_12);
  758. dJointSetPRParam(jId_12, dParamLoStop, -1);
  759. dJointSetPRParam(jId_12, dParamHiStop, 0.25);
  760. dJointAttach(fixed, bId1_12, 0);
  761. dJointSetFixed(fixed);
  762. dJointAttach(jId, 0, bId);
  763. dJointSetPRParam(jId, dParamLoStop, -1);
  764. dJointSetPRParam(jId, dParamHiStop, 0.25);
  765. for (int i=0; i<50; ++i)
  766. dWorldStep(wId, 1.0);
  767. const dReal *pos2_12 = dBodyGetPosition(bId2_12);
  768. const dReal *pos = dBodyGetPosition(bId);
  769. CHECK_CLOSE (pos2_12[0], pos[0], 1e-2);
  770. CHECK_CLOSE (pos2_12[1], pos[1], 1e-2);
  771. CHECK_CLOSE (pos2_12[2], pos[2], 1e-2);
  772. const dReal *q2_12 = dBodyGetQuaternion(bId2_12);
  773. const dReal *q = dBodyGetQuaternion(bId);
  774. CHECK_CLOSE (q2_12[0], q[0], 1e-4);
  775. CHECK_CLOSE (q2_12[1], q[1], 1e-4);
  776. CHECK_CLOSE (q2_12[2], q[2], 1e-4);
  777. CHECK_CLOSE (q2_12[3], q[3], 1e-4);
  778. }
  779. // This test compare the result of a slider with 2 bodies where body body 2 is
  780. // fixed to the world to a slider with only one body at position 1.
  781. //
  782. // Test the limits [0, 0] when only one body at is attached to the joint
  783. // using dJointAttache(jId, bId, 0);
  784. //
  785. // The body should not move since their is no room between the two limits
  786. //
  787. TEST_FIXTURE(Fixture_dxJointPR_Compare_Body_At_Zero_AxisP_Along_Y,
  788. test_Limit_0_0_One_Body_on_left)
  789. {
  790. // Linear velocity along the prismatic axis;
  791. dVector3 axis;
  792. dJointGetPRAxis1(jId_12, axis);
  793. dBodySetLinearVel (bId1_12, 4*axis[0], 4*axis[1], 4*axis[2]);
  794. dJointAttach(jId_12, bId1_12, bId2_12);
  795. dJointSetPRParam(jId_12, dParamLoStop, 0);
  796. dJointSetPRParam(jId_12, dParamHiStop, 0);
  797. dJointAttach(fixed, 0, bId2_12);
  798. dJointSetFixed(fixed);
  799. dJointAttach(jId, bId, 0);
  800. dJointSetPRParam(jId, dParamLoStop, 0);
  801. dJointSetPRParam(jId, dParamHiStop, 0);
  802. for (int i=0; i<50; ++i)
  803. dWorldStep(wId, 1.0);
  804. const dReal *pos1_12 = dBodyGetPosition(bId1_12);
  805. const dReal *pos = dBodyGetPosition(bId);
  806. CHECK_CLOSE (pos1_12[0], pos[0], 1e-4);
  807. CHECK_CLOSE (pos1_12[1], pos[1], 1e-4);
  808. CHECK_CLOSE (pos1_12[2], pos[2], 1e-4);
  809. CHECK_CLOSE (0, pos[0], 1e-4);
  810. CHECK_CLOSE (0, pos[1], 1e-4);
  811. CHECK_CLOSE (0, pos[2], 1e-4);
  812. const dReal *q1_12 = dBodyGetQuaternion(bId1_12);
  813. const dReal *q = dBodyGetQuaternion(bId);
  814. CHECK_CLOSE (q1_12[0], q[0], 1e-4);
  815. CHECK_CLOSE (q1_12[1], q[1], 1e-4);
  816. CHECK_CLOSE (q1_12[2], q[2], 1e-4);
  817. CHECK_CLOSE (q1_12[3], q[3], 1e-4);
  818. }
  819. // This test compare the result of a slider with 2 bodies where body body 1 is
  820. // fixed to the world to a slider with only one body at position 2.
  821. //
  822. // Test the limits [0, 0] when only one body at is attached to the joint
  823. // using dJointAttache(jId, 0, bId);
  824. //
  825. // The body should not move since their is no room between the two limits
  826. //
  827. TEST_FIXTURE(Fixture_dxJointPR_Compare_Body_At_Zero_AxisP_Along_Y,
  828. test_Limit_0_0_One_Body_on_right)
  829. {
  830. // Linear velocity along the prismatic axis;
  831. dVector3 axis;
  832. dJointGetPRAxis1(jId_12, axis);
  833. dBodySetLinearVel (bId2_12, 4*axis[0], 4*axis[1], 4*axis[2]);
  834. dJointAttach(jId_12, bId1_12, bId2_12);
  835. dJointSetPRParam(jId_12, dParamLoStop, 0);
  836. dJointSetPRParam(jId_12, dParamHiStop, 0);
  837. dJointAttach(fixed, bId1_12, 0);
  838. dJointSetFixed(fixed);
  839. dJointAttach(jId, 0, bId);
  840. dJointSetPRParam(jId, dParamLoStop, 0);
  841. dJointSetPRParam(jId, dParamHiStop, 0);
  842. for (int i=0; i<50; ++i)
  843. {
  844. dWorldStep(wId, 1.0);
  845. }
  846. const dReal *pos2_12 = dBodyGetPosition(bId2_12);
  847. const dReal *pos = dBodyGetPosition(bId);
  848. CHECK_CLOSE (pos2_12[0], pos[0], 1e-4);
  849. CHECK_CLOSE (pos2_12[1], pos[1], 1e-4);
  850. CHECK_CLOSE (pos2_12[2], pos[2], 1e-4);
  851. CHECK_CLOSE (0, pos[0], 1e-4);
  852. CHECK_CLOSE (0, pos[1], 1e-4);
  853. CHECK_CLOSE (0, pos[2], 1e-4);
  854. const dReal *q2_12 = dBodyGetQuaternion(bId2_12);
  855. const dReal *q = dBodyGetQuaternion(bId);
  856. CHECK_CLOSE (q2_12[0], q[0], 1e-4);
  857. CHECK_CLOSE (q2_12[1], q[1], 1e-4);
  858. CHECK_CLOSE (q2_12[2], q[2], 1e-4);
  859. CHECK_CLOSE (q2_12[3], q[3], 1e-4);
  860. }
  861. } // End of SUITE TestdxJointPR