piston.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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/piston.cpp
  27. //
  28. //
  29. ////////////////////////////////////////////////////////////////////////////////
  30. #include <UnitTest++.h>
  31. #include <ode/ode.h>
  32. #include "../../ode/src/joints/piston.h"
  33. SUITE (TestdxJointPiston)
  34. {
  35. // The 2 bodies are positionned at (0, 0, 0), with no rotation
  36. // The joint is a Piston Joint
  37. // Axis is along the X axis
  38. // Anchor at (0, 0, 0)
  39. struct Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X
  40. {
  41. Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X()
  42. {
  43. wId = dWorldCreate();
  44. bId1 = dBodyCreate (wId);
  45. dBodySetPosition (bId1, 0, 0, 0);
  46. bId2 = dBodyCreate (wId);
  47. dBodySetPosition (bId2, 0, 0, 0);
  48. jId = dJointCreatePiston (wId, 0);
  49. joint = (dxJointPiston*) jId;
  50. dJointAttach (jId, bId1, bId2);
  51. dJointSetPistonAxis (jId, axis[0], axis[1], axis[2]);
  52. }
  53. ~Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X()
  54. {
  55. dWorldDestroy (wId);
  56. }
  57. dWorldID wId;
  58. dBodyID bId1;
  59. dBodyID bId2;
  60. dJointID jId;
  61. dxJointPiston* joint;
  62. static const dVector3 axis;
  63. static const dReal offset;
  64. };
  65. const dVector3 Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X::axis =
  66. {
  67. 1, 0, 0
  68. };
  69. const dReal Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X::offset = REAL (3.1);
  70. // Move 1st body offset unit in the X direction
  71. //
  72. // X-------> X---------> Axis -->
  73. // B1 => B1
  74. // B2 B2
  75. //
  76. // Start with a Offset of offset unit
  77. //
  78. // X-------> X---------> Axis -->
  79. // B1 => B1
  80. // B2 B2
  81. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X,
  82. test_dJointSetPistonAxisOffset_B1_3Unit)
  83. {
  84. dJointSetPistonAnchor (jId, 0, 0, 0);
  85. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  86. dBodySetPosition (bId1, offset, 0, 0);
  87. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  88. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  89. offset*axis[0],offset*axis[1],offset*axis[2]);
  90. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  91. dBodySetPosition (bId1, 0, 0, 0);
  92. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  93. // Only here to test a deprecated warning
  94. #if 0 // the deprecated warning is not a functional part of the API, no need to test it.
  95. dJointSetPistonAxisDelta (jId, 1, 0, 0, 0, 0, 0);
  96. #endif
  97. }
  98. // Move 1st body offset unit in the opposite X direction
  99. //
  100. // X-------> X---------> Axis -->
  101. // B1 => B1
  102. // B2 B2
  103. //
  104. // Start with a Offset of -offset unit
  105. //
  106. // X-------> X---------> Axis -->
  107. // B1 => B1
  108. // B2 B2
  109. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X,
  110. test_dJointSetPistonAxisOffset_B1_Minus_3Unit)
  111. {
  112. dJointSetPistonAnchor (jId, 0, 0, 0);
  113. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  114. dBodySetPosition (bId1, -offset, 0, 0);
  115. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  116. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  117. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  118. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  119. dBodySetPosition (bId1, 0, 0, 0);
  120. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  121. }
  122. // Move 2nd 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_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X,
  134. test_dJointSetPistonAxisOffset_B2_3Unit)
  135. {
  136. dJointSetPistonAnchor (jId, 0, 0, 0);
  137. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  138. dBodySetPosition (bId2, offset, 0, 0);
  139. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  140. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  141. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  142. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  143. dBodySetPosition (bId2, 0, 0, 0);
  144. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  145. }
  146. // Move 2nd 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_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X,
  158. test_dJointSetPistonAxisOffset_B2_Minus_3Unit)
  159. {
  160. dJointSetPistonAnchor (jId, 0, 0, 0);
  161. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  162. dBodySetPosition (bId2, -offset, 0, 0);
  163. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  164. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  165. offset*axis[0],offset*axis[1],offset*axis[2]);
  166. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  167. dBodySetPosition (bId2, 0, 0, 0);
  168. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  169. }
  170. // The 2 bodies are positionned at (0, 0, 0), with no rotation
  171. // The joint is a Piston Joint
  172. // Axis is the opposite of the X axis
  173. // Anchor at (0, 0, 0)
  174. struct Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X
  175. {
  176. Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X()
  177. {
  178. wId = dWorldCreate();
  179. bId1 = dBodyCreate (wId);
  180. dBodySetPosition (bId1, 0, 0, 0);
  181. bId2 = dBodyCreate (wId);
  182. dBodySetPosition (bId2, 0, 0, 0);
  183. jId = dJointCreatePiston (wId, 0);
  184. joint = (dxJointPiston*) jId;
  185. dJointAttach (jId, bId1, bId2);
  186. dJointSetPistonAxis (jId, axis[0], axis[1], axis[2]);
  187. }
  188. ~Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X()
  189. {
  190. dWorldDestroy (wId);
  191. }
  192. dWorldID wId;
  193. dBodyID bId1;
  194. dBodyID bId2;
  195. dJointID jId;
  196. dxJointPiston* joint;
  197. static const dVector3 axis;
  198. static const dReal offset;
  199. };
  200. const dVector3 Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X::axis =
  201. {
  202. -1, 0, 0
  203. };
  204. const dReal Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X::offset = REAL (3.1);
  205. // Move 1st body offset unit in the X direction
  206. //
  207. // X-------> X---------> <-- Axis
  208. // B1 => B1
  209. // B2 B2
  210. //
  211. // Start with a Offset of offset unit
  212. //
  213. // X-------> X---------> <-- Axis
  214. // B1 => B1
  215. // B2 B2
  216. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  217. test_dJointSetPistonAxisOffset_B1_3Unit)
  218. {
  219. dJointSetPistonAnchor (jId, 0, 0, 0);
  220. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  221. dBodySetPosition (bId1, offset, 0, 0);
  222. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  223. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  224. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  225. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  226. dBodySetPosition (bId1, 0, 0, 0);
  227. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  228. }
  229. // Move 1st body offset unit in the opposite X direction
  230. //
  231. // X-------> X---------> <-- Axis
  232. // B1 => B1
  233. // B2 B2
  234. //
  235. // Start with a Offset of offset unit
  236. //
  237. // X-------> X---------> <-- Axis
  238. // B1 => B1
  239. // B2 B2
  240. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  241. test_dJointSetPistonAxisOffset_B1_Minus_3Unit)
  242. {
  243. dJointSetPistonAnchor (jId, 0, 0, 0);
  244. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  245. dBodySetPosition (bId1, -offset, 0, 0);
  246. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  247. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  248. offset*axis[0],offset*axis[1],offset*axis[2]);
  249. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  250. dBodySetPosition (bId1, 0, 0, 0);
  251. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  252. }
  253. // Move 2nd body offset unit in the X direction
  254. //
  255. // X-------> X---------> <-- Axis
  256. // B1 => B1
  257. // B2 B2
  258. //
  259. // Start with a Offset of offset unit
  260. //
  261. // X-------> X---------> <-- Axis
  262. // B1 => B1
  263. // B2 B2
  264. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  265. test_dJointSetPistonAxisOffset_B2_3Unit)
  266. {
  267. dJointSetPistonAnchor (jId, 0, 0, 0);
  268. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  269. dBodySetPosition (bId2, offset, 0, 0);
  270. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  271. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  272. offset*axis[0],offset*axis[1],offset*axis[2]);
  273. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  274. dBodySetPosition (bId2, 0, 0, 0);
  275. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  276. }
  277. // Move 2nd body offset unit in the opposite X direction
  278. //
  279. // X-------> X---------> <-- Axis
  280. // B1 => B1
  281. // B2 B2
  282. //
  283. // Start with a Offset of -offset unit
  284. //
  285. // X-------> X---------> <-- Axis
  286. // B1 => B1
  287. // B2 B2
  288. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  289. test_dJointSetPistonAxisOffset_B2_Minus_3Unit)
  290. {
  291. dJointSetPistonAnchor (jId, 0, 0, 0);
  292. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  293. dBodySetPosition (bId2, -offset, 0, 0);
  294. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  295. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  296. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  297. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  298. dBodySetPosition (bId2, 0, 0, 0);
  299. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  300. }
  301. // Only body 1
  302. // The body are positionned at (0, 0, 0), with no rotation
  303. // The joint is a Piston Joint
  304. // Axis is along the X axis
  305. // Anchor at (0, 0, 0)
  306. struct Fixture_dxJointPiston_B1_At_Zero_Axis_Along_X
  307. {
  308. Fixture_dxJointPiston_B1_At_Zero_Axis_Along_X()
  309. {
  310. wId = dWorldCreate();
  311. bId1 = dBodyCreate (wId);
  312. dBodySetPosition (bId1, 0, 0, 0);
  313. jId = dJointCreatePiston (wId, 0);
  314. joint = (dxJointPiston*) jId;
  315. dJointAttach (jId, bId1, NULL);
  316. dJointSetPistonAxis (jId, axis[0], axis[1], axis[2]);
  317. }
  318. ~Fixture_dxJointPiston_B1_At_Zero_Axis_Along_X()
  319. {
  320. dWorldDestroy (wId);
  321. }
  322. dWorldID wId;
  323. dBodyID bId1;
  324. dJointID jId;
  325. dxJointPiston* joint;
  326. static const dVector3 axis;
  327. static const dReal offset;
  328. };
  329. const dVector3 Fixture_dxJointPiston_B1_At_Zero_Axis_Along_X::axis =
  330. {
  331. 1, 0, 0
  332. };
  333. const dReal Fixture_dxJointPiston_B1_At_Zero_Axis_Along_X::offset = REAL (3.1);
  334. // Move 1st body offset unit in the X direction
  335. //
  336. // X-------> X---------> Axis -->
  337. // B1 => B1
  338. //
  339. // Start with a Offset of offset unit
  340. //
  341. // X-------> X---------> Axis -->
  342. // B1 => B1
  343. TEST_FIXTURE (Fixture_dxJointPiston_B1_At_Zero_Axis_Along_X,
  344. test_dJointSetPistonAxisOffset_B1_OffsetUnit)
  345. {
  346. dJointSetPistonAnchor (jId, 0, 0, 0);
  347. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  348. dBodySetPosition (bId1, offset, 0, 0);
  349. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  350. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  351. offset*axis[0],offset*axis[1],offset*axis[2]);
  352. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  353. dBodySetPosition (bId1, 0, 0, 0);
  354. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  355. }
  356. // Move 1st body offset unit in the opposite 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_dxJointPiston_B1_At_Zero_Axis_Along_X,
  366. test_dJointSetPistonAxisOffset_B1_Minus_OffsetUnit)
  367. {
  368. dJointSetPistonAnchor (jId, 0, 0, 0);
  369. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  370. dBodySetPosition (bId1, -offset, 0, 0);
  371. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  372. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  373. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  374. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  375. dBodySetPosition (bId1, 0, 0, 0);
  376. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  377. }
  378. // Only body 1
  379. // The body are positionned at (0, 0, 0), with no rotation
  380. // The joint is a Piston Joint
  381. // Axis is in the oppsite X axis
  382. // Anchor at (0, 0, 0)
  383. struct Fixture_dxJointPiston_B1_At_Zero_Axis_Inverse_of_X
  384. {
  385. Fixture_dxJointPiston_B1_At_Zero_Axis_Inverse_of_X()
  386. {
  387. wId = dWorldCreate();
  388. bId1 = dBodyCreate (wId);
  389. dBodySetPosition (bId1, 0, 0, 0);
  390. jId = dJointCreatePiston (wId, 0);
  391. joint = (dxJointPiston*) jId;
  392. dJointAttach (jId, bId1, NULL);
  393. dJointSetPistonAxis (jId, axis[0], axis[1], axis[2]);
  394. }
  395. ~Fixture_dxJointPiston_B1_At_Zero_Axis_Inverse_of_X()
  396. {
  397. dWorldDestroy (wId);
  398. }
  399. dWorldID wId;
  400. dBodyID bId1;
  401. dJointID jId;
  402. dxJointPiston* joint;
  403. static const dVector3 axis;
  404. static const dReal offset;
  405. };
  406. const dVector3 Fixture_dxJointPiston_B1_At_Zero_Axis_Inverse_of_X::axis =
  407. {
  408. -1, 0, 0
  409. };
  410. const dReal Fixture_dxJointPiston_B1_At_Zero_Axis_Inverse_of_X::offset = REAL (3.1);
  411. // Move 1st body offset unit in the X direction
  412. //
  413. // X-------> X---------> <--- Axis
  414. // B1 => B1
  415. //
  416. // Start with a Offset of offset unit
  417. //
  418. // X-------> X---------> <--- Axis
  419. // B1 => B1
  420. TEST_FIXTURE (Fixture_dxJointPiston_B1_At_Zero_Axis_Inverse_of_X,
  421. test_dJointSetPistonAxisOffset_B1_OffsetUnit)
  422. {
  423. dJointSetPistonAnchor (jId, 0, 0, 0);
  424. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  425. dBodySetPosition (bId1, offset, 0, 0);
  426. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  427. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  428. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  429. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  430. dBodySetPosition (bId1, 0, 0, 0);
  431. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  432. }
  433. // Move 1st body offset unit in the opposite X direction
  434. //
  435. // X-------> X---------> <--- Axis
  436. // B1 => B1
  437. //
  438. // Start with a Offset of -offset unit
  439. //
  440. // X-------> X---------> <--- Axis
  441. // B1 => B1
  442. TEST_FIXTURE (Fixture_dxJointPiston_B1_At_Zero_Axis_Inverse_of_X,
  443. test_dJointSetPistonAxisOffset_B1_Minus_OffsetUnit)
  444. {
  445. dJointSetPistonAnchor (jId, 0, 0, 0);
  446. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  447. dBodySetPosition (bId1, -offset, 0, 0);
  448. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  449. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  450. offset*axis[0],offset*axis[1],offset*axis[2]);
  451. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  452. dBodySetPosition (bId1, 0, 0, 0);
  453. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  454. }
  455. // Only body 2
  456. // The body are positionned at (0, 0, 0), with no rotation
  457. // The joint is a Piston Joint
  458. // Axis is along the X axis
  459. // Anchor at (0, 0, 0)
  460. struct Fixture_dxJointPiston_B2_At_Zero_Axis_Along_X
  461. {
  462. Fixture_dxJointPiston_B2_At_Zero_Axis_Along_X()
  463. {
  464. wId = dWorldCreate();
  465. bId2 = dBodyCreate (wId);
  466. dBodySetPosition (bId2, 0, 0, 0);
  467. jId = dJointCreatePiston (wId, 0);
  468. joint = (dxJointPiston*) jId;
  469. dJointAttach (jId, NULL, bId2);
  470. dJointSetPistonAxis (jId, axis[0], axis[1], axis[2]);
  471. }
  472. ~Fixture_dxJointPiston_B2_At_Zero_Axis_Along_X()
  473. {
  474. dWorldDestroy (wId);
  475. }
  476. dWorldID wId;
  477. dBodyID bId2;
  478. dJointID jId;
  479. dxJointPiston* joint;
  480. static const dVector3 axis;
  481. static const dReal offset;
  482. };
  483. const dVector3 Fixture_dxJointPiston_B2_At_Zero_Axis_Along_X::axis =
  484. {
  485. 1, 0, 0
  486. };
  487. const dReal Fixture_dxJointPiston_B2_At_Zero_Axis_Along_X::offset = REAL (3.1);
  488. // Move 2nd body offset unit in the X direction
  489. //
  490. // X-------> X---------> Axis -->
  491. // B2 => B2
  492. //
  493. // Start with a Offset of offset unit
  494. //
  495. // X-------> X---------> Axis -->
  496. // B2 => B2
  497. TEST_FIXTURE (Fixture_dxJointPiston_B2_At_Zero_Axis_Along_X,
  498. test_dJointSetPistonAxisOffset_B2_OffsetUnit)
  499. {
  500. dJointSetPistonAnchor (jId, 0, 0, 0);
  501. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  502. dBodySetPosition (bId2, offset, 0, 0);
  503. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  504. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  505. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  506. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  507. dBodySetPosition (bId2, 0, 0, 0);
  508. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  509. }
  510. // Move 2nd body offset unit in the opposite X direction
  511. //
  512. // X-------> X---------> Axis -->
  513. // B2 => B2
  514. //
  515. // Start with a Offset of -offset unit
  516. //
  517. // X-------> X---------> Axis -->
  518. // B2 => B2
  519. TEST_FIXTURE (Fixture_dxJointPiston_B2_At_Zero_Axis_Along_X,
  520. test_dJointSetPistonAxisOffset_B2_Minus_OffsetUnit)
  521. {
  522. dJointSetPistonAnchor (jId, 0, 0, 0);
  523. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  524. dBodySetPosition (bId2, -offset, 0, 0);
  525. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  526. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  527. offset*axis[0],offset*axis[1],offset*axis[2]);
  528. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  529. dBodySetPosition (bId2, 0, 0, 0);
  530. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  531. }
  532. // Only body 2
  533. // The body are positionned at (0, 0, 0), with no rotation
  534. // The joint is a Piston Joint
  535. // Axis is in the opposite X axis
  536. // Anchor at (0, 0, 0)
  537. struct Fixture_dxJointPiston_B2_At_Zero_Axis_Inverse_of_X
  538. {
  539. Fixture_dxJointPiston_B2_At_Zero_Axis_Inverse_of_X()
  540. {
  541. wId = dWorldCreate();
  542. bId2 = dBodyCreate (wId);
  543. dBodySetPosition (bId2, 0, 0, 0);
  544. jId = dJointCreatePiston (wId, 0);
  545. joint = (dxJointPiston*) jId;
  546. dJointAttach (jId, NULL, bId2);
  547. dJointSetPistonAxis (jId, axis[0], axis[1], axis[2]);
  548. }
  549. ~Fixture_dxJointPiston_B2_At_Zero_Axis_Inverse_of_X()
  550. {
  551. dWorldDestroy (wId);
  552. }
  553. dWorldID wId;
  554. dBodyID bId2;
  555. dJointID jId;
  556. dxJointPiston* joint;
  557. static const dVector3 axis;
  558. static const dReal offset;
  559. };
  560. const dVector3 Fixture_dxJointPiston_B2_At_Zero_Axis_Inverse_of_X::axis =
  561. {
  562. -1, 0, 0
  563. };
  564. const dReal Fixture_dxJointPiston_B2_At_Zero_Axis_Inverse_of_X::offset = REAL (3.1);
  565. // Move 2nd body offset unit in the X direction
  566. //
  567. // X-------> X---------> <--- Axis
  568. // B2 => B2
  569. //
  570. // Start with a Offset of offset unit
  571. //
  572. // X-------> X---------> <--- Axis
  573. // B2 => B2
  574. TEST_FIXTURE (Fixture_dxJointPiston_B2_At_Zero_Axis_Inverse_of_X,
  575. test_dJointSetPistonAxisOffset_B2_OffsetUnit)
  576. {
  577. dJointSetPistonAnchor (jId, 0, 0, 0);
  578. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  579. dBodySetPosition (bId2, offset, 0, 0);
  580. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  581. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  582. offset*axis[0],offset*axis[1],offset*axis[2]);
  583. CHECK_CLOSE (offset, dJointGetPistonPosition (jId), 1e-4);
  584. dBodySetPosition (bId2, 0, 0, 0);
  585. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  586. #if 0 // another deprecated warning test?
  587. dJointSetPistonAxisDelta (jId, 1, 0, 0, 0, 0, 0);
  588. #endif
  589. }
  590. // Move 1st body offset unit in the opposite X direction
  591. //
  592. // X-------> X---------> <--- Axis
  593. // B2 => B2
  594. //
  595. // Start with a Offset of -offset unit
  596. //
  597. // X-------> X---------> <--- Axis
  598. // B2 => B2
  599. TEST_FIXTURE (Fixture_dxJointPiston_B2_At_Zero_Axis_Inverse_of_X,
  600. test_dJointSetPistonAxisOffset_B2_Minus_OffsetUnit)
  601. {
  602. dJointSetPistonAnchor (jId, 0, 0, 0);
  603. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  604. dBodySetPosition (bId2, -offset, 0, 0);
  605. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  606. dJointSetPistonAnchorOffset (jId, 0, 0, 0,
  607. -offset*axis[0],-offset*axis[1],-offset*axis[2]);
  608. CHECK_CLOSE (-offset, dJointGetPistonPosition (jId), 1e-4);
  609. dBodySetPosition (bId2, 0, 0, 0);
  610. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  611. }
  612. // ==========================================================================
  613. // Test Position Rate
  614. // ==========================================================================
  615. // Apply force on 1st body in the X direction also the Axis direction
  616. //
  617. // X-------> X---------> Axis -->
  618. // B1 F-> => B1
  619. // B2 B2
  620. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X,
  621. test_dJointSetPistonPositionRate_Force_Along_Axis_on_B1)
  622. {
  623. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  624. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  625. dBodyAddForce (bId1, 1.0, 0, 0);
  626. dWorldQuickStep (wId, 1.0);
  627. CHECK_CLOSE (1, dJointGetPistonPositionRate (jId), 1e-4);
  628. }
  629. // Apply force on 1st body in the inverse X direction
  630. //
  631. // X-------> X---------> Axis -->
  632. // B1 <-F => B1
  633. // B2 B2
  634. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X,
  635. test_dJointSetPistonPositionRate_Force_Inverse_of_Axis_on_B1)
  636. {
  637. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  638. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  639. dBodyAddForce (bId1, -1.0, 0, 0);
  640. dWorldQuickStep (wId, 1.0);
  641. CHECK_CLOSE (-1, dJointGetPistonPositionRate (jId), 1e-4);
  642. }
  643. // Apply force on 1st body in the X direction also the Axis direction
  644. //
  645. // X-------> X---------> <-- Axis
  646. // B1 F-> => B1
  647. // B2 B2
  648. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  649. test_dJointSetPistonPositionRate_Force_Inverse_Axis_on_B1)
  650. {
  651. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  652. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  653. dBodyAddForce (bId1, 1.0, 0, 0);
  654. dWorldQuickStep (wId, 1.0);
  655. CHECK_CLOSE (-1, dJointGetPistonPositionRate (jId), 1e-4);
  656. }
  657. // Apply force on 1st body in the inverse X direction
  658. //
  659. // X-------> X---------> <-- Axis
  660. // B1 <-F => B1
  661. // B2 B2
  662. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  663. test_dJointSetPistonPositionRate_Force_Along_of_Axis_on_B1)
  664. {
  665. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  666. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  667. dBodyAddForce (bId1, -1.0, 0, 0);
  668. dWorldQuickStep (wId, 1.0);
  669. CHECK_CLOSE (1, dJointGetPistonPositionRate (jId), 1e-4);
  670. }
  671. // Apply force on 1st body in the X direction also the Axis direction
  672. //
  673. // X-------> X---------> Axis -->
  674. // B1 => B1
  675. // B2 F-> B2
  676. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X,
  677. test_dJointSetPistonPositionRate_Force_Along_Axis_on_B2)
  678. {
  679. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  680. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  681. dBodyAddForce (bId2, 1.0, 0, 0);
  682. dWorldQuickStep (wId, 1.0);
  683. CHECK_CLOSE (-1, dJointGetPistonPositionRate (jId), 1e-4);
  684. }
  685. // Apply force on 1st body in the inverse X direction
  686. //
  687. // X-------> X---------> Axis -->
  688. // B1 => B1
  689. // B2 <-F B2
  690. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Along_X,
  691. test_dJointSetPistonPositionRate_Force_Inverse_of_Axis_on_B2)
  692. {
  693. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  694. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  695. dBodyAddForce (bId2, -1.0, 0, 0);
  696. dWorldQuickStep (wId, 1.0);
  697. CHECK_CLOSE (1, dJointGetPistonPositionRate (jId), 1e-4);
  698. }
  699. // Apply force on 1st body in the X direction also the Axis direction
  700. //
  701. // X-------> X---------> <-- Axis
  702. // B1 => B1
  703. // B2 F-> B2
  704. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  705. test_dJointSetPistonPositionRate_Force_Inverse_Axis_on_B2)
  706. {
  707. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  708. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  709. dBodyAddForce (bId2, 1.0, 0, 0);
  710. dWorldQuickStep (wId, 1.0);
  711. CHECK_CLOSE (1, dJointGetPistonPositionRate (jId), 1e-4);
  712. }
  713. // Apply force on 1st body in the inverse X direction
  714. //
  715. // X-------> X---------> <-- Axis
  716. // B1 => B1
  717. // B2 <-F B2
  718. TEST_FIXTURE (Fixture_dxJointPiston_B1_and_B2_At_Zero_Axis_Inverse_of_X,
  719. test_dJointSetPistonPositionRate_Force_Along_of_Axis_on_B2)
  720. {
  721. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  722. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  723. dBodyAddForce (bId2, -1.0, 0, 0);
  724. dWorldQuickStep (wId, 1.0);
  725. CHECK_CLOSE (-1, dJointGetPistonPositionRate (jId), 1e-4);
  726. }
  727. // Apply force on 1st body in the X direction also the Axis direction
  728. //
  729. // X-------> X---------> Axis -->
  730. // B1 F-> => B1
  731. TEST_FIXTURE (Fixture_dxJointPiston_B1_At_Zero_Axis_Along_X,
  732. test_dJointSetPistonPositionRate_Force_Along_Axis_on_B1)
  733. {
  734. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  735. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  736. dBodyAddForce (bId1, 1.0, 0, 0);
  737. dWorldQuickStep (wId, 1.0);
  738. CHECK_CLOSE (1, dJointGetPistonPositionRate (jId), 1e-4);
  739. }
  740. // Apply force on 1st body in the inverse X direction
  741. //
  742. // X-------> X---------> Axis -->
  743. // B1 <-F => B1
  744. TEST_FIXTURE (Fixture_dxJointPiston_B1_At_Zero_Axis_Along_X,
  745. test_dJointSetPistonPositionRate_Force_Inverse_of_Axis_on_B1)
  746. {
  747. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  748. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  749. dBodyAddForce (bId1, -1.0, 0, 0);
  750. dWorldQuickStep (wId, 1.0);
  751. CHECK_CLOSE (-1, dJointGetPistonPositionRate (jId), 1e-4);
  752. }
  753. // Apply force on 1st body in the X direction also the Axis direction
  754. //
  755. // X-------> X---------> <-- Axis
  756. // B1 F-> => B1
  757. TEST_FIXTURE (Fixture_dxJointPiston_B1_At_Zero_Axis_Inverse_of_X,
  758. test_dJointSetPistonPositionRate_Force_Inverse_Axis_on_B1)
  759. {
  760. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  761. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  762. dBodyAddForce (bId1, 1.0, 0, 0);
  763. dWorldQuickStep (wId, 1.0);
  764. CHECK_CLOSE (-1, dJointGetPistonPositionRate (jId), 1e-4);
  765. }
  766. // Apply force on 1st body in the inverse X direction
  767. //
  768. // X-------> X---------> <-- Axis
  769. // B1 <-F => B1
  770. TEST_FIXTURE (Fixture_dxJointPiston_B1_At_Zero_Axis_Inverse_of_X,
  771. test_dJointSetPistonPositionRate_Force_Along_of_Axis_on_B1)
  772. {
  773. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  774. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  775. dBodyAddForce (bId1, -1.0, 0, 0);
  776. dWorldQuickStep (wId, 1.0);
  777. CHECK_CLOSE (1, dJointGetPistonPositionRate (jId), 1e-4);
  778. }
  779. // Apply force on body 2 in the X direction also the Axis direction
  780. //
  781. // X-------> X---------> Axis -->
  782. // B2 F-> B2
  783. TEST_FIXTURE (Fixture_dxJointPiston_B2_At_Zero_Axis_Along_X,
  784. test_dJointSetPistonPositionRate_Force_Along_Axis_on_B2)
  785. {
  786. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  787. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  788. dBodyAddForce (bId2, 1.0, 0, 0);
  789. dWorldQuickStep (wId, 1.0);
  790. CHECK_CLOSE (-1, dJointGetPistonPositionRate (jId), 1e-4);
  791. }
  792. // Apply force on body 2 in the inverse X direction
  793. //
  794. // X-------> X---------> Axis -->
  795. // B2 <-F B2
  796. TEST_FIXTURE (Fixture_dxJointPiston_B2_At_Zero_Axis_Along_X,
  797. test_dJointSetPistonPositionRate_Force_Inverse_of_Axis_on_B2)
  798. {
  799. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  800. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  801. dBodyAddForce (bId2, -1.0, 0, 0);
  802. dWorldQuickStep (wId, 1.0);
  803. CHECK_CLOSE (1, dJointGetPistonPositionRate (jId), 1e-4);
  804. }
  805. // Apply force on body 2 in the X direction also the Axis direction
  806. //
  807. // X-------> X---------> <-- Axis
  808. // B2 F-> B2
  809. TEST_FIXTURE (Fixture_dxJointPiston_B2_At_Zero_Axis_Inverse_of_X,
  810. test_dJointSetPistonPositionRate_Force_Inverse_Axis_on_B2)
  811. {
  812. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  813. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  814. dBodyAddForce (bId2, 1.0, 0, 0);
  815. dWorldQuickStep (wId, 1.0);
  816. CHECK_CLOSE (1, dJointGetPistonPositionRate (jId), 1e-4);
  817. }
  818. // Apply force on body 2 in the inverse X direction
  819. //
  820. // X-------> X---------> <-- Axis
  821. // B2 <-F B2
  822. TEST_FIXTURE (Fixture_dxJointPiston_B2_At_Zero_Axis_Inverse_of_X,
  823. test_dJointSetPistonPositionRate_Force_Along_of_Axis_on_B2)
  824. {
  825. CHECK_CLOSE (0.0, dJointGetPistonPosition (jId), 1e-4);
  826. CHECK_CLOSE (0.0, dJointGetPistonPositionRate (jId), 1e-4);
  827. dBodyAddForce (bId2, -1.0, 0, 0);
  828. dWorldQuickStep (wId, 1.0);
  829. CHECK_CLOSE (-1, dJointGetPistonPositionRate (jId), 1e-4);
  830. }
  831. // Create 2 bodies attached by a Piston joint
  832. // Axis is along the X axis (Default value
  833. // Anchor at (0, 0, 0) (Default value)
  834. //
  835. // ^Y
  836. // |
  837. // * Body2
  838. // |
  839. // |
  840. // Body1 |
  841. // * Z-------->
  842. struct dxJointPiston_Test_Initialization
  843. {
  844. dxJointPiston_Test_Initialization()
  845. {
  846. wId = dWorldCreate();
  847. // Remove gravity to have the only force be the force of the joint
  848. dWorldSetGravity(wId, 0,0,0);
  849. for (int j=0; j<2; ++j)
  850. {
  851. bId[j][0] = dBodyCreate (wId);
  852. dBodySetPosition (bId[j][0], -1, -2, -3);
  853. bId[j][1] = dBodyCreate (wId);
  854. dBodySetPosition (bId[j][1], 11, 22, 33);
  855. dMatrix3 R;
  856. dVector3 axis; // Random axis
  857. axis[0] = REAL(0.53);
  858. axis[1] = -REAL(0.71);
  859. axis[2] = REAL(0.43);
  860. dNormalize3(axis);
  861. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  862. REAL(0.47123)); // 27deg
  863. dBodySetRotation (bId[j][0], R);
  864. axis[0] = REAL(1.2);
  865. axis[1] = REAL(0.87);
  866. axis[2] = -REAL(0.33);
  867. dNormalize3(axis);
  868. dRFromAxisAndAngle (R, axis[0], axis[1], axis[2],
  869. REAL(0.47123)); // 27deg
  870. dBodySetRotation (bId[j][1], R);
  871. jId[j] = dJointCreatePiston (wId, 0);
  872. dJointAttach (jId[j], bId[j][0], bId[j][1]);
  873. }
  874. }
  875. ~dxJointPiston_Test_Initialization()
  876. {
  877. dWorldDestroy (wId);
  878. }
  879. dWorldID wId;
  880. dBodyID bId[2][2];
  881. dJointID jId[2];
  882. };
  883. // Test if setting a Piston with its default values
  884. // will behave the same as a default Piston joint
  885. TEST_FIXTURE (dxJointPiston_Test_Initialization,
  886. test_Piston_Initialization)
  887. {
  888. using namespace std;
  889. dVector3 axis;
  890. dJointGetPistonAxis(jId[1], axis);
  891. dJointSetPistonAxis(jId[1], axis[0], axis[1], axis[2]);
  892. dVector3 anchor;
  893. dJointGetPistonAnchor(jId[1], anchor);
  894. dJointSetPistonAnchor(jId[1], anchor[0], anchor[1], anchor[2]);
  895. for (int b=0; b<2; ++b)
  896. {
  897. // Compare body b of the first joint with its equivalent on the
  898. // second joint
  899. const dReal *qA = dBodyGetQuaternion(bId[0][b]);
  900. const dReal *qB = dBodyGetQuaternion(bId[1][b]);
  901. CHECK_CLOSE (qA[0], qB[0], 1e-6);
  902. CHECK_CLOSE (qA[1], qB[1], 1e-6);
  903. CHECK_CLOSE (qA[2], qB[2], 1e-6);
  904. CHECK_CLOSE (qA[3], qB[3], 1e-6);
  905. }
  906. dWorldStep (wId,0.5);
  907. dWorldStep (wId,0.5);
  908. dWorldStep (wId,0.5);
  909. dWorldStep (wId,0.5);
  910. for (int b=0; b<2; ++b)
  911. {
  912. // Compare body b of the first joint with its equivalent on the
  913. // second joint
  914. const dReal *qA = dBodyGetQuaternion(bId[0][b]);
  915. const dReal *qB = dBodyGetQuaternion(bId[1][b]);
  916. CHECK_CLOSE (qA[0], qB[0], 1e-6);
  917. CHECK_CLOSE (qA[1], qB[1], 1e-6);
  918. CHECK_CLOSE (qA[2], qB[2], 1e-6);
  919. CHECK_CLOSE (qA[3], qB[3], 1e-6);
  920. const dReal *posA = dBodyGetPosition(bId[0][b]);
  921. const dReal *posB = dBodyGetPosition(bId[1][b]);
  922. CHECK_CLOSE (posA[0], posB[0], 1e-6);
  923. CHECK_CLOSE (posA[1], posB[1], 1e-6);
  924. CHECK_CLOSE (posA[2], posB[2], 1e-6);
  925. CHECK_CLOSE (posA[3], posB[3], 1e-6);
  926. }
  927. }
  928. // Compare only one body to 2 bodies with one fixed.
  929. //
  930. // The body are positionned at (0, 0, 0), with no rotation
  931. // The joint is a Piston Joint
  932. // Axis is along the X axis
  933. // Anchor at (0, 0, 0)
  934. struct Fixture_dxJointPiston_Compare_Body_At_Zero_Axis_Along_X
  935. {
  936. Fixture_dxJointPiston_Compare_Body_At_Zero_Axis_Along_X()
  937. {
  938. wId = dWorldCreate();
  939. bId1_12 = dBodyCreate (wId);
  940. dBodySetPosition (bId1_12, 0, 0, 0);
  941. bId2_12 = dBodyCreate (wId);
  942. dBodySetPosition (bId2_12, 0, 0, 0);
  943. // The force will be added in the function since it is not
  944. // always on the same body
  945. jId_12 = dJointCreatePiston (wId, 0);
  946. dJointAttach(jId_12, bId1_12, bId2_12);
  947. fixed = dJointCreateFixed (wId, 0);
  948. bId = dBodyCreate (wId);
  949. dBodySetPosition (bId, 0, 0, 0);
  950. dBodyAddForce (bId, 4, 0, 0);
  951. jId = dJointCreatePiston (wId, 0);
  952. }
  953. ~Fixture_dxJointPiston_Compare_Body_At_Zero_Axis_Along_X()
  954. {
  955. dWorldDestroy (wId);
  956. }
  957. dWorldID wId;
  958. dBodyID bId1_12;
  959. dBodyID bId2_12;
  960. dJointID jId_12; // Joint with 2 bodies
  961. dJointID fixed;
  962. dBodyID bId;
  963. dJointID jId; // Joint with one body
  964. };
  965. // This test compare the result of a slider with 2 bodies where body body 2 is
  966. // fixed to the world to a slider with only one body at position 1.
  967. //
  968. // Test the limits [-1, 0.25] when only one body at is attached to the joint
  969. // using dJointAttache(jId, bId, 0);
  970. //
  971. TEST_FIXTURE(Fixture_dxJointPiston_Compare_Body_At_Zero_Axis_Along_X,
  972. test_Limit_minus1_025_One_Body_on_left)
  973. {
  974. dBodyAddForce (bId1_12, 4, 0, 0);
  975. dJointAttach(jId_12, bId1_12, bId2_12);
  976. dJointSetPistonParam(jId_12, dParamLoStop, -1);
  977. dJointSetPistonParam(jId_12, dParamHiStop, 0.25);
  978. dJointAttach(fixed, 0, bId2_12);
  979. dJointSetFixed(fixed);
  980. dJointAttach(jId, bId, 0);
  981. dJointSetPistonParam(jId, dParamLoStop, -1);
  982. dJointSetPistonParam(jId, dParamHiStop, 0.25);
  983. for (int i=0; i<50; ++i)
  984. dWorldStep(wId, 1.0);
  985. const dReal *pos1_12 = dBodyGetPosition(bId1_12);
  986. const dReal *pos = dBodyGetPosition(bId);
  987. CHECK_CLOSE (pos1_12[0], pos[0], 1e-2);
  988. CHECK_CLOSE (pos1_12[1], pos[1], 1e-2);
  989. CHECK_CLOSE (pos1_12[2], pos[2], 1e-2);
  990. const dReal *q1_12 = dBodyGetQuaternion(bId1_12);
  991. const dReal *q = dBodyGetQuaternion(bId);
  992. CHECK_CLOSE (q1_12[0], q[0], 1e-4);
  993. CHECK_CLOSE (q1_12[1], q[1], 1e-4);
  994. CHECK_CLOSE (q1_12[2], q[2], 1e-4);
  995. CHECK_CLOSE (q1_12[3], q[3], 1e-4);
  996. }
  997. // This test compare the result of a slider with 2 bodies where body body 1 is
  998. // fixed to the world to a slider with only one body at position 2.
  999. //
  1000. // Test the limits [-1, 0.25] when only one body at is attached to the joint
  1001. // using dJointAttache(jId, 0, bId);
  1002. //
  1003. TEST_FIXTURE(Fixture_dxJointPiston_Compare_Body_At_Zero_Axis_Along_X,
  1004. test_Limit_minus1_025_One_Body_on_right)
  1005. {
  1006. dBodyAddForce (bId2_12, 4, 0, 0);
  1007. dJointAttach(jId_12, bId1_12, bId2_12);
  1008. dJointSetPistonParam(jId_12, dParamLoStop, -1);
  1009. dJointSetPistonParam(jId_12, dParamHiStop, 0.25);
  1010. dJointAttach(fixed, bId1_12, 0);
  1011. dJointSetFixed(fixed);
  1012. dJointAttach(jId, 0, bId);
  1013. dJointSetPistonParam(jId, dParamLoStop, -1);
  1014. dJointSetPistonParam(jId, dParamHiStop, 0.25);
  1015. for (int i=0; i<50; ++i)
  1016. dWorldStep(wId, 1.0);
  1017. const dReal *pos2_12 = dBodyGetPosition(bId2_12);
  1018. const dReal *pos = dBodyGetPosition(bId);
  1019. CHECK_CLOSE (pos2_12[0], pos[0], 1e-2);
  1020. CHECK_CLOSE (pos2_12[1], pos[1], 1e-2);
  1021. CHECK_CLOSE (pos2_12[2], pos[2], 1e-2);
  1022. const dReal *q2_12 = dBodyGetQuaternion(bId2_12);
  1023. const dReal *q = dBodyGetQuaternion(bId);
  1024. CHECK_CLOSE (q2_12[0], q[0], 1e-4);
  1025. CHECK_CLOSE (q2_12[1], q[1], 1e-4);
  1026. CHECK_CLOSE (q2_12[2], q[2], 1e-4);
  1027. CHECK_CLOSE (q2_12[3], q[3], 1e-4);
  1028. }
  1029. // This test compare the result of a slider with 2 bodies where body body 2 is
  1030. // fixed to the world to a slider with only one body at position 1.
  1031. //
  1032. // Test the limits [0, 0] when only one body at is attached to the joint
  1033. // using dJointAttache(jId, bId, 0);
  1034. //
  1035. // The body should not move since their is no room between the two limits
  1036. //
  1037. TEST_FIXTURE(Fixture_dxJointPiston_Compare_Body_At_Zero_Axis_Along_X,
  1038. test_Limit_0_0_One_Body_on_left)
  1039. {
  1040. dBodyAddForce (bId1_12, 4, 0, 0);
  1041. dJointAttach(jId_12, bId1_12, bId2_12);
  1042. dJointSetPistonParam(jId_12, dParamLoStop, 0);
  1043. dJointSetPistonParam(jId_12, dParamHiStop, 0);
  1044. dJointAttach(fixed, 0, bId2_12);
  1045. dJointSetFixed(fixed);
  1046. dJointAttach(jId, bId, 0);
  1047. dJointSetPistonParam(jId, dParamLoStop, 0);
  1048. dJointSetPistonParam(jId, dParamHiStop, 0);
  1049. for (int i=0; i<500; ++i)
  1050. dWorldStep(wId, 1.0);
  1051. const dReal *pos1_12 = dBodyGetPosition(bId1_12);
  1052. const dReal *pos = dBodyGetPosition(bId);
  1053. CHECK_CLOSE (pos1_12[0], pos[0], 1e-4);
  1054. CHECK_CLOSE (pos1_12[1], pos[1], 1e-4);
  1055. CHECK_CLOSE (pos1_12[2], pos[2], 1e-4);
  1056. CHECK_CLOSE (0, pos[0], 1e-4);
  1057. CHECK_CLOSE (0, pos[1], 1e-4);
  1058. CHECK_CLOSE (0, pos[2], 1e-4);
  1059. const dReal *q1_12 = dBodyGetQuaternion(bId1_12);
  1060. const dReal *q = dBodyGetQuaternion(bId);
  1061. CHECK_CLOSE (q1_12[0], q[0], 1e-4);
  1062. CHECK_CLOSE (q1_12[1], q[1], 1e-4);
  1063. CHECK_CLOSE (q1_12[2], q[2], 1e-4);
  1064. CHECK_CLOSE (q1_12[3], q[3], 1e-4);
  1065. }
  1066. // This test compare the result of a slider with 2 bodies where body body 1 is
  1067. // fixed to the world to a slider with only one body at position 2.
  1068. //
  1069. // Test the limits [0, 0] when only one body at is attached to the joint
  1070. // using dJointAttache(jId, 0, bId);
  1071. //
  1072. // The body should not move since their is no room between the two limits
  1073. //
  1074. TEST_FIXTURE(Fixture_dxJointPiston_Compare_Body_At_Zero_Axis_Along_X,
  1075. test_Limit_0_0_One_Body_on_right)
  1076. {
  1077. dBodyAddForce (bId2_12, 4, 0, 0);
  1078. dJointAttach(jId_12, bId1_12, bId2_12);
  1079. dJointSetPistonParam(jId_12, dParamLoStop, 0);
  1080. dJointSetPistonParam(jId_12, dParamHiStop, 0);
  1081. dJointAttach(fixed, bId1_12, 0);
  1082. dJointSetFixed(fixed);
  1083. dJointAttach(jId, 0, bId);
  1084. dJointSetPistonParam(jId, dParamLoStop, 0);
  1085. dJointSetPistonParam(jId, dParamHiStop, 0);
  1086. for (int i=0; i<500; ++i)
  1087. dWorldStep(wId, 1.0);
  1088. const dReal *pos2_12 = dBodyGetPosition(bId2_12);
  1089. const dReal *pos = dBodyGetPosition(bId);
  1090. CHECK_CLOSE (pos2_12[0], pos[0], 1e-4);
  1091. CHECK_CLOSE (pos2_12[1], pos[1], 1e-4);
  1092. CHECK_CLOSE (pos2_12[2], pos[2], 1e-4);
  1093. CHECK_CLOSE (0, pos[0], 1e-4);
  1094. CHECK_CLOSE (0, pos[1], 1e-4);
  1095. CHECK_CLOSE (0, pos[2], 1e-4);
  1096. const dReal *q2_12 = dBodyGetQuaternion(bId2_12);
  1097. const dReal *q = dBodyGetQuaternion(bId);
  1098. CHECK_CLOSE (q2_12[0], q[0], 1e-4);
  1099. CHECK_CLOSE (q2_12[1], q[1], 1e-4);
  1100. CHECK_CLOSE (q2_12[2], q[2], 1e-4);
  1101. CHECK_CLOSE (q2_12[3], q[3], 1e-4);
  1102. }
  1103. } // End of SUITE TestdxJointPiston