openxr_hand_tracking_extension.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**************************************************************************/
  2. /* openxr_hand_tracking_extension.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "openxr_hand_tracking_extension.h"
  31. #include "../openxr_api.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/string/print_string.h"
  34. #include "servers/xr_server.h"
  35. #include <openxr/openxr.h>
  36. OpenXRHandTrackingExtension *OpenXRHandTrackingExtension::singleton = nullptr;
  37. OpenXRHandTrackingExtension *OpenXRHandTrackingExtension::get_singleton() {
  38. return singleton;
  39. }
  40. OpenXRHandTrackingExtension::OpenXRHandTrackingExtension() {
  41. singleton = this;
  42. // Make sure this is cleared until we actually request it
  43. handTrackingSystemProperties.supportsHandTracking = false;
  44. }
  45. OpenXRHandTrackingExtension::~OpenXRHandTrackingExtension() {
  46. singleton = nullptr;
  47. }
  48. HashMap<String, bool *> OpenXRHandTrackingExtension::get_requested_extensions() {
  49. HashMap<String, bool *> request_extensions;
  50. request_extensions[XR_EXT_HAND_TRACKING_EXTENSION_NAME] = &hand_tracking_ext;
  51. request_extensions[XR_EXT_HAND_JOINTS_MOTION_RANGE_EXTENSION_NAME] = &hand_motion_range_ext;
  52. request_extensions[XR_EXT_HAND_TRACKING_DATA_SOURCE_EXTENSION_NAME] = &hand_tracking_source_ext;
  53. return request_extensions;
  54. }
  55. void OpenXRHandTrackingExtension::on_instance_created(const XrInstance p_instance) {
  56. if (hand_tracking_ext) {
  57. EXT_INIT_XR_FUNC(xrCreateHandTrackerEXT);
  58. EXT_INIT_XR_FUNC(xrDestroyHandTrackerEXT);
  59. EXT_INIT_XR_FUNC(xrLocateHandJointsEXT);
  60. hand_tracking_ext = xrCreateHandTrackerEXT_ptr && xrDestroyHandTrackerEXT_ptr && xrLocateHandJointsEXT_ptr;
  61. }
  62. }
  63. void OpenXRHandTrackingExtension::on_session_destroyed() {
  64. cleanup_hand_tracking();
  65. }
  66. void OpenXRHandTrackingExtension::on_instance_destroyed() {
  67. xrCreateHandTrackerEXT_ptr = nullptr;
  68. xrDestroyHandTrackerEXT_ptr = nullptr;
  69. xrLocateHandJointsEXT_ptr = nullptr;
  70. }
  71. void *OpenXRHandTrackingExtension::set_system_properties_and_get_next_pointer(void *p_next_pointer) {
  72. if (!hand_tracking_ext) {
  73. // not supported...
  74. return p_next_pointer;
  75. }
  76. handTrackingSystemProperties = {
  77. XR_TYPE_SYSTEM_HAND_TRACKING_PROPERTIES_EXT, // type
  78. p_next_pointer, // next
  79. false, // supportsHandTracking
  80. };
  81. return &handTrackingSystemProperties;
  82. }
  83. void OpenXRHandTrackingExtension::on_state_ready() {
  84. if (!handTrackingSystemProperties.supportsHandTracking) {
  85. // not supported...
  86. return;
  87. }
  88. // Setup our hands and reset data
  89. for (int i = 0; i < OPENXR_MAX_TRACKED_HANDS; i++) {
  90. // we'll do this later
  91. hand_trackers[i].is_initialized = false;
  92. hand_trackers[i].hand_tracker = XR_NULL_HANDLE;
  93. hand_trackers[i].locations.isActive = false;
  94. for (int j = 0; j < XR_HAND_JOINT_COUNT_EXT; j++) {
  95. hand_trackers[i].joint_locations[j] = { 0, { { 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 } }, 0.0 };
  96. hand_trackers[i].joint_velocities[j] = { 0, { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 } };
  97. }
  98. }
  99. }
  100. void OpenXRHandTrackingExtension::on_process() {
  101. if (!handTrackingSystemProperties.supportsHandTracking) {
  102. // not supported...
  103. return;
  104. }
  105. // process our hands
  106. const XrTime time = OpenXRAPI::get_singleton()->get_predicted_display_time();
  107. if (time == 0) {
  108. // we don't have timing info yet, or we're skipping a frame...
  109. return;
  110. }
  111. XrResult result;
  112. for (int i = 0; i < OPENXR_MAX_TRACKED_HANDS; i++) {
  113. if (hand_trackers[i].hand_tracker == XR_NULL_HANDLE) {
  114. void *next_pointer = nullptr;
  115. // Originally not all XR runtimes supported hand tracking data sourced both from controllers and normal hand tracking.
  116. // With this extension we can indicate we accept input from both sources so hand tracking data is consistently provided
  117. // on runtimes that support this.
  118. XrHandTrackingDataSourceEXT data_sources[2] = { XR_HAND_TRACKING_DATA_SOURCE_UNOBSTRUCTED_EXT, XR_HAND_TRACKING_DATA_SOURCE_CONTROLLER_EXT };
  119. XrHandTrackingDataSourceInfoEXT data_source_info = { XR_TYPE_HAND_TRACKING_DATA_SOURCE_INFO_EXT, next_pointer, 2, data_sources };
  120. if (hand_tracking_source_ext) {
  121. // If supported include this info
  122. next_pointer = &data_source_info;
  123. }
  124. XrHandTrackerCreateInfoEXT create_info = {
  125. XR_TYPE_HAND_TRACKER_CREATE_INFO_EXT, // type
  126. next_pointer, // next
  127. i == 0 ? XR_HAND_LEFT_EXT : XR_HAND_RIGHT_EXT, // hand
  128. XR_HAND_JOINT_SET_DEFAULT_EXT, // handJointSet
  129. };
  130. result = xrCreateHandTrackerEXT(OpenXRAPI::get_singleton()->get_session(), &create_info, &hand_trackers[i].hand_tracker);
  131. if (XR_FAILED(result)) {
  132. // not successful? then we do nothing.
  133. print_line("OpenXR: Failed to obtain hand tracking information [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  134. hand_trackers[i].is_initialized = false;
  135. } else {
  136. next_pointer = nullptr;
  137. hand_trackers[i].velocities.type = XR_TYPE_HAND_JOINT_VELOCITIES_EXT;
  138. hand_trackers[i].velocities.next = next_pointer;
  139. hand_trackers[i].velocities.jointCount = XR_HAND_JOINT_COUNT_EXT;
  140. hand_trackers[i].velocities.jointVelocities = hand_trackers[i].joint_velocities;
  141. next_pointer = &hand_trackers[i].velocities;
  142. if (hand_tracking_source_ext) {
  143. hand_trackers[i].data_source.type = XR_TYPE_HAND_TRACKING_DATA_SOURCE_STATE_EXT;
  144. hand_trackers[i].data_source.next = next_pointer;
  145. hand_trackers[i].data_source.isActive = false;
  146. hand_trackers[i].data_source.dataSource = XrHandTrackingDataSourceEXT(0);
  147. next_pointer = &hand_trackers[i].data_source;
  148. }
  149. // Needed for vendor hand tracking extensions implemented from GDExtension.
  150. for (OpenXRExtensionWrapper *wrapper : OpenXRAPI::get_singleton()->get_registered_extension_wrappers()) {
  151. void *np = wrapper->set_hand_joint_locations_and_get_next_pointer(i, next_pointer);
  152. if (np != nullptr) {
  153. next_pointer = np;
  154. }
  155. }
  156. hand_trackers[i].locations.type = XR_TYPE_HAND_JOINT_LOCATIONS_EXT;
  157. hand_trackers[i].locations.next = next_pointer;
  158. hand_trackers[i].locations.isActive = false;
  159. hand_trackers[i].locations.jointCount = XR_HAND_JOINT_COUNT_EXT;
  160. hand_trackers[i].locations.jointLocations = hand_trackers[i].joint_locations;
  161. Ref<XRHandTracker> godot_tracker;
  162. godot_tracker.instantiate();
  163. godot_tracker->set_tracker_hand(i == 0 ? XRPositionalTracker::TRACKER_HAND_LEFT : XRPositionalTracker::TRACKER_HAND_RIGHT);
  164. godot_tracker->set_tracker_name(i == 0 ? "/user/hand_tracker/left" : "/user/hand_tracker/right");
  165. XRServer::get_singleton()->add_tracker(godot_tracker);
  166. hand_trackers[i].godot_tracker = godot_tracker;
  167. hand_trackers[i].is_initialized = true;
  168. }
  169. }
  170. if (hand_trackers[i].is_initialized) {
  171. Ref<XRHandTracker> godot_tracker = hand_trackers[i].godot_tracker;
  172. void *next_pointer = nullptr;
  173. XrHandJointsMotionRangeInfoEXT motion_range_info = { XR_TYPE_HAND_JOINTS_MOTION_RANGE_INFO_EXT, next_pointer, hand_trackers[i].motion_range };
  174. if (hand_motion_range_ext) {
  175. next_pointer = &motion_range_info;
  176. }
  177. XrHandJointsLocateInfoEXT locateInfo = {
  178. XR_TYPE_HAND_JOINTS_LOCATE_INFO_EXT, // type
  179. next_pointer, // next
  180. OpenXRAPI::get_singleton()->get_play_space(), // baseSpace
  181. time, // time
  182. };
  183. result = xrLocateHandJointsEXT(hand_trackers[i].hand_tracker, &locateInfo, &hand_trackers[i].locations);
  184. if (XR_FAILED(result)) {
  185. // not successful? then we do nothing.
  186. print_line("OpenXR: Failed to get tracking for hand", i, "[", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  187. godot_tracker->set_has_tracking_data(false);
  188. continue;
  189. }
  190. // For some reason an inactive controller isn't coming back as inactive but has coordinates either as NAN or very large
  191. const XrPosef &palm = hand_trackers[i].joint_locations[XR_HAND_JOINT_PALM_EXT].pose;
  192. if (!hand_trackers[i].locations.isActive || isnan(palm.position.x) || palm.position.x < -1000000.00 || palm.position.x > 1000000.00) {
  193. hand_trackers[i].locations.isActive = false; // workaround, make sure its inactive
  194. }
  195. if (hand_trackers[i].locations.isActive) {
  196. godot_tracker->set_has_tracking_data(true);
  197. // SKELETON_RIG_HUMANOID bone adjustment. This rotation performs:
  198. // OpenXR Z+ -> Godot Humanoid Y- (Back along the bone)
  199. // OpenXR Y+ -> Godot Humanoid Z- (Out the back of the hand)
  200. const Quaternion bone_adjustment(0.0, -Math_SQRT12, Math_SQRT12, 0.0);
  201. for (int joint = 0; joint < XR_HAND_JOINT_COUNT_EXT; joint++) {
  202. const XrHandJointLocationEXT &location = hand_trackers[i].joint_locations[joint];
  203. const XrHandJointVelocityEXT &velocity = hand_trackers[i].joint_velocities[joint];
  204. const XrHandTrackingDataSourceStateEXT &data_source = hand_trackers[i].data_source;
  205. const XrPosef &pose = location.pose;
  206. Transform3D transform;
  207. Vector3 linear_velocity;
  208. Vector3 angular_velocity;
  209. BitField<XRHandTracker::HandJointFlags> flags;
  210. if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) {
  211. if (pose.orientation.x != 0 || pose.orientation.y != 0 || pose.orientation.z != 0 || pose.orientation.w != 0) {
  212. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_ORIENTATION_VALID);
  213. transform.basis = Basis(Quaternion(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w) * bone_adjustment);
  214. }
  215. }
  216. if (location.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {
  217. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_POSITION_VALID);
  218. transform.origin = Vector3(pose.position.x, pose.position.y, pose.position.z);
  219. }
  220. if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) {
  221. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_ORIENTATION_TRACKED);
  222. }
  223. if (location.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT) {
  224. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_POSITION_TRACKED);
  225. }
  226. if (location.locationFlags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT) {
  227. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_LINEAR_VELOCITY_VALID);
  228. linear_velocity = Vector3(velocity.linearVelocity.x, velocity.linearVelocity.y, velocity.linearVelocity.z);
  229. godot_tracker->set_hand_joint_linear_velocity((XRHandTracker::HandJoint)joint, linear_velocity);
  230. }
  231. if (location.locationFlags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT) {
  232. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_ANGULAR_VELOCITY_VALID);
  233. angular_velocity = Vector3(velocity.angularVelocity.x, velocity.angularVelocity.y, velocity.angularVelocity.z);
  234. godot_tracker->set_hand_joint_angular_velocity((XRHandTracker::HandJoint)joint, angular_velocity);
  235. }
  236. godot_tracker->set_hand_joint_flags((XRHandTracker::HandJoint)joint, flags);
  237. godot_tracker->set_hand_joint_transform((XRHandTracker::HandJoint)joint, transform);
  238. godot_tracker->set_hand_joint_radius((XRHandTracker::HandJoint)joint, location.radius);
  239. if (joint == XR_HAND_JOINT_PALM_EXT) {
  240. XRHandTracker::HandTrackingSource source = XRHandTracker::HAND_TRACKING_SOURCE_UNKNOWN;
  241. if (data_source.dataSource == XR_HAND_TRACKING_DATA_SOURCE_UNOBSTRUCTED_EXT) {
  242. source = XRHandTracker::HAND_TRACKING_SOURCE_UNOBSTRUCTED;
  243. } else if (data_source.dataSource == XR_HAND_TRACKING_DATA_SOURCE_CONTROLLER_EXT) {
  244. source = XRHandTracker::HAND_TRACKING_SOURCE_CONTROLLER;
  245. }
  246. godot_tracker->set_hand_tracking_source(source);
  247. godot_tracker->set_pose("default", transform, linear_velocity, angular_velocity);
  248. }
  249. }
  250. } else {
  251. godot_tracker->set_has_tracking_data(false);
  252. godot_tracker->invalidate_pose("default");
  253. }
  254. }
  255. }
  256. }
  257. void OpenXRHandTrackingExtension::on_state_stopping() {
  258. // cleanup
  259. cleanup_hand_tracking();
  260. }
  261. void OpenXRHandTrackingExtension::cleanup_hand_tracking() {
  262. XRServer *xr_server = XRServer::get_singleton();
  263. ERR_FAIL_NULL(xr_server);
  264. for (int i = 0; i < OPENXR_MAX_TRACKED_HANDS; i++) {
  265. if (hand_trackers[i].hand_tracker != XR_NULL_HANDLE) {
  266. xrDestroyHandTrackerEXT(hand_trackers[i].hand_tracker);
  267. hand_trackers[i].is_initialized = false;
  268. hand_trackers[i].hand_tracker = XR_NULL_HANDLE;
  269. XRServer::get_singleton()->remove_tracker(hand_trackers[i].godot_tracker);
  270. }
  271. }
  272. }
  273. bool OpenXRHandTrackingExtension::get_active() {
  274. return handTrackingSystemProperties.supportsHandTracking;
  275. }
  276. const OpenXRHandTrackingExtension::HandTracker *OpenXRHandTrackingExtension::get_hand_tracker(HandTrackedHands p_hand) const {
  277. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, nullptr);
  278. return &hand_trackers[p_hand];
  279. }
  280. XrHandJointsMotionRangeEXT OpenXRHandTrackingExtension::get_motion_range(HandTrackedHands p_hand) const {
  281. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, XR_HAND_JOINTS_MOTION_RANGE_MAX_ENUM_EXT);
  282. return hand_trackers[p_hand].motion_range;
  283. }
  284. OpenXRHandTrackingExtension::HandTrackedSource OpenXRHandTrackingExtension::get_hand_tracking_source(HandTrackedHands p_hand) const {
  285. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, OPENXR_SOURCE_UNKNOWN);
  286. if (hand_tracking_source_ext && hand_trackers[p_hand].data_source.isActive) {
  287. switch (hand_trackers[p_hand].data_source.dataSource) {
  288. case XR_HAND_TRACKING_DATA_SOURCE_UNOBSTRUCTED_EXT:
  289. return OPENXR_SOURCE_UNOBSTRUCTED;
  290. case XR_HAND_TRACKING_DATA_SOURCE_CONTROLLER_EXT:
  291. return OPENXR_SOURCE_CONTROLLER;
  292. default:
  293. return OPENXR_SOURCE_UNKNOWN;
  294. }
  295. }
  296. return OPENXR_SOURCE_UNKNOWN;
  297. }
  298. void OpenXRHandTrackingExtension::set_motion_range(HandTrackedHands p_hand, XrHandJointsMotionRangeEXT p_motion_range) {
  299. ERR_FAIL_UNSIGNED_INDEX(p_hand, OPENXR_MAX_TRACKED_HANDS);
  300. hand_trackers[p_hand].motion_range = p_motion_range;
  301. }
  302. XrSpaceLocationFlags OpenXRHandTrackingExtension::get_hand_joint_location_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  303. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, XrSpaceLocationFlags(0));
  304. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, XrSpaceLocationFlags(0));
  305. if (!hand_trackers[p_hand].is_initialized) {
  306. return XrSpaceLocationFlags(0);
  307. }
  308. const XrHandJointLocationEXT &location = hand_trackers[p_hand].joint_locations[p_joint];
  309. return location.locationFlags;
  310. }
  311. Quaternion OpenXRHandTrackingExtension::get_hand_joint_rotation(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  312. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Quaternion());
  313. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Quaternion());
  314. if (!hand_trackers[p_hand].is_initialized) {
  315. return Quaternion();
  316. }
  317. const XrHandJointLocationEXT &location = hand_trackers[p_hand].joint_locations[p_joint];
  318. return Quaternion(location.pose.orientation.x, location.pose.orientation.y, location.pose.orientation.z, location.pose.orientation.w);
  319. }
  320. Vector3 OpenXRHandTrackingExtension::get_hand_joint_position(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  321. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Vector3());
  322. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Vector3());
  323. if (!hand_trackers[p_hand].is_initialized) {
  324. return Vector3();
  325. }
  326. const XrHandJointLocationEXT &location = hand_trackers[p_hand].joint_locations[p_joint];
  327. return Vector3(location.pose.position.x, location.pose.position.y, location.pose.position.z);
  328. }
  329. float OpenXRHandTrackingExtension::get_hand_joint_radius(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  330. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, 0.0);
  331. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, 0.0);
  332. if (!hand_trackers[p_hand].is_initialized) {
  333. return 0.0;
  334. }
  335. return hand_trackers[p_hand].joint_locations[p_joint].radius;
  336. }
  337. XrSpaceVelocityFlags OpenXRHandTrackingExtension::get_hand_joint_velocity_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  338. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, XrSpaceVelocityFlags(0));
  339. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, XrSpaceVelocityFlags(0));
  340. if (!hand_trackers[p_hand].is_initialized) {
  341. return XrSpaceVelocityFlags(0);
  342. }
  343. const XrHandJointVelocityEXT &velocity = hand_trackers[p_hand].joint_velocities[p_joint];
  344. return velocity.velocityFlags;
  345. }
  346. Vector3 OpenXRHandTrackingExtension::get_hand_joint_linear_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  347. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Vector3());
  348. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Vector3());
  349. if (!hand_trackers[p_hand].is_initialized) {
  350. return Vector3();
  351. }
  352. const XrHandJointVelocityEXT &velocity = hand_trackers[p_hand].joint_velocities[p_joint];
  353. return Vector3(velocity.linearVelocity.x, velocity.linearVelocity.y, velocity.linearVelocity.z);
  354. }
  355. Vector3 OpenXRHandTrackingExtension::get_hand_joint_angular_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  356. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Vector3());
  357. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Vector3());
  358. if (!hand_trackers[p_hand].is_initialized) {
  359. return Vector3();
  360. }
  361. const XrHandJointVelocityEXT &velocity = hand_trackers[p_hand].joint_velocities[p_joint];
  362. return Vector3(velocity.angularVelocity.x, velocity.angularVelocity.y, velocity.angularVelocity.z);
  363. }