You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ovaC3DEntityManipulator.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "ovaC3DEntityManipulator.h"
  2. REGISTER_OBJECT_FACTORY(C3DEntityManipulator, "ovaC3DEntityManipulator");
  3. C3DEntityManipulator::C3DEntityManipulator(OMK::Controller& controller, const OMK::ObjectDescriptor& objectDesc) : OMK::SimulatedObject(controller, objectDesc) { }
  4. C3DEntityManipulator::~C3DEntityManipulator() { }
  5. void C3DEntityManipulator::init()
  6. {
  7. registerForSignal(g_sVrpnButtonStateUpdate);
  8. registerForSignal(g_sVrpnAnalogStateUpdate);
  9. m_ballPosition[0] = 1000000;
  10. m_ballPosition[1] = 1000000;
  11. m_ballPosition[2] = 1000000;
  12. m_button[0] = false;
  13. m_button[1] = false;
  14. m_button[2] = false;
  15. }
  16. void C3DEntityManipulator::compute()
  17. {
  18. if (m_button[0] && m_Analogs.size())
  19. {
  20. while (m_Analogs.size())
  21. {
  22. double value = m_Analogs.front() * 0.002;
  23. m_ballSpeed[0] = 0;
  24. m_ballSpeed[1] = 0;
  25. m_ballSpeed[2] = value;
  26. m_ballPosition[0] += m_ballSpeed[0];
  27. m_ballPosition[1] += m_ballSpeed[1];
  28. m_ballPosition[2] += m_ballSpeed[2];
  29. m_Analogs.pop_front();
  30. }
  31. }
  32. else
  33. {
  34. m_ballPosition[0] += m_ballSpeed[0];
  35. m_ballPosition[1] += m_ballSpeed[1];
  36. m_ballPosition[2] += m_ballSpeed[2];
  37. }
  38. const double viscosity = 0.010;
  39. m_ballSpeed[0] *= (1 - viscosity);
  40. m_ballSpeed[1] *= (1 - viscosity);
  41. m_ballSpeed[2] *= (1 - viscosity);
  42. Position far;
  43. far[0] = 1000000;
  44. far[1] = 1000000;
  45. far[2] = 1000000;
  46. sendValuedEvent((m_button[1] || m_button[2]) ? "OpenViBE_ball" : "OpenViBE_passive_ball", g_sManipulate3DEntity_SetPosition, PositionType(m_ballPosition));
  47. sendValuedEvent((m_button[1] || m_button[2]) ? "OpenViBE_passive_ball" : "OpenViBE_ball", g_sManipulate3DEntity_SetPosition, PositionType(far));
  48. Orientation orientation;
  49. orientation[0] = 0;
  50. orientation[1] = (m_ballPosition[2] * 180.0 / M_PI) / 0.5;
  51. orientation[2] = 0;
  52. sendValuedEvent("OpenViBE_passive_ball", g_sManipulate3DEntity_SetOrientation, OrientationType(orientation));
  53. sendValuedEvent("OpenViBE_ball", g_sManipulate3DEntity_SetOrientation, OrientationType(orientation));
  54. if (m_ballPosition[2] < -7 || m_ballPosition[2]>7)
  55. {
  56. sendEvent(getName(), g_s3DEntityManipulator_Reset);
  57. sendEvent("OpenViBE_goal_display", g_sManipulate3DEntity_Goal);
  58. }
  59. }
  60. bool C3DEntityManipulator::processEvent(OMK::Event* pEvent)
  61. {
  62. if (pEvent->eventId == g_s3DEntityManipulator_Reset)
  63. {
  64. m_ballPosition[0] = 0;
  65. m_ballPosition[1] = 0.5;
  66. m_ballPosition[2] = 0;
  67. m_ballSpeed[0] = 0;
  68. m_ballSpeed[1] = 0;
  69. m_ballSpeed[2] = 0;
  70. return true;
  71. }
  72. if (pEvent->eventId == g_sVrpnButtonStateUpdate)
  73. {
  74. VrpnButtonStateEvent* event = dynamic_cast <VrpnButtonStateEvent*>(pEvent);
  75. if (event)
  76. {
  77. const VrpnButtonState& vrpnButtonState = event->value;
  78. if (vrpnButtonState.first<int(sizeof(m_button) / sizeof(m_button[0])))
  79. {
  80. m_button[vrpnButtonState.first] = (vrpnButtonState.second ? true : false);
  81. }
  82. }
  83. return true;
  84. }
  85. if (pEvent->eventId == g_sVrpnAnalogStateUpdate)
  86. {
  87. VrpnAnalogStateEvent* event = dynamic_cast <VrpnAnalogStateEvent*>(pEvent);
  88. if (event)
  89. {
  90. const VrpnAnalogState& vrpnAnalogState = event->value;
  91. m_AnalogsCache.push_back(vrpnAnalogState.front());
  92. if (m_AnalogsCache.size() > 10) { m_AnalogsCache.pop_front(); }
  93. if (m_button[0])
  94. {
  95. double analog = 0;
  96. for (auto it = m_AnalogsCache.begin(); it != m_AnalogsCache.end(); ++it) { analog += *it; }
  97. m_Analogs.push_back(analog / m_AnalogsCache.size());
  98. }
  99. }
  100. return true;
  101. }
  102. return false;
  103. }