pu.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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/pu.cpp
  27. //
  28. //
  29. ////////////////////////////////////////////////////////////////////////////////
  30. #include <UnitTest++.h>
  31. #include <ode/ode.h>
  32. #include "../../ode/src/config.h"
  33. #include "../../ode/src/joints/pu.h"
  34. SUITE (TestdxJointPU)
  35. {
  36. // The 2 bodies are positionned at (0, 0, 0), and (0, 0, 0)
  37. // The second body has a rotation of 27deg around X axis.
  38. // The joint is a PU Joint
  39. // Axis is along the X axis
  40. // Anchor at (0, 0, 0)
  41. struct Fixture_dxJointPU_B1_and_B2_At_Zero_Axis_Along_X
  42. {
  43. Fixture_dxJointPU_B1_and_B2_At_Zero_Axis_Along_X()
  44. {
  45. wId = dWorldCreate();
  46. bId1 = dBodyCreate (wId);
  47. dBodySetPosition (bId1, 0, 0, 0);
  48. bId2 = dBodyCreate (wId);
  49. dBodySetPosition (bId2, 0, 0, 0);
  50. dMatrix3 R;
  51. dRFromAxisAndAngle (R, 1, 0, 0, REAL(0.47123)); // 27deg
  52. dBodySetRotation (bId2, R);
  53. jId = dJointCreatePU (wId, 0);
  54. joint = (dxJointPU*) jId;
  55. dJointAttach (jId, bId1, bId2);
  56. }
  57. ~Fixture_dxJointPU_B1_and_B2_At_Zero_Axis_Along_X()
  58. {
  59. dWorldDestroy (wId);
  60. }
  61. dWorldID wId;
  62. dBodyID bId1;
  63. dBodyID bId2;
  64. dJointID jId;
  65. dxJointPU* joint;
  66. };
  67. // Test is dJointSetPUAxis and dJointGetPUAxis return same value
  68. TEST_FIXTURE (Fixture_dxJointPU_B1_and_B2_At_Zero_Axis_Along_X,
  69. test_dJointSetGetPUAxis)
  70. {
  71. dVector3 axisOrig, axis;
  72. dJointGetPUAxis1 (jId, axisOrig);
  73. dJointGetPUAxis1 (jId, axis);
  74. dJointSetPUAxis1 (jId, axis[0], axis[1], axis[2]);
  75. dJointGetPUAxis1 (jId, axis);
  76. CHECK_CLOSE (axis[0], axisOrig[0] , 1e-4);
  77. CHECK_CLOSE (axis[1], axisOrig[1] , 1e-4);
  78. CHECK_CLOSE (axis[2], axisOrig[2] , 1e-4);
  79. dJointGetPUAxis2 (jId, axisOrig);
  80. dJointGetPUAxis2(jId, axis);
  81. dJointSetPUAxis2 (jId, axis[0], axis[1], axis[2]);
  82. dJointGetPUAxis2 (jId, axis);
  83. CHECK_CLOSE (axis[0], axisOrig[0] , 1e-4);
  84. CHECK_CLOSE (axis[1], axisOrig[1] , 1e-4);
  85. CHECK_CLOSE (axis[2], axisOrig[2] , 1e-4);
  86. dJointGetPUAxis3 (jId, axisOrig);
  87. dJointGetPUAxis3(jId, axis);
  88. dJointSetPUAxis3 (jId, axis[0], axis[1], axis[2]);
  89. dJointGetPUAxis3 (jId, axis);
  90. CHECK_CLOSE (axis[0], axisOrig[0] , 1e-4);
  91. CHECK_CLOSE (axis[1], axisOrig[1] , 1e-4);
  92. CHECK_CLOSE (axis[2], axisOrig[2] , 1e-4);
  93. }
  94. // The joint is a PU Joint
  95. // Default joint value
  96. // The two bodies at at (0, 0, 0)
  97. struct Fixture_dxJointPU_B1_and_B2_At_Zero
  98. {
  99. Fixture_dxJointPU_B1_and_B2_At_Zero()
  100. {
  101. wId = dWorldCreate();
  102. bId1 = dBodyCreate (wId);
  103. dBodySetPosition (bId1, 0, 0, 0);
  104. bId2 = dBodyCreate (wId);
  105. dBodySetPosition (bId2, 0, 0, 0);
  106. jId = dJointCreatePU (wId, 0);
  107. joint = (dxJointPU*) jId;
  108. dJointAttach (jId, bId1, bId2);
  109. }
  110. ~Fixture_dxJointPU_B1_and_B2_At_Zero()
  111. {
  112. dWorldDestroy (wId);
  113. }
  114. dWorldID wId;
  115. dBodyID bId1;
  116. dBodyID bId2;
  117. dJointID jId;
  118. dxJointPU* joint;
  119. static const dReal offset;
  120. };
  121. const dReal Fixture_dxJointPU_B1_and_B2_At_Zero::offset = REAL (3.1);
  122. // Move 1st body offset unit in the X direction
  123. //
  124. // X-------> X---------> Axis -->
  125. // B1 => B1
  126. // B2 B2
  127. //
  128. // Start with a Offset of offset unit
  129. //
  130. // X-------> X---------> Axis -->
  131. // B1 => B1
  132. // B2 B2
  133. TEST_FIXTURE (Fixture_dxJointPU_B1_and_B2_At_Zero,
  134. test_dJointSetPUAxisOffset_B1_3Unit)
  135. {
  136. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  137. dBodySetPosition (bId1, offset, 0, 0);
  138. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  139. dVector3 axis;
  140. dJointGetPUAxisP (jId, axis);
  141. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  142. offset*axis[0],offset*axis[1],offset*axis[2]);
  143. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  144. dBodySetPosition (bId1, 0, 0, 0);
  145. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  146. }
  147. // Move 1st body offset unit in the opposite X direction
  148. //
  149. // X-------> X---------> Axis -->
  150. // B1 => B1
  151. // B2 B2
  152. //
  153. // Start with a Offset of -offset unit
  154. //
  155. // X-------> X---------> Axis -->
  156. // B1 => B1
  157. // B2 B2
  158. TEST_FIXTURE (Fixture_dxJointPU_B1_and_B2_At_Zero,
  159. test_dJointSetPUAxisOffset_B1_Minus_3Unit)
  160. {
  161. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  162. dBodySetPosition (bId1, -offset, 0, 0);
  163. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  164. dVector3 axis;
  165. dJointGetPUAxisP (jId, axis);
  166. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  167. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  168. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  169. dBodySetPosition (bId1, 0, 0, 0);
  170. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  171. }
  172. // Move 2nd body offset unit in the X direction
  173. //
  174. // X-------> X---------> Axis -->
  175. // B1 => B1
  176. // B2 B2
  177. //
  178. // Start with a Offset of offset unit
  179. //
  180. // X-------> X---------> Axis -->
  181. // B1 => B1
  182. // B2 B2
  183. TEST_FIXTURE (Fixture_dxJointPU_B1_and_B2_At_Zero,
  184. test_dJointSetPUAxisOffset_B2_3Unit)
  185. {
  186. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  187. dBodySetPosition (bId2, offset, 0, 0);
  188. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  189. dVector3 axis;
  190. dJointGetPUAxisP (jId, axis);
  191. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  192. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  193. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  194. dBodySetPosition (bId2, 0, 0, 0);
  195. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  196. }
  197. // Move 2nd body offset unit in the opposite X direction
  198. //
  199. // X-------> X---------> Axis -->
  200. // B1 => B1
  201. // B2 B2
  202. //
  203. // Start with a Offset of -offset unit
  204. //
  205. // X-------> X---------> Axis -->
  206. // B1 => B1
  207. // B2 B2
  208. TEST_FIXTURE (Fixture_dxJointPU_B1_and_B2_At_Zero,
  209. test_dJointSetPUAxisOffset_B2_Minus_3Unit)
  210. {
  211. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  212. dBodySetPosition (bId2, -offset, 0, 0);
  213. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  214. dVector3 axis;
  215. dJointGetPUAxisP (jId, axis);
  216. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  217. offset*axis[0],offset*axis[1],offset*axis[2]);
  218. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  219. dBodySetPosition (bId2, 0, 0, 0);
  220. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  221. }
  222. // Attach only one body at position 1 to the joint dJointAttach (jId, bId, 0)
  223. // Move 1st body offset unit in the 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_dxJointPU_B1_and_B2_At_Zero,
  233. test_dJointSetPUAxisOffset_B1_OffsetUnit)
  234. {
  235. dJointAttach (jId, bId1, 0);
  236. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  237. dBodySetPosition (bId1, offset, 0, 0);
  238. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  239. dVector3 axis;
  240. dJointGetPUAxisP (jId, axis);
  241. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  242. offset*axis[0],offset*axis[1],offset*axis[2]);
  243. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  244. dBodySetPosition (bId1, 0, 0, 0);
  245. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  246. }
  247. // Attache only one body at position 1 to the joint dJointAttach (jId, bId, 0)
  248. // Move 1st body offset unit in the opposite X direction
  249. //
  250. // X-------> X---------> Axis -->
  251. // B1 => B1
  252. //
  253. // Start with a Offset of -offset unit
  254. //
  255. // X-------> X---------> Axis -->
  256. // B1 => B1
  257. TEST_FIXTURE (Fixture_dxJointPU_B1_and_B2_At_Zero,
  258. test_dJointSetPUAxisOffset_B1_Minus_OffsetUnit)
  259. {
  260. dJointAttach (jId, bId1, 0);
  261. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  262. dBodySetPosition (bId1, -offset, 0, 0);
  263. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  264. dVector3 axis;
  265. dJointGetPUAxisP (jId, axis);
  266. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  267. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  268. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  269. dBodySetPosition (bId1, 0, 0, 0);
  270. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  271. }
  272. // Attache only one body at position 2 to the joint dJointAttach (jId, 0, bId)
  273. // Move 1st body offset unit in the X direction
  274. //
  275. // X-------> X---------> Axis -->
  276. // B2 => B2
  277. //
  278. // Start with a Offset of offset unit
  279. //
  280. // X-------> X---------> Axis -->
  281. // B2 => B2
  282. TEST_FIXTURE (Fixture_dxJointPU_B1_and_B2_At_Zero,
  283. test_dJointSetPUAxisOffset_B2_OffsetUnit)
  284. {
  285. dJointAttach (jId, 0, bId2);
  286. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  287. dBodySetPosition (bId2, offset, 0, 0);
  288. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  289. dVector3 axis;
  290. dJointGetPUAxisP (jId, axis);
  291. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  292. -offset*axis[0], -offset*axis[1], -offset*axis[2]);
  293. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  294. dBodySetPosition (bId2, 0, 0, 0);
  295. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  296. }
  297. // Attache only one body at position 2 to the joint dJointAttach (jId, 0, bId)
  298. // Move 1st body offset unit in the opposite X direction
  299. //
  300. // X-------> X---------> Axis -->
  301. // B2 => B2
  302. //
  303. // Start with a Offset of -offset unit
  304. //
  305. // X-------> X---------> Axis -->
  306. // B2 => B2
  307. TEST_FIXTURE (Fixture_dxJointPU_B1_and_B2_At_Zero,
  308. test_dJointSetPUAxisOffset_B2_Minus_OffsetUnit)
  309. {
  310. dJointAttach (jId, 0, bId2);
  311. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  312. dBodySetPosition (bId2, -offset, 0, 0);
  313. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  314. dVector3 axis;
  315. dJointGetPUAxisP (jId, axis);
  316. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  317. offset*axis[0], offset*axis[1], offset*axis[2]);
  318. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  319. dBodySetPosition (bId2, 0, 0, 0);
  320. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  321. }
  322. // Only one body
  323. // The body are positionned at (0, 0, 0), with no rotation
  324. // The joint is a PU Joint
  325. // Axis is in the oppsite X axis
  326. // Anchor at (0, 0, 0)
  327. // N.B. By default the body is attached at position 1 on the joint
  328. // dJointAttach (jId, bId, 0);
  329. struct Fixture_dxJointPU_One_Body_At_Zero_Axis_Inverse_of_X
  330. {
  331. Fixture_dxJointPU_One_Body_At_Zero_Axis_Inverse_of_X()
  332. {
  333. wId = dWorldCreate();
  334. bId = dBodyCreate (wId);
  335. dBodySetPosition (bId, 0, 0, 0);
  336. jId = dJointCreatePU (wId, 0);
  337. joint = (dxJointPU*) jId;
  338. dJointAttach (jId, bId, NULL);
  339. dJointSetPUAxisP (jId, axis[0], axis[1], axis[2]);
  340. }
  341. ~Fixture_dxJointPU_One_Body_At_Zero_Axis_Inverse_of_X()
  342. {
  343. dWorldDestroy (wId);
  344. }
  345. dWorldID wId;
  346. dBodyID bId;
  347. dJointID jId;
  348. dxJointPU* joint;
  349. static const dVector3 axis;
  350. static const dReal offset;
  351. };
  352. const dVector3 Fixture_dxJointPU_One_Body_At_Zero_Axis_Inverse_of_X::axis =
  353. {
  354. -1, 0, 0
  355. };
  356. const dReal Fixture_dxJointPU_One_Body_At_Zero_Axis_Inverse_of_X::offset = REAL (3.1);
  357. // Move 1st body offset unit in the X direction
  358. //
  359. // X-------> X---------> <--- Axis
  360. // B1 => B1
  361. //
  362. // Start with a Offset of offset unit
  363. //
  364. // X-------> X---------> <--- Axis
  365. // B1 => B1
  366. TEST_FIXTURE (Fixture_dxJointPU_One_Body_At_Zero_Axis_Inverse_of_X,
  367. test_dJointSetPUAxisOffset_B1_At_Position_1_OffsetUnit)
  368. {
  369. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  370. dBodySetPosition (bId, offset, 0, 0);
  371. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  372. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  373. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  374. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  375. dBodySetPosition (bId, 0, 0, 0);
  376. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  377. }
  378. // Move 1st body offset unit in the opposite X direction
  379. //
  380. // X-------> X---------> <--- Axis
  381. // B1 => B1
  382. //
  383. // Start with a Offset of -offset unit
  384. //
  385. // X-------> X---------> <--- Axis
  386. // B1 => B1
  387. TEST_FIXTURE (Fixture_dxJointPU_One_Body_At_Zero_Axis_Inverse_of_X,
  388. test_dJointSetPUAxisOffset_B1_Minus_OffsetUnit)
  389. {
  390. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  391. dBodySetPosition (bId, -offset, 0, 0);
  392. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  393. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  394. offset*axis[0],offset*axis[1],offset*axis[2]);
  395. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  396. dBodySetPosition (bId, 0, 0, 0);
  397. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  398. }
  399. // Move 1st body offset unit in the X direction
  400. //
  401. // X-------> X---------> <--- Axis
  402. // B2 => B2
  403. //
  404. // Start with a Offset of offset unit
  405. //
  406. // X-------> X---------> <--- Axis
  407. // B2 => B2
  408. TEST_FIXTURE (Fixture_dxJointPU_One_Body_At_Zero_Axis_Inverse_of_X,
  409. test_dJointSetPUAxisOffset_B2_OffsetUnit)
  410. {
  411. // By default it is attached to position 1
  412. // Now attach the body at positiojn 2
  413. dJointAttach(jId, 0, bId);
  414. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  415. dBodySetPosition (bId, offset, 0, 0);
  416. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  417. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  418. offset*axis[0], offset*axis[1], offset*axis[2]);
  419. CHECK_CLOSE (offset, dJointGetPUPosition (jId), 1e-4);
  420. dBodySetPosition (bId, 0, 0, 0);
  421. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  422. }
  423. // Move 1st body offset unit in the opposite X direction
  424. //
  425. // X-------> X---------> <--- Axis
  426. // B2 => B2
  427. //
  428. // Start with a Offset of -offset unit
  429. //
  430. // X-------> X---------> <--- Axis
  431. // B2 => B2
  432. TEST_FIXTURE (Fixture_dxJointPU_One_Body_At_Zero_Axis_Inverse_of_X,
  433. test_dJointSetPUAxisOffset_B2_Minus_OffsetUnit)
  434. {
  435. // By default it is attached to position 1
  436. // Now attach the body at positiojn 2
  437. dJointAttach(jId, 0, bId);
  438. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  439. dBodySetPosition (bId, -offset, 0, 0);
  440. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  441. dJointSetPUAnchorOffset (jId, 0, 0, 0,
  442. -offset*axis[0], -offset*axis[1], -offset*axis[2]);
  443. CHECK_CLOSE (-offset, dJointGetPUPosition (jId), 1e-4);
  444. dBodySetPosition (bId, 0, 0, 0);
  445. CHECK_CLOSE (0.0, dJointGetPUPosition (jId), 1e-4);
  446. }
  447. // Compare only one body to 2 bodies with one fixed.
  448. //
  449. // The body are positionned at (0, 0, 0), with no rotation
  450. // The joint is a PU Joint with default values
  451. struct Fixture_dxJointPU_Compare_One_Body_To_Two_Bodies_At_Zero
  452. {
  453. Fixture_dxJointPU_Compare_One_Body_To_Two_Bodies_At_Zero()
  454. {
  455. wId = dWorldCreate();
  456. bId1_12 = dBodyCreate (wId);
  457. dBodySetPosition (bId1_12, 0, 0, 0);
  458. bId2_12 = dBodyCreate (wId);
  459. dBodySetPosition (bId2_12, 0, 0, 0);
  460. // The force will be added in the function since it is not
  461. // always on the same body
  462. jId_12 = dJointCreatePU (wId, 0);
  463. dJointAttach(jId_12, bId1_12, bId2_12);
  464. fixed = dJointCreateFixed (wId, 0);
  465. jId = dJointCreatePU (wId, 0);
  466. bId = dBodyCreate (wId);
  467. dBodySetPosition (bId, 0, 0, 0);
  468. // Linear velocity along the prismatic axis;
  469. dVector3 axis;
  470. dJointGetPUAxisP(jId_12, axis);
  471. dJointSetPUAxisP(jId, axis[0], axis[1], axis[2]);
  472. dBodySetLinearVel (bId, magnitude*axis[0], magnitude*axis[1], magnitude*axis[2]);
  473. }
  474. ~Fixture_dxJointPU_Compare_One_Body_To_Two_Bodies_At_Zero()
  475. {
  476. dWorldDestroy (wId);
  477. }
  478. dWorldID wId;
  479. dBodyID bId1_12;
  480. dBodyID bId2_12;
  481. dJointID jId_12; // Joint with 2 bodies
  482. dJointID fixed;
  483. dBodyID bId;
  484. dJointID jId; // Joint with one body
  485. static const dReal magnitude;
  486. };
  487. const dReal Fixture_dxJointPU_Compare_One_Body_To_Two_Bodies_At_Zero::magnitude = REAL (4.27);
  488. TEST_FIXTURE (Fixture_dxJointPU_Compare_One_Body_To_Two_Bodies_At_Zero,
  489. test_dJointSetPUPositionRate_Only_B1)
  490. {
  491. // Linear velocity along the prismatic axis;
  492. dVector3 axis;
  493. dJointGetPUAxisP(jId_12, axis);
  494. dBodySetLinearVel (bId1_12, magnitude*axis[0], magnitude*axis[1], magnitude*axis[2]);
  495. dJointAttach(jId_12, bId1_12, bId2_12);
  496. dJointAttach(fixed, 0, bId2_12);
  497. dJointSetFixed(fixed);
  498. dJointAttach(jId, bId, 0);
  499. CHECK_CLOSE(dJointGetPUPositionRate(jId_12), dJointGetPUPositionRate(jId), 1e-2);
  500. }
  501. TEST_FIXTURE (Fixture_dxJointPU_Compare_One_Body_To_Two_Bodies_At_Zero,
  502. test_dJointSetPUPositionRate_Only_B2)
  503. {
  504. // Linear velocity along the prismatic axis;
  505. dVector3 axis;
  506. dJointGetPUAxisP(jId_12, axis);
  507. dBodySetLinearVel (bId2_12, magnitude*axis[0], magnitude*axis[1], magnitude*axis[2]);
  508. dJointAttach(jId_12, bId1_12, bId2_12);
  509. dJointAttach(fixed, bId1_12, 0);
  510. dJointSetFixed(fixed);
  511. dJointAttach(jId, 0, bId);
  512. CHECK_CLOSE(dJointGetPUPositionRate(jId_12), dJointGetPUPositionRate(jId), 1e-2);
  513. }
  514. // This test compare the result of a pu joint with 2 bodies where body body 2 is
  515. // fixed to the world to a pu joint with only one body at position 1.
  516. //
  517. // Test the limits [-1, 0.25] when only one body at is attached to the joint
  518. // using dJointAttache(jId, bId, 0);
  519. //
  520. TEST_FIXTURE(Fixture_dxJointPU_Compare_One_Body_To_Two_Bodies_At_Zero,
  521. test_Limit_minus1_025_One_Body_on_left)
  522. {
  523. dVector3 axis;
  524. dJointGetPUAxisP(jId_12, axis);
  525. dJointSetPUAxisP(jId, axis[0], axis[1], axis[2]);
  526. dBodySetLinearVel (bId1_12, magnitude*axis[0], magnitude*axis[1], magnitude*axis[2]);
  527. dJointAttach(jId_12, bId1_12, bId2_12);
  528. dJointSetPUParam(jId_12, dParamLoStop3, -1);
  529. dJointSetPUParam(jId_12, dParamHiStop3, 0.25);
  530. dJointAttach(fixed, 0, bId2_12);
  531. dJointSetFixed(fixed);
  532. dJointAttach(jId, bId, 0);
  533. dJointSetPUParam(jId, dParamLoStop3, -1);
  534. dJointSetPUParam(jId, dParamHiStop3, 0.25);
  535. for (int i=0; i<50; ++i)
  536. dWorldStep(wId, 1.0);
  537. const dReal *pos1_12 = dBodyGetPosition(bId1_12);
  538. const dReal *pos = dBodyGetPosition(bId);
  539. CHECK_CLOSE (pos1_12[0], pos[0], 1e-2);
  540. CHECK_CLOSE (pos1_12[1], pos[1], 1e-2);
  541. CHECK_CLOSE (pos1_12[2], pos[2], 1e-2);
  542. const dReal *q1_12 = dBodyGetQuaternion(bId1_12);
  543. const dReal *q = dBodyGetQuaternion(bId);
  544. CHECK_CLOSE (q1_12[0], q[0], 1e-4);
  545. CHECK_CLOSE (q1_12[1], q[1], 1e-4);
  546. CHECK_CLOSE (q1_12[2], q[2], 1e-4);
  547. CHECK_CLOSE (q1_12[3], q[3], 1e-4);
  548. // Should be different than zero
  549. CHECK( dJointGetPUPosition(jId_12) );
  550. CHECK( dJointGetPUPosition(jId) );
  551. CHECK( dJointGetPUPositionRate(jId_12) );
  552. CHECK( dJointGetPUPositionRate(jId) );
  553. }
  554. // This test compare the result of a pu joint with 2 bodies where body body 1 is
  555. // fixed to the world to a pu joint with only one body at position 2.
  556. //
  557. // Test the limits [-1, 0.25] when only one body at is attached to the joint
  558. // using dJointAttache(jId, 0, bId);
  559. //
  560. TEST_FIXTURE(Fixture_dxJointPU_Compare_One_Body_To_Two_Bodies_At_Zero,
  561. test_Limit_minus1_025_One_Body_on_right)
  562. {
  563. dVector3 axis;
  564. dJointGetPUAxisP(jId_12, axis);
  565. dJointSetPUAxisP(jId, axis[0], axis[1], axis[2]);
  566. dBodySetLinearVel (bId2_12, magnitude*axis[0], magnitude*axis[1], magnitude*axis[2]);
  567. dJointAttach(jId_12, bId1_12, bId2_12);
  568. dJointSetPUParam(jId_12, dParamLoStop3, -1);
  569. dJointSetPUParam(jId_12, dParamHiStop3, 0.25);
  570. dJointAttach(fixed, bId1_12, 0);
  571. dJointSetFixed(fixed);
  572. dJointAttach(jId, 0, bId);
  573. dJointSetPUParam(jId, dParamLoStop3, -1);
  574. dJointSetPUParam(jId, dParamHiStop3, 0.25);
  575. for (int i=0; i<50; ++i)
  576. dWorldStep(wId, 1.0);
  577. const dReal *pos2_12 = dBodyGetPosition(bId2_12);
  578. const dReal *pos = dBodyGetPosition(bId);
  579. CHECK_CLOSE (pos2_12[0], pos[0], 1e-2);
  580. CHECK_CLOSE (pos2_12[1], pos[1], 1e-2);
  581. CHECK_CLOSE (pos2_12[2], pos[2], 1e-2);
  582. const dReal *q2_12 = dBodyGetQuaternion(bId2_12);
  583. const dReal *q = dBodyGetQuaternion(bId);
  584. CHECK_CLOSE (q2_12[0], q[0], 1e-4);
  585. CHECK_CLOSE (q2_12[1], q[1], 1e-4);
  586. CHECK_CLOSE (q2_12[2], q[2], 1e-4);
  587. CHECK_CLOSE (q2_12[3], q[3], 1e-4);
  588. // Should be different than zero
  589. CHECK( dJointGetPUPosition(jId_12) );
  590. CHECK( dJointGetPUPosition(jId) );
  591. CHECK( dJointGetPUPositionRate(jId_12) );
  592. CHECK( dJointGetPUPositionRate(jId) );
  593. }
  594. // This test compare the result of a pu joint with 2 bodies where body 2 is
  595. // fixed to the world to a pu joint with only one body at position 1.
  596. //
  597. // Test the limits [0, 0] when only one body at is attached to the joint
  598. // using dJointAttache(jId, bId, 0);
  599. //
  600. // The body should not move since their is no room between the two limits
  601. //
  602. TEST_FIXTURE(Fixture_dxJointPU_Compare_One_Body_To_Two_Bodies_At_Zero,
  603. test_Limit_0_0_One_Body_on_left)
  604. {
  605. dVector3 axis;
  606. dJointGetPUAxisP(jId_12, axis);
  607. dJointSetPUAxisP(jId, axis[0], axis[1], axis[2]);
  608. dBodySetLinearVel (bId1_12, magnitude*axis[0], magnitude*axis[1], magnitude*axis[2]);
  609. dJointAttach(jId_12, bId1_12, bId2_12);
  610. dJointSetPUParam(jId_12, dParamLoStop3, 0);
  611. dJointSetPUParam(jId_12, dParamHiStop3, 0);
  612. dJointAttach(fixed, 0, bId2_12);
  613. dJointSetFixed(fixed);
  614. dJointAttach(jId, bId, 0);
  615. dJointSetPUParam(jId, dParamLoStop3, 0);
  616. dJointSetPUParam(jId, dParamHiStop3, 0);
  617. for (int i=0; i<500; ++i)
  618. dWorldStep(wId, 1.0);
  619. const dReal *pos1_12 = dBodyGetPosition(bId1_12);
  620. const dReal *pos = dBodyGetPosition(bId);
  621. CHECK_CLOSE (pos1_12[0], pos[0], 1e-4);
  622. CHECK_CLOSE (pos1_12[1], pos[1], 1e-4);
  623. CHECK_CLOSE (pos1_12[2], pos[2], 1e-4);
  624. CHECK_CLOSE (0, pos[0], 1e-4);
  625. CHECK_CLOSE (0, pos[1], 1e-4);
  626. CHECK_CLOSE (0, pos[2], 1e-4);
  627. const dReal *q1_12 = dBodyGetQuaternion(bId1_12);
  628. const dReal *q = dBodyGetQuaternion(bId);
  629. CHECK_CLOSE (q1_12[0], q[0], 1e-4);
  630. CHECK_CLOSE (q1_12[1], q[1], 1e-4);
  631. CHECK_CLOSE (q1_12[2], q[2], 1e-4);
  632. CHECK_CLOSE (q1_12[3], q[3], 1e-4);
  633. }
  634. // This test compare the result of a pu joint with 2 bodies where body body 1 is
  635. // fixed to the world to a pu joint with only one body at position 2.
  636. //
  637. // Test the limits [0, 0] when only one body at is attached to the joint
  638. // using dJointAttache(jId, 0, bId);
  639. //
  640. // The body should not move since their is no room between the two limits
  641. //
  642. TEST_FIXTURE(Fixture_dxJointPU_Compare_One_Body_To_Two_Bodies_At_Zero,
  643. test_Limit_0_0_One_Body_on_right)
  644. {
  645. dVector3 axis;
  646. dJointGetPUAxisP(jId_12, axis);
  647. dJointSetPUAxisP(jId, axis[0], axis[1], axis[2]);
  648. dBodySetLinearVel (bId2_12, magnitude*axis[0], magnitude*axis[1], magnitude*axis[2]);
  649. dJointAttach(jId_12, bId1_12, bId2_12);
  650. dJointSetPUParam(jId_12, dParamLoStop3, 0);
  651. dJointSetPUParam(jId_12, dParamHiStop3, 0);
  652. dJointAttach(fixed, bId1_12, 0);
  653. dJointSetFixed(fixed);
  654. dJointAttach(jId, 0, bId);
  655. dJointSetPUParam(jId, dParamLoStop3, 0);
  656. dJointSetPUParam(jId, dParamHiStop3, 0);
  657. for (int i=0; i<500; ++i)
  658. dWorldStep(wId, 1.0);
  659. const dReal *pos2_12 = dBodyGetPosition(bId2_12);
  660. const dReal *pos = dBodyGetPosition(bId);
  661. CHECK_CLOSE (pos2_12[0], pos[0], 1e-4);
  662. CHECK_CLOSE (pos2_12[1], pos[1], 1e-4);
  663. CHECK_CLOSE (pos2_12[2], pos[2], 1e-4);
  664. CHECK_CLOSE (0, pos[0], 1e-4);
  665. CHECK_CLOSE (0, pos[1], 1e-4);
  666. CHECK_CLOSE (0, pos[2], 1e-4);
  667. const dReal *q2_12 = dBodyGetQuaternion(bId2_12);
  668. const dReal *q = dBodyGetQuaternion(bId);
  669. CHECK_CLOSE (q2_12[0], q[0], 1e-4);
  670. CHECK_CLOSE (q2_12[1], q[1], 1e-4);
  671. CHECK_CLOSE (q2_12[2], q[2], 1e-4);
  672. CHECK_CLOSE (q2_12[3], q[3], 1e-4);
  673. }
  674. } // End of SUITE TestdxJointPU