arvr_positional_tracker.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*************************************************************************/
  2. /* arvr_positional_tracker.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "arvr_positional_tracker.h"
  31. #include "core/os/input.h"
  32. void ARVRPositionalTracker::_bind_methods() {
  33. BIND_ENUM_CONSTANT(TRACKER_HAND_UNKNOWN);
  34. BIND_ENUM_CONSTANT(TRACKER_LEFT_HAND);
  35. BIND_ENUM_CONSTANT(TRACKER_RIGHT_HAND);
  36. // this class is read only from GDScript, so we only have access to getters..
  37. ClassDB::bind_method(D_METHOD("get_type"), &ARVRPositionalTracker::get_type);
  38. ClassDB::bind_method(D_METHOD("get_name"), &ARVRPositionalTracker::get_name);
  39. ClassDB::bind_method(D_METHOD("get_joy_id"), &ARVRPositionalTracker::get_joy_id);
  40. ClassDB::bind_method(D_METHOD("get_tracks_orientation"), &ARVRPositionalTracker::get_tracks_orientation);
  41. ClassDB::bind_method(D_METHOD("get_orientation"), &ARVRPositionalTracker::get_orientation);
  42. ClassDB::bind_method(D_METHOD("get_tracks_position"), &ARVRPositionalTracker::get_tracks_position);
  43. ClassDB::bind_method(D_METHOD("get_position"), &ARVRPositionalTracker::get_position);
  44. ClassDB::bind_method(D_METHOD("get_hand"), &ARVRPositionalTracker::get_hand);
  45. ClassDB::bind_method(D_METHOD("get_transform", "adjust_by_reference_frame"), &ARVRPositionalTracker::get_transform);
  46. // these functions we don't want to expose to normal users but do need to be callable from GDNative
  47. ClassDB::bind_method(D_METHOD("_set_type", "type"), &ARVRPositionalTracker::set_type);
  48. ClassDB::bind_method(D_METHOD("_set_name", "name"), &ARVRPositionalTracker::set_name);
  49. ClassDB::bind_method(D_METHOD("_set_joy_id", "joy_id"), &ARVRPositionalTracker::set_joy_id);
  50. ClassDB::bind_method(D_METHOD("_set_orientation", "orientation"), &ARVRPositionalTracker::set_orientation);
  51. ClassDB::bind_method(D_METHOD("_set_rw_position", "rw_position"), &ARVRPositionalTracker::set_rw_position);
  52. ClassDB::bind_method(D_METHOD("get_rumble"), &ARVRPositionalTracker::get_rumble);
  53. ClassDB::bind_method(D_METHOD("set_rumble", "rumble"), &ARVRPositionalTracker::set_rumble);
  54. ADD_PROPERTY(PropertyInfo(Variant::REAL, "rumble"), "set_rumble", "get_rumble");
  55. };
  56. void ARVRPositionalTracker::set_type(ARVRServer::TrackerType p_type) {
  57. if (type != p_type) {
  58. type = p_type;
  59. hand = ARVRPositionalTracker::TRACKER_HAND_UNKNOWN;
  60. ARVRServer *arvr_server = ARVRServer::get_singleton();
  61. ERR_FAIL_NULL(arvr_server);
  62. // get a tracker id for our type
  63. // note if this is a controller this will be 3 or higher but we may change it later.
  64. tracker_id = arvr_server->get_free_tracker_id_for_type(p_type);
  65. };
  66. };
  67. ARVRServer::TrackerType ARVRPositionalTracker::get_type() const {
  68. return type;
  69. };
  70. void ARVRPositionalTracker::set_name(const String p_name) {
  71. name = p_name;
  72. };
  73. StringName ARVRPositionalTracker::get_name() const {
  74. return name;
  75. };
  76. int ARVRPositionalTracker::get_tracker_id() const {
  77. return tracker_id;
  78. };
  79. void ARVRPositionalTracker::set_joy_id(int p_joy_id) {
  80. joy_id = p_joy_id;
  81. };
  82. int ARVRPositionalTracker::get_joy_id() const {
  83. return joy_id;
  84. };
  85. bool ARVRPositionalTracker::get_tracks_orientation() const {
  86. return tracks_orientation;
  87. };
  88. void ARVRPositionalTracker::set_orientation(const Basis &p_orientation) {
  89. _THREAD_SAFE_METHOD_
  90. tracks_orientation = true; // obviously we have this
  91. orientation = p_orientation;
  92. };
  93. Basis ARVRPositionalTracker::get_orientation() const {
  94. _THREAD_SAFE_METHOD_
  95. return orientation;
  96. };
  97. bool ARVRPositionalTracker::get_tracks_position() const {
  98. return tracks_position;
  99. };
  100. void ARVRPositionalTracker::set_position(const Vector3 &p_position) {
  101. _THREAD_SAFE_METHOD_
  102. ARVRServer *arvr_server = ARVRServer::get_singleton();
  103. ERR_FAIL_NULL(arvr_server);
  104. real_t world_scale = arvr_server->get_world_scale();
  105. ERR_FAIL_COND(world_scale == 0);
  106. tracks_position = true; // obviously we have this
  107. rw_position = p_position / world_scale;
  108. };
  109. Vector3 ARVRPositionalTracker::get_position() const {
  110. _THREAD_SAFE_METHOD_
  111. ARVRServer *arvr_server = ARVRServer::get_singleton();
  112. ERR_FAIL_NULL_V(arvr_server, rw_position);
  113. real_t world_scale = arvr_server->get_world_scale();
  114. return rw_position * world_scale;
  115. };
  116. void ARVRPositionalTracker::set_rw_position(const Vector3 &p_rw_position) {
  117. _THREAD_SAFE_METHOD_
  118. tracks_position = true; // obviously we have this
  119. rw_position = p_rw_position;
  120. };
  121. Vector3 ARVRPositionalTracker::get_rw_position() const {
  122. _THREAD_SAFE_METHOD_
  123. return rw_position;
  124. };
  125. ARVRPositionalTracker::TrackerHand ARVRPositionalTracker::get_hand() const {
  126. return hand;
  127. };
  128. void ARVRPositionalTracker::set_hand(const ARVRPositionalTracker::TrackerHand p_hand) {
  129. ARVRServer *arvr_server = ARVRServer::get_singleton();
  130. ERR_FAIL_NULL(arvr_server);
  131. if (hand != p_hand) {
  132. // we can only set this if we've previously set this to be a controller!!
  133. ERR_FAIL_COND((type != ARVRServer::TRACKER_CONTROLLER) && (p_hand != ARVRPositionalTracker::TRACKER_HAND_UNKNOWN));
  134. hand = p_hand;
  135. if (hand == ARVRPositionalTracker::TRACKER_LEFT_HAND) {
  136. if (!arvr_server->is_tracker_id_in_use_for_type(type, 1)) {
  137. tracker_id = 1;
  138. };
  139. } else if (hand == ARVRPositionalTracker::TRACKER_RIGHT_HAND) {
  140. if (!arvr_server->is_tracker_id_in_use_for_type(type, 2)) {
  141. tracker_id = 2;
  142. };
  143. };
  144. };
  145. };
  146. Transform ARVRPositionalTracker::get_transform(bool p_adjust_by_reference_frame) const {
  147. Transform new_transform;
  148. new_transform.basis = get_orientation();
  149. new_transform.origin = get_position();
  150. if (p_adjust_by_reference_frame) {
  151. ARVRServer *arvr_server = ARVRServer::get_singleton();
  152. ERR_FAIL_NULL_V(arvr_server, new_transform);
  153. new_transform = arvr_server->get_reference_frame() * new_transform;
  154. };
  155. return new_transform;
  156. };
  157. real_t ARVRPositionalTracker::get_rumble() const {
  158. return rumble;
  159. };
  160. void ARVRPositionalTracker::set_rumble(real_t p_rumble) {
  161. if (p_rumble > 0.0) {
  162. rumble = p_rumble;
  163. } else {
  164. rumble = 0.0;
  165. };
  166. };
  167. ARVRPositionalTracker::ARVRPositionalTracker() {
  168. type = ARVRServer::TRACKER_UNKNOWN;
  169. name = "Unknown";
  170. joy_id = -1;
  171. tracker_id = 0;
  172. tracks_orientation = false;
  173. tracks_position = false;
  174. hand = TRACKER_HAND_UNKNOWN;
  175. rumble = 0.0;
  176. };
  177. ARVRPositionalTracker::~ARVRPositionalTracker(){
  178. };