collisionResponseAnimator.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (C) 2008-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. using namespace irr;
  5. using namespace core;
  6. using namespace scene;
  7. using namespace video;
  8. static bool expectedCollisionCallbackPositions = true;
  9. class CMyCollisionCallback : public ICollisionCallback
  10. {
  11. public:
  12. bool onCollision(const ISceneNodeAnimatorCollisionResponse& animator)
  13. {
  14. const vector3df & collisionPoint = animator.getCollisionPoint();
  15. logTestString("Collision callback at %f %f %f\n",
  16. collisionPoint.X, collisionPoint.Y, collisionPoint.Z);
  17. if(collisionPoint != ExpectedCollisionPoint)
  18. {
  19. logTestString("*** Error: collision point, expected %f %f %f\n",
  20. ExpectedCollisionPoint.X, ExpectedCollisionPoint.Y, ExpectedCollisionPoint.Z);
  21. expectedCollisionCallbackPositions = false;
  22. assert_log(false);
  23. }
  24. const vector3df & nodePosition = animator.getCollisionResultPosition();
  25. if(nodePosition != ExpectedNodePosition)
  26. {
  27. logTestString("*** Error: result position, expected %f %f %f\n",
  28. ExpectedNodePosition.X, ExpectedNodePosition.Y, ExpectedNodePosition.Z);
  29. expectedCollisionCallbackPositions = false;
  30. assert_log(false);
  31. }
  32. if(animator.getTargetNode() != ExpectedTarget)
  33. {
  34. logTestString("*** Error: wrong node\n");
  35. expectedCollisionCallbackPositions = false;
  36. assert_log(false);
  37. }
  38. return ConsumeNextCollision;
  39. }
  40. void setNextExpectedCollision(ISceneNode* target,
  41. const vector3df& expectedPoint,
  42. const vector3df& expectedPosition,
  43. bool consume)
  44. {
  45. ExpectedTarget = target;
  46. ExpectedCollisionPoint = expectedPoint;
  47. ExpectedNodePosition = expectedPosition;
  48. ConsumeNextCollision = consume;
  49. }
  50. private:
  51. ISceneNode * ExpectedTarget;
  52. vector3df ExpectedCollisionPoint;
  53. vector3df ExpectedNodePosition;
  54. bool ConsumeNextCollision;
  55. };
  56. /** Test that collision response animator will reset itself when removed from a
  57. scene node, so that the scene node can then be moved without the animator
  58. jumping it back again. */
  59. bool collisionResponseAnimator(void)
  60. {
  61. IrrlichtDevice * device = irr::createDevice(video::EDT_NULL);
  62. assert_log(device);
  63. if(!device)
  64. return false;
  65. ISceneManager * smgr = device->getSceneManager();
  66. // Create 2 nodes to the left of a "wall"
  67. ISceneNode * testNode1 = smgr->addEmptySceneNode();
  68. ISceneNode * testNode2 = smgr->addEmptySceneNode();
  69. testNode1->setPosition(vector3df(-50, 0,0));
  70. testNode2->setPosition(vector3df(-50, 0,0));
  71. // Create a "wall" node, and collision response animators for each test node.
  72. IMeshSceneNode * wallNode = smgr->addCubeSceneNode(10.f);
  73. ITriangleSelector * wallSelector = smgr->createTriangleSelectorFromBoundingBox(wallNode);
  74. ISceneNodeAnimatorCollisionResponse * collisionAnimator1 =
  75. smgr->createCollisionResponseAnimator(wallSelector,
  76. testNode1,
  77. vector3df(10,10,10),
  78. vector3df(0, 0, 0));
  79. testNode1->addAnimator(collisionAnimator1);
  80. CMyCollisionCallback collisionCallback;
  81. collisionAnimator1->setCollisionCallback(&collisionCallback);
  82. collisionAnimator1->drop();
  83. collisionAnimator1 = 0;
  84. ISceneNodeAnimatorCollisionResponse * collisionAnimator2 =
  85. smgr->createCollisionResponseAnimator(wallSelector,
  86. testNode2,
  87. vector3df(10,10,10),
  88. vector3df(0, 0, 0));
  89. testNode2->addAnimator(collisionAnimator2);
  90. collisionAnimator2->setCollisionCallback(&collisionCallback);
  91. wallSelector->drop();
  92. // Don't drop() collisionAnimator2 since we're going to use it.
  93. // Get the system in a good state
  94. device->run();
  95. smgr->drawAll();
  96. // Try to move both nodes to the right of the wall.
  97. // This one should be stopped by its animator.
  98. testNode1->setPosition(vector3df(50, 0,0));
  99. collisionCallback.setNextExpectedCollision(testNode1,
  100. vector3df(-5.005f, 0, 0),
  101. vector3df(-15.005f, 0, 0),
  102. false);
  103. // Whereas this one, by forcing the animator to update its target node, should be
  104. // able to pass through the wall. (In <=1.6 it was stopped by the wall even if
  105. // the animator was removed and later re-added);
  106. testNode2->setPosition(vector3df(50, 0,0));
  107. collisionAnimator2->setTargetNode(testNode2);
  108. collisionAnimator2->drop(); // We're done using this now.
  109. device->run();
  110. smgr->drawAll();
  111. bool result = true;
  112. if(testNode1->getAbsolutePosition().X > -15.f)
  113. {
  114. logTestString("collisionResponseAnimator test node 1 wasn't stopped from moving.\n");
  115. assert_log(false);
  116. result = false;
  117. }
  118. if(testNode2->getAbsolutePosition().X < 50.f)
  119. {
  120. logTestString("collisionResponseAnimator test node 2 was stopped from moving.\n");
  121. assert_log(false);
  122. result = false;
  123. }
  124. // Now try to move the second node back through the wall again. Now it should be
  125. // stopped by the wall.
  126. testNode2->setPosition(vector3df(-50, 0, 0));
  127. // We'll consume this collision, so the node will actually move all the way through.
  128. collisionCallback.setNextExpectedCollision(testNode2,
  129. vector3df(5.005f, 0, 0),
  130. vector3df(15.005f, 0, 0),
  131. true);
  132. device->run();
  133. smgr->drawAll();
  134. if(testNode2->getAbsolutePosition().X != -50.f)
  135. {
  136. logTestString("collisionResponseAnimator test node 2 was stopped from moving.\n");
  137. assert_log(false);
  138. result = false;
  139. }
  140. // Now we'll try to move it back to the right and allow it to be stopped.
  141. collisionCallback.setNextExpectedCollision(testNode2,
  142. vector3df(-5.005f, 0, 0),
  143. vector3df(-15.005f, 0, 0),
  144. false);
  145. testNode2->setPosition(vector3df(50, 0, 0));
  146. device->run();
  147. smgr->drawAll();
  148. if(testNode2->getAbsolutePosition().X > -15.f)
  149. {
  150. logTestString("collisionResponseAnimator test node 2 moved too far.\n");
  151. assert_log(false);
  152. result = false;
  153. }
  154. device->closeDevice();
  155. device->run();
  156. device->drop();
  157. result &= expectedCollisionCallbackPositions;
  158. return result;
  159. }