2017-04-23 14:10:41 +02:00
|
|
|
/*************************************************************************/
|
2020-04-08 16:47:36 +02:00
|
|
|
/* xr_positional_tracker.cpp */
|
2017-04-23 14:10:41 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2017-04-23 14:10:41 +02:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-04-23 14:10:41 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
#include "xr_positional_tracker.h"
|
2020-04-28 15:19:37 +02:00
|
|
|
|
|
|
|
#include "core/input/input.h"
|
2017-04-23 14:10:41 +02:00
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
void XRPositionalTracker::_bind_methods() {
|
2017-09-10 08:15:11 +02:00
|
|
|
BIND_ENUM_CONSTANT(TRACKER_HAND_UNKNOWN);
|
|
|
|
BIND_ENUM_CONSTANT(TRACKER_LEFT_HAND);
|
|
|
|
BIND_ENUM_CONSTANT(TRACKER_RIGHT_HAND);
|
|
|
|
|
2017-04-23 14:10:41 +02:00
|
|
|
// this class is read only from GDScript, so we only have access to getters..
|
2020-04-15 21:22:14 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_tracker_type"), &XRPositionalTracker::get_tracker_type);
|
2020-04-08 16:47:36 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_tracker_id"), &XRPositionalTracker::get_tracker_id);
|
2020-04-15 21:22:14 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_tracker_name"), &XRPositionalTracker::get_tracker_name);
|
2020-04-08 16:47:36 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_joy_id"), &XRPositionalTracker::get_joy_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_tracks_orientation"), &XRPositionalTracker::get_tracks_orientation);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_orientation"), &XRPositionalTracker::get_orientation);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_tracks_position"), &XRPositionalTracker::get_tracks_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_position"), &XRPositionalTracker::get_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_hand"), &XRPositionalTracker::get_hand);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_transform", "adjust_by_reference_frame"), &XRPositionalTracker::get_transform);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mesh"), &XRPositionalTracker::get_mesh);
|
2017-08-03 10:58:05 +02:00
|
|
|
|
|
|
|
// these functions we don't want to expose to normal users but do need to be callable from GDNative
|
2020-04-08 16:47:36 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_type", "type"), &XRPositionalTracker::set_type);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_name", "name"), &XRPositionalTracker::set_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_joy_id", "joy_id"), &XRPositionalTracker::set_joy_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_orientation", "orientation"), &XRPositionalTracker::set_orientation);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_rw_position", "rw_position"), &XRPositionalTracker::set_rw_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("_set_mesh", "mesh"), &XRPositionalTracker::set_mesh);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_rumble"), &XRPositionalTracker::get_rumble);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_rumble", "rumble"), &XRPositionalTracker::set_rumble);
|
2017-11-01 11:46:37 +01:00
|
|
|
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rumble"), "set_rumble", "get_rumble");
|
2017-04-23 14:10:41 +02:00
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
void XRPositionalTracker::set_type(XRServer::TrackerType p_type) {
|
2017-04-23 14:10:41 +02:00
|
|
|
if (type != p_type) {
|
|
|
|
type = p_type;
|
2020-04-08 16:47:36 +02:00
|
|
|
hand = XRPositionalTracker::TRACKER_HAND_UNKNOWN;
|
2017-04-23 14:10:41 +02:00
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
2017-04-23 14:10:41 +02:00
|
|
|
|
|
|
|
// get a tracker id for our type
|
2017-12-21 13:10:44 +01:00
|
|
|
// note if this is a controller this will be 3 or higher but we may change it later.
|
2020-04-08 16:47:36 +02:00
|
|
|
tracker_id = xr_server->get_free_tracker_id_for_type(p_type);
|
2017-12-21 13:10:44 +01:00
|
|
|
};
|
2017-04-23 14:10:41 +02:00
|
|
|
};
|
|
|
|
|
2020-04-15 21:22:14 +02:00
|
|
|
XRServer::TrackerType XRPositionalTracker::get_tracker_type() const {
|
2017-04-23 14:10:41 +02:00
|
|
|
return type;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
void XRPositionalTracker::set_name(const String &p_name) {
|
2017-04-23 14:10:41 +02:00
|
|
|
name = p_name;
|
|
|
|
};
|
|
|
|
|
2020-04-15 21:22:14 +02:00
|
|
|
StringName XRPositionalTracker::get_tracker_name() const {
|
2017-04-23 14:10:41 +02:00
|
|
|
return name;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
int XRPositionalTracker::get_tracker_id() const {
|
2017-04-23 14:10:41 +02:00
|
|
|
return tracker_id;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
void XRPositionalTracker::set_joy_id(int p_joy_id) {
|
2017-04-23 14:10:41 +02:00
|
|
|
joy_id = p_joy_id;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
int XRPositionalTracker::get_joy_id() const {
|
2017-04-23 14:10:41 +02:00
|
|
|
return joy_id;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
bool XRPositionalTracker::get_tracks_orientation() const {
|
2017-04-23 14:10:41 +02:00
|
|
|
return tracks_orientation;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
void XRPositionalTracker::set_orientation(const Basis &p_orientation) {
|
2017-04-23 14:10:41 +02:00
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
|
|
|
tracks_orientation = true; // obviously we have this
|
|
|
|
orientation = p_orientation;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
Basis XRPositionalTracker::get_orientation() const {
|
2017-04-23 14:10:41 +02:00
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
|
|
|
return orientation;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
bool XRPositionalTracker::get_tracks_position() const {
|
2017-04-23 14:10:41 +02:00
|
|
|
return tracks_position;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
void XRPositionalTracker::set_position(const Vector3 &p_position) {
|
2017-04-23 14:10:41 +02:00
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
|
|
|
real_t world_scale = xr_server->get_world_scale();
|
2017-08-03 10:58:05 +02:00
|
|
|
ERR_FAIL_COND(world_scale == 0);
|
|
|
|
|
2017-04-23 14:10:41 +02:00
|
|
|
tracks_position = true; // obviously we have this
|
2017-08-03 10:58:05 +02:00
|
|
|
rw_position = p_position / world_scale;
|
2017-04-23 14:10:41 +02:00
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
Vector3 XRPositionalTracker::get_position() const {
|
2017-04-23 14:10:41 +02:00
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, rw_position);
|
|
|
|
real_t world_scale = xr_server->get_world_scale();
|
2017-08-03 10:58:05 +02:00
|
|
|
|
|
|
|
return rw_position * world_scale;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
void XRPositionalTracker::set_rw_position(const Vector3 &p_rw_position) {
|
2017-08-03 10:58:05 +02:00
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
|
|
|
tracks_position = true; // obviously we have this
|
|
|
|
rw_position = p_rw_position;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
Vector3 XRPositionalTracker::get_rw_position() const {
|
2017-08-03 10:58:05 +02:00
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
|
|
|
return rw_position;
|
2017-04-23 14:10:41 +02:00
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
void XRPositionalTracker::set_mesh(const Ref<Mesh> &p_mesh) {
|
2019-02-05 11:02:13 +01:00
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
|
|
|
mesh = p_mesh;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
Ref<Mesh> XRPositionalTracker::get_mesh() const {
|
2019-02-05 11:02:13 +01:00
|
|
|
_THREAD_SAFE_METHOD_
|
|
|
|
|
|
|
|
return mesh;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
XRPositionalTracker::TrackerHand XRPositionalTracker::get_hand() const {
|
2017-09-10 08:15:11 +02:00
|
|
|
return hand;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
void XRPositionalTracker::set_hand(const XRPositionalTracker::TrackerHand p_hand) {
|
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL(xr_server);
|
2017-12-21 13:10:44 +01:00
|
|
|
|
|
|
|
if (hand != p_hand) {
|
|
|
|
// we can only set this if we've previously set this to be a controller!!
|
2020-04-08 16:47:36 +02:00
|
|
|
ERR_FAIL_COND((type != XRServer::TRACKER_CONTROLLER) && (p_hand != XRPositionalTracker::TRACKER_HAND_UNKNOWN));
|
2017-12-21 13:10:44 +01:00
|
|
|
|
|
|
|
hand = p_hand;
|
2020-04-08 16:47:36 +02:00
|
|
|
if (hand == XRPositionalTracker::TRACKER_LEFT_HAND) {
|
|
|
|
if (!xr_server->is_tracker_id_in_use_for_type(type, 1)) {
|
2017-12-21 13:10:44 +01:00
|
|
|
tracker_id = 1;
|
|
|
|
};
|
2020-04-08 16:47:36 +02:00
|
|
|
} else if (hand == XRPositionalTracker::TRACKER_RIGHT_HAND) {
|
|
|
|
if (!xr_server->is_tracker_id_in_use_for_type(type, 2)) {
|
2017-12-21 13:10:44 +01:00
|
|
|
tracker_id = 2;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2017-09-10 08:15:11 +02:00
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
Transform XRPositionalTracker::get_transform(bool p_adjust_by_reference_frame) const {
|
2017-04-23 14:10:41 +02:00
|
|
|
Transform new_transform;
|
|
|
|
|
|
|
|
new_transform.basis = get_orientation();
|
|
|
|
new_transform.origin = get_position();
|
|
|
|
|
|
|
|
if (p_adjust_by_reference_frame) {
|
2020-04-08 16:47:36 +02:00
|
|
|
XRServer *xr_server = XRServer::get_singleton();
|
|
|
|
ERR_FAIL_NULL_V(xr_server, new_transform);
|
2017-04-23 14:10:41 +02:00
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
new_transform = xr_server->get_reference_frame() * new_transform;
|
2017-04-23 14:10:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return new_transform;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
real_t XRPositionalTracker::get_rumble() const {
|
2017-11-01 11:46:37 +01:00
|
|
|
return rumble;
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
void XRPositionalTracker::set_rumble(real_t p_rumble) {
|
2017-11-01 11:46:37 +01:00
|
|
|
if (p_rumble > 0.0) {
|
|
|
|
rumble = p_rumble;
|
|
|
|
} else {
|
|
|
|
rumble = 0.0;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:47:36 +02:00
|
|
|
XRPositionalTracker::XRPositionalTracker() {
|
|
|
|
type = XRServer::TRACKER_UNKNOWN;
|
2017-04-23 14:10:41 +02:00
|
|
|
name = "Unknown";
|
|
|
|
joy_id = -1;
|
|
|
|
tracker_id = 0;
|
|
|
|
tracks_orientation = false;
|
|
|
|
tracks_position = false;
|
2017-09-10 08:15:11 +02:00
|
|
|
hand = TRACKER_HAND_UNKNOWN;
|
2017-11-01 11:46:37 +01:00
|
|
|
rumble = 0.0;
|
2017-04-23 14:10:41 +02:00
|
|
|
};
|