hinge.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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/hinge.cpp
  27. //
  28. //
  29. ////////////////////////////////////////////////////////////////////////////////
  30. #include <UnitTest++.h>
  31. #include <ode/ode.h>
  32. #include "../../ode/src/joints/hinge.h"
  33. SUITE (TestdxJointHinge)
  34. {
  35. // The 2 bodies are positionned at (0, 0, 0), with no rotation
  36. // The joint is an Hinge Joint
  37. // Axis is along the X axis
  38. // Anchor at (0, 0, 0)
  39. // ^Y
  40. // |
  41. // |
  42. // |
  43. // |
  44. // |
  45. // Z <---- . (X going out of the page)
  46. struct dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Along_X {
  47. dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Along_X()
  48. {
  49. wId = dWorldCreate();
  50. bId1 = dBodyCreate (wId);
  51. dBodySetPosition (bId1, 0, 0, 0);
  52. bId2 = dBodyCreate (wId);
  53. dBodySetPosition (bId2, 0, 0, 0);
  54. jId = dJointCreateHinge (wId, 0);
  55. joint = (dxJointHinge*) jId;
  56. dJointAttach (jId, bId1, bId2);
  57. dJointSetHingeAnchor (jId, 0, 0, 0);
  58. axis[0] = 1;
  59. axis[1] = 0;
  60. axis[2] = 0;
  61. }
  62. ~dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Along_X()
  63. {
  64. dWorldDestroy (wId);
  65. }
  66. dWorldID wId;
  67. dBodyID bId1;
  68. dBodyID bId2;
  69. dJointID jId;
  70. dxJointHinge* joint;
  71. dVector3 axis;
  72. };
  73. // Rotate 2nd body 90deg around X then back to original position
  74. //
  75. // ^ ^ ^
  76. // | | => | <---
  77. // | | |
  78. // B1 B2 B1 B2
  79. //
  80. // Start with a Delta of 90deg
  81. // ^ ^ ^
  82. // | <--- => | |
  83. // | | |
  84. // B1 B2 B1 B2
  85. TEST_FIXTURE (dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Along_X,
  86. test_dJointSetHingeAxisOffset_B2_90deg) {
  87. dMatrix3 R;
  88. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  89. dRFromAxisAndAngle (R, 1, 0, 0, M_PI/2.0);
  90. dBodySetRotation (bId2, R);
  91. CHECK_CLOSE (-M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  92. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], -M_PI/2.0);
  93. CHECK_CLOSE (-M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  94. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  95. dBodySetRotation (bId2, R);
  96. CHECK_CLOSE (0.0, dJointGetHingeAngle (jId), 1e-4);
  97. }
  98. // Rotate 2nd body -90deg around X then back to original position
  99. //
  100. // ^ ^ ^
  101. // | | => | --->
  102. // | | |
  103. // B1 B2 B1 B2
  104. //
  105. // Start with a Delta of 90deg
  106. // ^ ^ ^
  107. // | ---> => | |
  108. // | | |
  109. // B1 B2 B1 B2
  110. TEST_FIXTURE (dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Along_X,
  111. test_dJointSetHingeAxisOffset_B2_Minus90deg) {
  112. dMatrix3 R;
  113. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  114. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  115. dRFromAxisAndAngle (R, 1, 0, 0, -M_PI/2.0);
  116. dBodySetRotation (bId2, R);
  117. CHECK_CLOSE (M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  118. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], M_PI/2.0);
  119. CHECK_CLOSE (M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  120. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  121. dBodySetRotation (bId2, R);
  122. CHECK_CLOSE (0.0, dJointGetHingeAngle (jId), 1e-4);
  123. }
  124. // Rotate 1st body 0.23rad around X then back to original position
  125. //
  126. // ^ ^ ^ ^
  127. // | | => \ |
  128. // | | \ |
  129. // B1 B2 B1 B2
  130. //
  131. // Start with a Delta of 0.23rad
  132. // ^ ^ ^ ^
  133. // \ | => | |
  134. // \ | | |
  135. // B1 B2 B1 B2
  136. TEST_FIXTURE (dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Along_X,
  137. test_dJointSetHingeAxisOffset_B1_0_23rad) {
  138. dMatrix3 R;
  139. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  140. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  141. dRFromAxisAndAngle (R, 1, 0, 0, REAL(0.23) );
  142. dBodySetRotation (bId1, R);
  143. CHECK_CLOSE (REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  144. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], REAL(0.23));
  145. CHECK_CLOSE (REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  146. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  147. dBodySetRotation (bId1, R);
  148. CHECK_CLOSE (0.0, dJointGetHingeAngle (jId), 1e-4);
  149. }
  150. // Rotate 1st body -0.23rad around Z then back to original position
  151. //
  152. // ^ ^ ^ ^
  153. // | | => / |
  154. // | | / |
  155. // B1 B2 B1 B2
  156. //
  157. // Start with a Delta of 0.23rad
  158. // ^ ^ ^ ^
  159. // / | => | |
  160. // / | | |
  161. // B1 B2 B1 B2
  162. TEST_FIXTURE (dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Along_X,
  163. test_dJointSetHingeAxisOffset_B1_Minus0_23rad) {
  164. dMatrix3 R;
  165. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  166. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  167. dRFromAxisAndAngle (R, 1, 0, 0, -REAL(0.23));
  168. dBodySetRotation (bId1, R);
  169. CHECK_CLOSE (-REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  170. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], -REAL(0.23));
  171. CHECK_CLOSE (-REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  172. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  173. dBodySetRotation (bId1, R);
  174. CHECK_CLOSE (0.0, dJointGetHingeAngle (jId), 1e-4);
  175. }
  176. // The 2 bodies are positionned at (0, 0, 0), with no rotation
  177. // The joint is an Hinge Joint.
  178. // Axis in the inverse direction of the X axis
  179. // Anchor at (0, 0, 0)
  180. // ^Y
  181. // |
  182. // |
  183. // |
  184. // |
  185. // |
  186. // Z <---- x (X going out of the page)
  187. struct dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Inverse_of_X {
  188. dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Inverse_of_X()
  189. {
  190. wId = dWorldCreate();
  191. bId1 = dBodyCreate (wId);
  192. dBodySetPosition (bId1, 0, -1, 0);
  193. bId2 = dBodyCreate (wId);
  194. dBodySetPosition (bId2, 0, 1, 0);
  195. jId = dJointCreateHinge (wId, 0);
  196. joint = (dxJointHinge*) jId;
  197. dJointAttach (jId, bId1, bId2);
  198. dJointSetHingeAnchor (jId, 0, 0, 0);
  199. axis[0] = -1;
  200. axis[1] = 0;
  201. axis[2] = 0;
  202. }
  203. ~dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Inverse_of_X()
  204. {
  205. dWorldDestroy (wId);
  206. }
  207. dWorldID wId;
  208. dBodyID bId1;
  209. dBodyID bId2;
  210. dJointID jId;
  211. dxJointHinge* joint;
  212. dVector3 axis;
  213. };
  214. // Rotate 2nd body 90deg around X then back to original position
  215. //
  216. // ^ ^ ^
  217. // | | => | <---
  218. // | | |
  219. // B1 B2 B1 B2
  220. //
  221. // Start with a Delta of 90deg
  222. // ^ ^ ^
  223. // | <--- => | |
  224. // | | |
  225. // B1 B2 B1 B2
  226. TEST_FIXTURE (dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  227. test_dJointSetHingeAxisOffset_B2_90Deg) {
  228. dMatrix3 R;
  229. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  230. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  231. dRFromAxisAndAngle (R, 1, 0, 0, M_PI/2.0);
  232. dBodySetRotation (bId2, R);
  233. CHECK_CLOSE (M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  234. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], M_PI/2.0);
  235. CHECK_CLOSE (M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  236. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  237. dBodySetRotation (bId2, R);
  238. CHECK_CLOSE (0.0, dJointGetHingeAngle (jId), 1e-4);
  239. }
  240. // Rotate 2nd body -90deg around X then back to original position
  241. //
  242. // ^ ^ ^
  243. // | | => | --->
  244. // | | |
  245. // B1 B2 B1 B2
  246. //
  247. // Start with a Delta of 90deg
  248. // ^ ^ ^
  249. // | ---> => | |
  250. // | | |
  251. // B1 B2 B1 B2
  252. TEST_FIXTURE (dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  253. test_dJointSetHingeAxisOffset_B2_Minus90Deg) {
  254. dMatrix3 R;
  255. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  256. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  257. dRFromAxisAndAngle (R, 1, 0, 0, -M_PI/2.0);
  258. dBodySetRotation (bId2, R);
  259. CHECK_CLOSE (-M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  260. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], -M_PI/2.0);
  261. CHECK_CLOSE (-M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  262. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  263. dBodySetRotation (bId2, R);
  264. CHECK_CLOSE (0.0, dJointGetHingeAngle (jId), 1e-4);
  265. }
  266. // Rotate 1st body 0.23rad around X then back to original position
  267. //
  268. // ^ ^ ^ ^
  269. // | | => \ |
  270. // | | \ |
  271. // B1 B2 B1 B2
  272. //
  273. // Start with a Delta of 0.23rad
  274. // ^ ^ ^ ^
  275. // \ | => | |
  276. // \ | | |
  277. // B1 B2 B1 B2
  278. TEST_FIXTURE (dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  279. test_dJointSetHingeAxisOffset_B1_0_23rad) {
  280. dMatrix3 R;
  281. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  282. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  283. dRFromAxisAndAngle (R, 1, 0, 0, REAL(0.23));
  284. dBodySetRotation (bId1, R);
  285. CHECK_CLOSE (-REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  286. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], -REAL(0.23));
  287. CHECK_CLOSE (-REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  288. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  289. dBodySetRotation (bId1, R);
  290. CHECK_CLOSE (0.0, dJointGetHingeAngle (jId), 1e-4);
  291. }
  292. // Rotate 2nd body -0.23rad around Z then back to original position
  293. //
  294. // ^ ^ ^ ^
  295. // | | => / |
  296. // | | / |
  297. // B1 B2 B1 B2
  298. //
  299. // Start with a Delta of 0.23rad
  300. // ^ ^ ^ ^
  301. // / | => | |
  302. // / | | |
  303. // B1 B2 B1 B2
  304. TEST_FIXTURE (dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  305. test_dJointSetHingeAxisOffset_B1_Minus0_23rad) {
  306. dMatrix3 R;
  307. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  308. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  309. dRFromAxisAndAngle (R, 1, 0, 0, -REAL(0.23));
  310. dBodySetRotation (bId1, R);
  311. CHECK_CLOSE (REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  312. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], REAL(0.23));
  313. CHECK_CLOSE (REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  314. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  315. dBodySetRotation (bId1, R);
  316. CHECK_CLOSE (0.0, dJointGetHingeAngle (jId), 1e-4);
  317. }
  318. // Only one body body1 at (0,0,0)
  319. // The joint is an Hinge Joint.
  320. // Axis is along the X axis
  321. // Anchor at (0, 0, 0)
  322. //
  323. // ^Y
  324. // |
  325. // |
  326. // |
  327. // |
  328. // |
  329. // Z <-- X
  330. struct dxJointHinge_Fixture_B1_At_Zero_Axis_Along_X {
  331. dxJointHinge_Fixture_B1_At_Zero_Axis_Along_X()
  332. {
  333. wId = dWorldCreate();
  334. bId1 = dBodyCreate (wId);
  335. dBodySetPosition (bId1, 0, 0, 0);
  336. jId = dJointCreateHinge (wId, 0);
  337. joint = (dxJointHinge*) jId;
  338. dJointAttach (jId, bId1, NULL);
  339. dJointSetHingeAnchor (jId, 0, 0, 0);
  340. axis[0] = 1;
  341. axis[1] = 0;
  342. axis[2] = 0;
  343. }
  344. ~dxJointHinge_Fixture_B1_At_Zero_Axis_Along_X()
  345. {
  346. dWorldDestroy (wId);
  347. }
  348. dWorldID wId;
  349. dBodyID bId1;
  350. dJointID jId;
  351. dxJointHinge* joint;
  352. dVector3 axis;
  353. };
  354. // Rotate B1 by 90deg around X then back to original position
  355. //
  356. // ^
  357. // | => <---
  358. // |
  359. // B1 B1
  360. //
  361. // Start with a Delta of 90deg
  362. // ^
  363. // <--- => |
  364. // |
  365. // B1 B1
  366. TEST_FIXTURE (dxJointHinge_Fixture_B1_At_Zero_Axis_Along_X,
  367. test_dJointSetHingeAxisOffset_1Body_B1_90Deg) {
  368. dMatrix3 R;
  369. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  370. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  371. dRFromAxisAndAngle (R, 1, 0, 0, M_PI/2.0);
  372. dBodySetRotation (bId1, R);
  373. CHECK_CLOSE (M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  374. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], M_PI/2.0);
  375. CHECK_CLOSE (M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  376. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  377. dBodySetRotation (bId1, R);
  378. CHECK_CLOSE (0, dJointGetHingeAngle (jId), 1e-4);
  379. }
  380. // Rotate B1 by -0.23rad around X then back to original position
  381. //
  382. // ^ ^
  383. // | => /
  384. // | /
  385. // B1 B1
  386. //
  387. // Start with a Delta of -0.23rad
  388. // ^ ^
  389. // / => |
  390. // / |
  391. // B1 B1
  392. TEST_FIXTURE (dxJointHinge_Fixture_B1_At_Zero_Axis_Along_X,
  393. test_dJointSetHingeAxisOffset_1Body_B1_Minus0_23rad) {
  394. dMatrix3 R;
  395. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  396. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  397. dRFromAxisAndAngle (R, 1, 0, 0, -REAL(0.23));
  398. dBodySetRotation (bId1, R);
  399. CHECK_CLOSE (-REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  400. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], -REAL(0.23));
  401. CHECK_CLOSE (-REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  402. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  403. dBodySetRotation (bId1, R);
  404. CHECK_CLOSE (0, dJointGetHingeAngle (jId), 1e-4);
  405. }
  406. // Only one body body1 at (0,0,0)
  407. // The joint is an Hinge Joint.
  408. // Axis the inverse of the X axis
  409. // Anchor at (0, 0, 0)
  410. //
  411. // ^Y
  412. // |
  413. // |
  414. // |
  415. // |
  416. // |
  417. // Z <-- X
  418. struct dxJointHinge_Fixture_B1_At_Zero_Axis_Inverse_of_X {
  419. dxJointHinge_Fixture_B1_At_Zero_Axis_Inverse_of_X()
  420. {
  421. wId = dWorldCreate();
  422. bId1 = dBodyCreate (wId);
  423. dBodySetPosition (bId1, 0, 0, 0);
  424. jId = dJointCreateHinge (wId, 0);
  425. joint = (dxJointHinge*) jId;
  426. dJointAttach (jId, bId1, NULL);
  427. dJointSetHingeAnchor (jId, 0, 0, 0);
  428. axis[0] = -1;
  429. axis[1] = 0;
  430. axis[2] = 0;
  431. }
  432. ~dxJointHinge_Fixture_B1_At_Zero_Axis_Inverse_of_X()
  433. {
  434. dWorldDestroy (wId);
  435. }
  436. dWorldID wId;
  437. dBodyID bId1;
  438. dJointID jId;
  439. dxJointHinge* joint;
  440. dVector3 axis;
  441. };
  442. // Rotate B1 by 90deg around X then back to original position
  443. //
  444. // ^
  445. // | => <---
  446. // |
  447. // B1 B1
  448. //
  449. // Start with a Delta of 90deg
  450. // ^
  451. // <--- => |
  452. // |
  453. // B1 B1
  454. TEST_FIXTURE (dxJointHinge_Fixture_B1_At_Zero_Axis_Inverse_of_X,
  455. test_dJointSetHingeAxisOffset_1Body_B1_90Deg) {
  456. dMatrix3 R;
  457. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  458. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  459. dRFromAxisAndAngle (R, 1, 0, 0, M_PI/2.0);
  460. dBodySetRotation (bId1, R);
  461. CHECK_CLOSE (-M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  462. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], -M_PI/2.0);
  463. CHECK_CLOSE (-M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  464. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  465. dBodySetRotation (bId1, R);
  466. CHECK_CLOSE (0, dJointGetHingeAngle (jId), 1e-4);
  467. }
  468. // Rotate B1 by -0.23rad around X then back to original position
  469. //
  470. // ^ ^
  471. // | => /
  472. // | /
  473. // B1 B1
  474. //
  475. // Start with a Delta of -0.23rad
  476. // ^ ^
  477. // / => |
  478. // / |
  479. // B1 B1
  480. TEST_FIXTURE (dxJointHinge_Fixture_B1_At_Zero_Axis_Inverse_of_X,
  481. test_dJointSetHingeAxisOffset_1Body_B1_Minus0_23rad) {
  482. dMatrix3 R;
  483. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  484. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  485. dRFromAxisAndAngle (R, 1, 0, 0, -REAL(0.23));
  486. dBodySetRotation (bId1, R);
  487. CHECK_CLOSE (REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  488. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], REAL(0.23));
  489. CHECK_CLOSE (REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  490. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  491. dBodySetRotation (bId1, R);
  492. CHECK_CLOSE (0, dJointGetHingeAngle (jId), 1e-4);
  493. }
  494. // Only one body body2 at (0,0,0)
  495. // The joint is an Hinge Joint.
  496. // Axis is along the X axis
  497. // Anchor at (0, 0, 0)
  498. //
  499. // ^Y
  500. // |
  501. // |
  502. // |
  503. // |
  504. // |
  505. // Z <-- X
  506. struct dxJointHinge_Fixture_B2_At_Zero_Axis_Along_X {
  507. dxJointHinge_Fixture_B2_At_Zero_Axis_Along_X()
  508. {
  509. wId = dWorldCreate();
  510. bId2 = dBodyCreate (wId);
  511. dBodySetPosition (bId2, 0, 0, 0);
  512. jId = dJointCreateHinge (wId, 0);
  513. joint = (dxJointHinge*) jId;
  514. dJointAttach (jId, NULL, bId2);
  515. dJointSetHingeAnchor (jId, 0, 0, 0);
  516. axis[0] = 1;
  517. axis[1] = 0;
  518. axis[2] = 0;
  519. }
  520. ~dxJointHinge_Fixture_B2_At_Zero_Axis_Along_X()
  521. {
  522. dWorldDestroy (wId);
  523. }
  524. dWorldID wId;
  525. dBodyID bId2;
  526. dJointID jId;
  527. dxJointHinge* joint;
  528. dVector3 axis;
  529. };
  530. // Rotate B2 by 90deg around X then back to original position
  531. //
  532. // ^
  533. // | => <---
  534. // |
  535. // B2 B2
  536. //
  537. // Start with a Delta of 90deg
  538. // ^
  539. // <--- => |
  540. // |
  541. // B2 B2
  542. TEST_FIXTURE (dxJointHinge_Fixture_B2_At_Zero_Axis_Along_X,
  543. test_dJointSetHingeAxisOffset_1Body_B2_90Deg) {
  544. dMatrix3 R;
  545. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  546. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  547. dRFromAxisAndAngle (R, 1, 0, 0, M_PI/2.0);
  548. dBodySetRotation (bId2, R);
  549. CHECK_CLOSE (-M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  550. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], -M_PI/2.0);
  551. CHECK_CLOSE (-M_PI/2.0, dJointGetHingeAngle (jId), 1e-4);
  552. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  553. dBodySetRotation (bId2, R);
  554. CHECK_CLOSE (0, dJointGetHingeAngle (jId), 1e-4);
  555. }
  556. // Rotate B2 by -0.23rad around X then back to original position
  557. //
  558. // ^ ^
  559. // | => /
  560. // | /
  561. // B2 B2
  562. //
  563. // Start with a Delta of -0.23rad
  564. // ^ ^
  565. // / => |
  566. // / |
  567. // B2 B2
  568. TEST_FIXTURE (dxJointHinge_Fixture_B2_At_Zero_Axis_Along_X,
  569. test_dJointSetHingeAxisOffset_1Body_B2_Minus0_23rad) {
  570. dMatrix3 R;
  571. dJointSetHingeAxis (jId, axis[0], axis[1], axis[2]);
  572. CHECK_CLOSE (dJointGetHingeAngle (jId), 0.0, 1e-4);
  573. dRFromAxisAndAngle (R, 1, 0, 0, -REAL(0.23));
  574. dBodySetRotation (bId2, R);
  575. CHECK_CLOSE (REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  576. dJointSetHingeAxisOffset (jId, axis[0], axis[1], axis[2], REAL(0.23));
  577. CHECK_CLOSE (REAL(0.23), dJointGetHingeAngle (jId), 1e-4);
  578. dRFromAxisAndAngle (R, 1, 0, 0, 0);
  579. dBodySetRotation (bId2, R);
  580. CHECK_CLOSE (0, dJointGetHingeAngle (jId), 1e-4);
  581. }
  582. // Create 2 bodies attached by a Hinge joint
  583. // Axis is along the X axis (Default value
  584. // Anchor at (0, 0, 0) (Default value)
  585. //
  586. // ^Y
  587. // |
  588. // * Body2
  589. // |
  590. // |
  591. // Body1 |
  592. // * Z-------->
  593. struct dxJointHinge_Test_Initialization {
  594. dxJointHinge_Test_Initialization()
  595. {
  596. wId = dWorldCreate();
  597. // Remove gravity to have the only force be the force of the joint
  598. dWorldSetGravity(wId, 0,0,0);
  599. for (int j=0; j<2; ++j) {
  600. bId[j][0] = dBodyCreate (wId);
  601. dBodySetPosition (bId[j][0], -1, -2, -3);
  602. bId[j][1] = dBodyCreate (wId);
  603. dBodySetPosition (bId[j][1], 11, 22, 33);
  604. dMatrix3 R;
  605. dVector3 axis; // Random axis
  606. axis[0] = REAL(0.53);
  607. axis[1] = -REAL(0.71);
  608. axis[2] = REAL(0.43);
  609. dNormalize3(axis);
  610. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  611. REAL(0.47123)); // 27deg
  612. dBodySetRotation (bId[j][0], R);
  613. axis[0] = REAL(1.2);
  614. axis[1] = REAL(0.87);
  615. axis[2] = -REAL(0.33);
  616. dNormalize3(axis);
  617. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  618. REAL(0.47123)); // 27deg
  619. dBodySetRotation (bId[j][1], R);
  620. jId[j] = dJointCreateHinge (wId, 0);
  621. dJointAttach (jId[j], bId[j][0], bId[j][1]);
  622. // dJointSetHingeParam(jId[j], dParamLoStop, 1);
  623. // dJointSetHingeParam(jId[j], dParamHiStop, 2);
  624. // dJointSetHingeParam(jId[j], dParamFMax, 200);
  625. }
  626. }
  627. ~dxJointHinge_Test_Initialization()
  628. {
  629. dWorldDestroy (wId);
  630. }
  631. dWorldID wId;
  632. dBodyID bId[2][2];
  633. dJointID jId[2];
  634. };
  635. // Test if setting a Hinge with its default values
  636. // will behave the same as a default Hinge joint
  637. TEST_FIXTURE (dxJointHinge_Test_Initialization,
  638. test_Hinge_Initialization) {
  639. using namespace std;
  640. dVector3 axis;
  641. dJointGetHingeAxis(jId[1], axis);
  642. dJointSetHingeAxis(jId[1], axis[0], axis[1], axis[2]);
  643. dVector3 anchor;
  644. dJointGetHingeAnchor(jId[1], anchor);
  645. dJointSetHingeAnchor(jId[1], anchor[0], anchor[1], anchor[2]);
  646. for (int b=0; b<2; ++b) {
  647. // Compare body b of the first joint with its equivalent on the
  648. // second joint
  649. const dReal *qA = dBodyGetQuaternion(bId[0][b]);
  650. const dReal *qB = dBodyGetQuaternion(bId[1][b]);
  651. CHECK_CLOSE (qA[0], qB[0], 1e-6);
  652. CHECK_CLOSE (qA[1], qB[1], 1e-6);
  653. CHECK_CLOSE (qA[2], qB[2], 1e-6);
  654. CHECK_CLOSE (qA[3], qB[3], 1e-6);
  655. }
  656. dWorldStep (wId,0.5);
  657. dWorldStep (wId,0.5);
  658. dWorldStep (wId,0.5);
  659. dWorldStep (wId,0.5);
  660. for (int b=0; b<2; ++b) {
  661. // Compare body b of the first joint with its equivalent on the
  662. // second joint
  663. const dReal *qA = dBodyGetQuaternion(bId[0][b]);
  664. const dReal *qB = dBodyGetQuaternion(bId[1][b]);
  665. CHECK_CLOSE (qA[0], qB[0], 1e-6);
  666. CHECK_CLOSE (qA[1], qB[1], 1e-6);
  667. CHECK_CLOSE (qA[2], qB[2], 1e-6);
  668. CHECK_CLOSE (qA[3], qB[3], 1e-6);
  669. const dReal *posA = dBodyGetPosition(bId[0][b]);
  670. const dReal *posB = dBodyGetPosition(bId[1][b]);
  671. CHECK_CLOSE (posA[0], posB[0], 1e-6);
  672. CHECK_CLOSE (posA[1], posB[1], 1e-6);
  673. CHECK_CLOSE (posA[2], posB[2], 1e-6);
  674. CHECK_CLOSE (posA[3], posB[3], 1e-6);
  675. }
  676. }
  677. TEST_FIXTURE(dxJointHinge_Fixture_B1_and_B2_At_Zero_Axis_Along_X,
  678. test_Hinge_dParamVel)
  679. {
  680. const dReal targetvel = 100;
  681. const dReal tolerance = targetvel *
  682. #ifdef dSINGLE
  683. 1e-2
  684. #else
  685. 1e-6
  686. #endif
  687. ;
  688. dJointSetHingeParam(jId, dParamFMax, dInfinity);
  689. dJointSetHingeParam(jId, dParamVel, targetvel);
  690. dWorldStep(wId, 0.001);
  691. const dReal *v1 = dBodyGetAngularVel(bId1);
  692. const dReal *v2 = dBodyGetAngularVel(bId2);
  693. dVector3 rvel = { v1[0]-v2[0], v1[1]-v2[1], v1[2]-v2[2] };
  694. CHECK_CLOSE(rvel[0], targetvel, tolerance);
  695. CHECK_CLOSE(rvel[1], 0, tolerance);
  696. CHECK_CLOSE(rvel[2], 0, tolerance);
  697. }
  698. } // End of SUITE TestdxJointHinge