pu.cpp 28 KB

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