BoxStack.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "BoxStack.h"
  2. #include "../CommonInterfaces/CommonGraphicsAppInterface.h"
  3. #include "Bullet3Common/b3AlignedObjectArray.h"
  4. #include "../CommonInterfaces/CommonRenderInterface.h"
  5. #include "../CommonInterfaces/CommonExampleInterface.h"
  6. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  7. #include "../SharedMemory/PhysicsServerSharedMemory.h"
  8. #include "../SharedMemory/PhysicsClientC_API.h"
  9. #include "../CommonInterfaces/CommonParameterInterface.h"
  10. #include "../SharedMemory/SharedMemoryPublic.h"
  11. #include <string>
  12. #include "../RobotSimulator/b3RobotSimulatorClientAPI.h"
  13. #include "../Utils/b3Clock.h"
  14. class BoxStackExample : public CommonExampleInterface
  15. {
  16. CommonGraphicsApp* m_app;
  17. GUIHelperInterface* m_guiHelper;
  18. b3RobotSimulatorClientAPI m_robotSim;
  19. int m_options;
  20. public:
  21. BoxStackExample(GUIHelperInterface* helper, int options)
  22. : m_app(helper->getAppInterface()),
  23. m_guiHelper(helper),
  24. m_options(options)
  25. {
  26. m_app->setUpAxis(2);
  27. }
  28. virtual ~BoxStackExample()
  29. {
  30. }
  31. virtual void physicsDebugDraw(int debugDrawMode)
  32. {
  33. m_robotSim.debugDraw(debugDrawMode);
  34. }
  35. virtual void initPhysics()
  36. {
  37. int mode = eCONNECT_EXISTING_EXAMPLE_BROWSER;
  38. m_robotSim.setGuiHelper(m_guiHelper);
  39. bool connected = m_robotSim.connect(mode);
  40. b3Printf("robotSim connected = %d", connected);
  41. m_robotSim.configureDebugVisualizer(COV_ENABLE_RGB_BUFFER_PREVIEW, 0);
  42. m_robotSim.configureDebugVisualizer(COV_ENABLE_DEPTH_BUFFER_PREVIEW, 0);
  43. m_robotSim.configureDebugVisualizer(COV_ENABLE_SEGMENTATION_MARK_PREVIEW, 0);
  44. b3RobotSimulatorLoadUrdfFileArgs args;
  45. b3RobotSimulatorChangeDynamicsArgs dynamicsArgs;
  46. int massRatio = 4;
  47. int mass = 1;
  48. for (int i = 0; i < 8; i++)
  49. {
  50. args.m_startPosition.setValue(0, 0, i * .06);
  51. int boxIdx = m_robotSim.loadURDF("cube_small.urdf", args);
  52. dynamicsArgs.m_mass = mass;
  53. m_robotSim.changeDynamics(boxIdx, -1, dynamicsArgs);
  54. mass *= massRatio;
  55. }
  56. m_robotSim.loadURDF("plane.urdf");
  57. m_robotSim.setGravity(btVector3(0, 0, -10));
  58. }
  59. virtual void exitPhysics()
  60. {
  61. m_robotSim.disconnect();
  62. }
  63. virtual void stepSimulation(float deltaTime)
  64. {
  65. m_robotSim.stepSimulation();
  66. }
  67. virtual void renderScene()
  68. {
  69. m_robotSim.renderScene();
  70. }
  71. virtual bool mouseMoveCallback(float x, float y)
  72. {
  73. return m_robotSim.mouseMoveCallback(x, y);
  74. }
  75. virtual bool mouseButtonCallback(int button, int state, float x, float y)
  76. {
  77. return m_robotSim.mouseButtonCallback(button, state, x, y);
  78. }
  79. virtual bool keyboardCallback(int key, int state)
  80. {
  81. return false;
  82. }
  83. virtual void resetCamera()
  84. {
  85. float dist = 1.5;
  86. float pitch = -10;
  87. float yaw = 18;
  88. float targetPos[3] = {-0.2, 0.8, 0.3};
  89. m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
  90. if (m_app->m_renderer && m_app->m_renderer->getActiveCamera())
  91. {
  92. m_app->m_renderer->getActiveCamera()->setCameraDistance(dist);
  93. m_app->m_renderer->getActiveCamera()->setCameraPitch(pitch);
  94. m_app->m_renderer->getActiveCamera()->setCameraYaw(yaw);
  95. m_app->m_renderer->getActiveCamera()->setCameraTargetPosition(targetPos[0], targetPos[1], targetPos[2]);
  96. }
  97. }
  98. };
  99. class CommonExampleInterface* BoxStackExampleCreateFunc(struct CommonExampleOptions& options)
  100. {
  101. return new BoxStackExample(options.m_guiHelper, options.m_option);
  102. }