hinge.cpp 24 KB

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