piston.cpp 45 KB

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