arvr_positional_tracker.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_tracker_id"), &ARVRPositionalTracker::get_tracker_id);
  39. ClassDB::bind_method(D_METHOD("get_name"), &ARVRPositionalTracker::get_name);
  40. ClassDB::bind_method(D_METHOD("get_joy_id"), &ARVRPositionalTracker::get_joy_id);
  41. ClassDB::bind_method(D_METHOD("get_tracks_orientation"), &ARVRPositionalTracker::get_tracks_orientation);
  42. ClassDB::bind_method(D_METHOD("get_orientation"), &ARVRPositionalTracker::get_orientation);
  43. ClassDB::bind_method(D_METHOD("get_tracks_position"), &ARVRPositionalTracker::get_tracks_position);
  44. ClassDB::bind_method(D_METHOD("get_position"), &ARVRPositionalTracker::get_position);
  45. ClassDB::bind_method(D_METHOD("get_hand"), &ARVRPositionalTracker::get_hand);
  46. ClassDB::bind_method(D_METHOD("get_transform", "adjust_by_reference_frame"), &ARVRPositionalTracker::get_transform);
  47. ClassDB::bind_method(D_METHOD("get_mesh"), &ARVRPositionalTracker::get_mesh);
  48. // these functions we don't want to expose to normal users but do need to be callable from GDNative
  49. ClassDB::bind_method(D_METHOD("_set_type", "type"), &ARVRPositionalTracker::set_type);
  50. ClassDB::bind_method(D_METHOD("_set_name", "name"), &ARVRPositionalTracker::set_name);
  51. ClassDB::bind_method(D_METHOD("_set_joy_id", "joy_id"), &ARVRPositionalTracker::set_joy_id);
  52. ClassDB::bind_method(D_METHOD("_set_orientation", "orientation"), &ARVRPositionalTracker::set_orientation);
  53. ClassDB::bind_method(D_METHOD("_set_rw_position", "rw_position"), &ARVRPositionalTracker::set_rw_position);
  54. ClassDB::bind_method(D_METHOD("_set_mesh", "mesh"), &ARVRPositionalTracker::set_mesh);
  55. ClassDB::bind_method(D_METHOD("get_rumble"), &ARVRPositionalTracker::get_rumble);
  56. ClassDB::bind_method(D_METHOD("set_rumble", "rumble"), &ARVRPositionalTracker::set_rumble);
  57. ADD_PROPERTY(PropertyInfo(Variant::REAL, "rumble"), "set_rumble", "get_rumble");
  58. };
  59. void ARVRPositionalTracker::set_type(ARVRServer::TrackerType p_type) {
  60. if (type != p_type) {
  61. type = p_type;
  62. hand = ARVRPositionalTracker::TRACKER_HAND_UNKNOWN;
  63. ARVRServer *arvr_server = ARVRServer::get_singleton();
  64. ERR_FAIL_NULL(arvr_server);
  65. // get a tracker id for our type
  66. // note if this is a controller this will be 3 or higher but we may change it later.
  67. tracker_id = arvr_server->get_free_tracker_id_for_type(p_type);
  68. };
  69. };
  70. ARVRServer::TrackerType ARVRPositionalTracker::get_type() const {
  71. return type;
  72. };
  73. void ARVRPositionalTracker::set_name(const String &p_name) {
  74. name = p_name;
  75. };
  76. StringName ARVRPositionalTracker::get_name() const {
  77. return name;
  78. };
  79. int ARVRPositionalTracker::get_tracker_id() const {
  80. return tracker_id;
  81. };
  82. void ARVRPositionalTracker::set_joy_id(int p_joy_id) {
  83. joy_id = p_joy_id;
  84. };
  85. int ARVRPositionalTracker::get_joy_id() const {
  86. return joy_id;
  87. };
  88. bool ARVRPositionalTracker::get_tracks_orientation() const {
  89. return tracks_orientation;
  90. };
  91. void ARVRPositionalTracker::set_orientation(const Basis &p_orientation) {
  92. _THREAD_SAFE_METHOD_
  93. tracks_orientation = true; // obviously we have this
  94. orientation = p_orientation;
  95. };
  96. Basis ARVRPositionalTracker::get_orientation() const {
  97. _THREAD_SAFE_METHOD_
  98. return orientation;
  99. };
  100. bool ARVRPositionalTracker::get_tracks_position() const {
  101. return tracks_position;
  102. };
  103. void ARVRPositionalTracker::set_position(const Vector3 &p_position) {
  104. _THREAD_SAFE_METHOD_
  105. ARVRServer *arvr_server = ARVRServer::get_singleton();
  106. ERR_FAIL_NULL(arvr_server);
  107. real_t world_scale = arvr_server->get_world_scale();
  108. ERR_FAIL_COND(world_scale == 0);
  109. tracks_position = true; // obviously we have this
  110. rw_position = p_position / world_scale;
  111. };
  112. Vector3 ARVRPositionalTracker::get_position() const {
  113. _THREAD_SAFE_METHOD_
  114. ARVRServer *arvr_server = ARVRServer::get_singleton();
  115. ERR_FAIL_NULL_V(arvr_server, rw_position);
  116. real_t world_scale = arvr_server->get_world_scale();
  117. return rw_position * world_scale;
  118. };
  119. void ARVRPositionalTracker::set_rw_position(const Vector3 &p_rw_position) {
  120. _THREAD_SAFE_METHOD_
  121. tracks_position = true; // obviously we have this
  122. rw_position = p_rw_position;
  123. };
  124. Vector3 ARVRPositionalTracker::get_rw_position() const {
  125. _THREAD_SAFE_METHOD_
  126. return rw_position;
  127. };
  128. void ARVRPositionalTracker::set_mesh(const Ref<Mesh> &p_mesh) {
  129. _THREAD_SAFE_METHOD_
  130. mesh = p_mesh;
  131. };
  132. Ref<Mesh> ARVRPositionalTracker::get_mesh() const {
  133. _THREAD_SAFE_METHOD_
  134. return mesh;
  135. };
  136. ARVRPositionalTracker::TrackerHand ARVRPositionalTracker::get_hand() const {
  137. return hand;
  138. };
  139. void ARVRPositionalTracker::set_hand(const ARVRPositionalTracker::TrackerHand p_hand) {
  140. ARVRServer *arvr_server = ARVRServer::get_singleton();
  141. ERR_FAIL_NULL(arvr_server);
  142. if (hand != p_hand) {
  143. // we can only set this if we've previously set this to be a controller!!
  144. ERR_FAIL_COND((type != ARVRServer::TRACKER_CONTROLLER) && (p_hand != ARVRPositionalTracker::TRACKER_HAND_UNKNOWN));
  145. hand = p_hand;
  146. if (hand == ARVRPositionalTracker::TRACKER_LEFT_HAND) {
  147. if (!arvr_server->is_tracker_id_in_use_for_type(type, 1)) {
  148. tracker_id = 1;
  149. };
  150. } else if (hand == ARVRPositionalTracker::TRACKER_RIGHT_HAND) {
  151. if (!arvr_server->is_tracker_id_in_use_for_type(type, 2)) {
  152. tracker_id = 2;
  153. };
  154. };
  155. };
  156. };
  157. Transform ARVRPositionalTracker::get_transform(bool p_adjust_by_reference_frame) const {
  158. Transform new_transform;
  159. new_transform.basis = get_orientation();
  160. new_transform.origin = get_position();
  161. if (p_adjust_by_reference_frame) {
  162. ARVRServer *arvr_server = ARVRServer::get_singleton();
  163. ERR_FAIL_NULL_V(arvr_server, new_transform);
  164. new_transform = arvr_server->get_reference_frame() * new_transform;
  165. };
  166. return new_transform;
  167. };
  168. real_t ARVRPositionalTracker::get_rumble() const {
  169. return rumble;
  170. };
  171. void ARVRPositionalTracker::set_rumble(real_t p_rumble) {
  172. if (p_rumble > 0.0) {
  173. rumble = p_rumble;
  174. } else {
  175. rumble = 0.0;
  176. };
  177. };
  178. ARVRPositionalTracker::ARVRPositionalTracker() {
  179. type = ARVRServer::TRACKER_UNKNOWN;
  180. name = "Unknown";
  181. joy_id = -1;
  182. tracker_id = 0;
  183. tracks_orientation = false;
  184. tracks_position = false;
  185. hand = TRACKER_HAND_UNKNOWN;
  186. rumble = 0.0;
  187. };
  188. ARVRPositionalTracker::~ARVRPositionalTracker(){
  189. };