PhysicsCollisionObjectSample.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "PhysicsCollisionObjectSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Physics", "Collision Objects", PhysicsCollisionObjectSample, 1);
  5. #endif
  6. PhysicsCollisionObjectSample::PhysicsCollisionObjectSample()
  7. : _font(NULL), _scene(NULL), _lightNode(NULL), _form(NULL), _objectType(SPHERE), _throw(true), _drawDebug(0), _wireFrame(false)
  8. {
  9. const char* paths[] = {"res/common/physics.physics#ball","res/common/physics.physics#box", "res/common/physics.physics#capsule", "res/common/physics.physics#duck"};
  10. _collisionObjectPaths.assign(paths, paths + 4);
  11. const char* nodeIds[] = {"sphere", "box", "capsule", "duck"};
  12. _nodeIds.assign(nodeIds, nodeIds + 4);
  13. const char* nodeNames[] = {"Sphere", "Box", "Capsule", "Duck"};
  14. _nodeNames.assign(nodeNames, nodeNames + 4);
  15. Vector4 colors[] = {Vector4(1, 0, 0, 1), Vector4(0.1f, 0.6f, 0.1f, 1), Vector4(0, 0, 1, 1), Vector4(1, 1, 0, 1)};
  16. _colors.assign(colors, colors + 4);
  17. }
  18. void PhysicsCollisionObjectSample::initialize()
  19. {
  20. // Create the font for drawing the framerate.
  21. _font = Font::create("res/ui/arial.gpb");
  22. _scene = Scene::load("res/common/physics.scene");
  23. // Use the aspect ratio of the display instead of the aspect ratio defined in the scene file.
  24. _scene->getActiveCamera()->setAspectRatio(getAspectRatio());
  25. _lightNode = _scene->findNode("directionalLight");
  26. _scene->visit(this, &PhysicsCollisionObjectSample::bindLights);
  27. _form = Form::create("res/common/physics.form");
  28. static_cast<Button*>(_form->getControl("wireframeButton"))->addListener(this, Control::Listener::CLICK);
  29. static_cast<Button*>(_form->getControl("drawDebugButton"))->addListener(this, Control::Listener::CLICK);
  30. static_cast<Button*>(_form->getControl("throwButton"))->addListener(this, Control::Listener::CLICK);
  31. Button* shapeButton = static_cast<Button*>(_form->getControl("shapeButton"));
  32. shapeButton->addListener(this, Control::Listener::CLICK);
  33. shapeButton->setTextColor(_colors[_objectType]);
  34. }
  35. void PhysicsCollisionObjectSample::finalize()
  36. {
  37. SAFE_RELEASE(_font);
  38. SAFE_RELEASE(_scene);
  39. SAFE_RELEASE(_form);
  40. }
  41. void PhysicsCollisionObjectSample::update(float elapsedTime)
  42. {
  43. }
  44. void PhysicsCollisionObjectSample::render(float elapsedTime)
  45. {
  46. // Clear the color and depth buffers
  47. clear(CLEAR_COLOR_DEPTH, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0);
  48. // Visit all the nodes in the scene, drawing the models/mesh.
  49. _scene->visit(this, &PhysicsCollisionObjectSample::drawScene);
  50. if (_drawDebug == 1)
  51. {
  52. // Draw the physics debug information.
  53. getPhysicsController()->drawDebug(_scene->getActiveCamera()->getViewProjectionMatrix());
  54. }
  55. drawFrameRate(_font, Vector4(0, 0.5f, 1, 1), 5, 1, getFrameRate());
  56. _form->draw();
  57. }
  58. void PhysicsCollisionObjectSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  59. {
  60. switch (evt)
  61. {
  62. case Touch::TOUCH_PRESS:
  63. if (x < 75 && y < 50)
  64. {
  65. // Toggle Vsync if the user touches the top left corner.
  66. setVsync(!isVsync());
  67. }
  68. else
  69. {
  70. // Fire a physics object into the scene.
  71. Ray ray;
  72. Camera* camera = _scene->getActiveCamera();
  73. camera->pickRay(getViewport(), x, y, &ray);
  74. fireProjectile(ray);
  75. }
  76. break;
  77. case Touch::TOUCH_RELEASE:
  78. break;
  79. case Touch::TOUCH_MOVE:
  80. break;
  81. };
  82. }
  83. void PhysicsCollisionObjectSample::keyEvent(Keyboard::KeyEvent evt, int key)
  84. {
  85. if (evt == Keyboard::KEY_PRESS)
  86. {
  87. switch (key)
  88. {
  89. case Keyboard::KEY_B:
  90. case Keyboard::KEY_CAPITAL_B:
  91. incrementDebugDraw();
  92. break;
  93. case Keyboard::KEY_M:
  94. case Keyboard::KEY_CAPITAL_M:
  95. toggleWireframe();
  96. break;
  97. }
  98. }
  99. }
  100. bool PhysicsCollisionObjectSample::drawScene(Node* node)
  101. {
  102. Model* model = node->getModel();
  103. if (model)
  104. model->draw(_wireFrame);
  105. return true;
  106. }
  107. bool PhysicsCollisionObjectSample::bindLights(Node* node)
  108. {
  109. Model* model = node->getModel();
  110. if (model)
  111. {
  112. Material* material = model->getMaterial();
  113. if (material)
  114. {
  115. MaterialParameter* ambientColorParam = material->getParameter("u_ambientColor");
  116. if (ambientColorParam)
  117. {
  118. ambientColorParam->setValue(_scene->getAmbientColor());
  119. }
  120. if (_lightNode && _lightNode->getLight())
  121. {
  122. MaterialParameter* lightDirectionParam = material->getParameter("u_directionalLightDirection[0]");
  123. MaterialParameter* lightColorParam = material->getParameter("u_directionalLightColor[0]");
  124. if (lightDirectionParam)
  125. {
  126. lightDirectionParam->bindValue(_lightNode, &Node::getForwardVectorView);
  127. }
  128. if (lightColorParam)
  129. {
  130. lightColorParam->setValue(_lightNode->getLight()->getColor());
  131. }
  132. }
  133. }
  134. }
  135. return true;
  136. }
  137. void PhysicsCollisionObjectSample::fireProjectile(const Ray& ray)
  138. {
  139. Node* clone = _scene->findNode(_nodeIds[_objectType])->clone();
  140. clone->setRotation(Quaternion::identity());
  141. if (_throw)
  142. {
  143. clone->setTranslation(ray.getOrigin());
  144. }
  145. else
  146. {
  147. // Find the position where the pick ray intersects with the floor.
  148. float distance = ray.intersects(Plane(0, 1, 0, -0.5f));
  149. if (distance != Ray::INTERSECTS_NONE)
  150. {
  151. Vector3 position((ray.getDirection() * distance) + ray.getOrigin());
  152. position.y += 8.0f;
  153. clone->setTranslation(position);
  154. }
  155. }
  156. // It is important to set the transform before attaching the collision object because this rigid body is not kinematic.
  157. // Once the non-kinematic rigid body is attached, you should only move the object using forces.
  158. PhysicsCollisionObject* collisionObject = clone->setCollisionObject(_collisionObjectPaths[_objectType]);
  159. _scene->addNode(clone);
  160. if (_throw)
  161. {
  162. PhysicsRigidBody* rigidBody = static_cast<PhysicsRigidBody*>(collisionObject);
  163. Vector3 impulse(ray.getDirection());
  164. impulse.normalize();
  165. impulse.scale(50.0f * rigidBody->getMass());
  166. rigidBody->applyImpulse(impulse);
  167. }
  168. // Release the new cloned node because the scene now holds the reference to it.
  169. clone->release();
  170. }
  171. void PhysicsCollisionObjectSample::incrementDebugDraw()
  172. {
  173. _drawDebug = (_drawDebug + 1) % 2;
  174. static_cast<Button*>(_form->getControl("drawDebugButton"))->setText(_drawDebug == 0 ? "Normal" : "Debug");
  175. }
  176. void PhysicsCollisionObjectSample::toggleWireframe()
  177. {
  178. _wireFrame = !_wireFrame;
  179. static_cast<Button*>(_form->getControl("wireframeButton"))->setText(_wireFrame ? "Wireframe" : "Solid");
  180. }
  181. void PhysicsCollisionObjectSample::controlEvent(Control* control, EventType evt)
  182. {
  183. Button* button = static_cast<Button*>(control);
  184. if (strcmp(button->getId(), "wireframeButton") == 0)
  185. {
  186. toggleWireframe();
  187. }
  188. else if (strcmp(button->getId(), "drawDebugButton") == 0)
  189. {
  190. incrementDebugDraw();
  191. }
  192. else if (strcmp(button->getId(), "throwButton") == 0)
  193. {
  194. _throw = !_throw;
  195. button->setText(_throw ? "Throw" : "Drop");
  196. }
  197. else if (strcmp(button->getId(), "shapeButton") == 0)
  198. {
  199. if (_objectType == SPHERE)
  200. _objectType = BOX;
  201. else if (_objectType == BOX)
  202. _objectType = CAPSULE;
  203. else if (_objectType == CAPSULE)
  204. _objectType = DUCK;
  205. else
  206. _objectType = SPHERE;
  207. button->setText(_nodeNames[_objectType]);
  208. button->setTextColor(_colors[_objectType]);
  209. }
  210. }