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-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #include "arvr_positional_tracker.h"
32 #include "core/os/input.h"
33 
_bind_methods()34 void ARVRPositionalTracker::_bind_methods() {
35 	BIND_ENUM_CONSTANT(TRACKER_HAND_UNKNOWN);
36 	BIND_ENUM_CONSTANT(TRACKER_LEFT_HAND);
37 	BIND_ENUM_CONSTANT(TRACKER_RIGHT_HAND);
38 
39 	// this class is read only from GDScript, so we only have access to getters..
40 	ClassDB::bind_method(D_METHOD("get_type"), &ARVRPositionalTracker::get_type);
41 	ClassDB::bind_method(D_METHOD("get_tracker_id"), &ARVRPositionalTracker::get_tracker_id);
42 	ClassDB::bind_method(D_METHOD("get_name"), &ARVRPositionalTracker::get_name);
43 	ClassDB::bind_method(D_METHOD("get_joy_id"), &ARVRPositionalTracker::get_joy_id);
44 	ClassDB::bind_method(D_METHOD("get_tracks_orientation"), &ARVRPositionalTracker::get_tracks_orientation);
45 	ClassDB::bind_method(D_METHOD("get_orientation"), &ARVRPositionalTracker::get_orientation);
46 	ClassDB::bind_method(D_METHOD("get_tracks_position"), &ARVRPositionalTracker::get_tracks_position);
47 	ClassDB::bind_method(D_METHOD("get_position"), &ARVRPositionalTracker::get_position);
48 	ClassDB::bind_method(D_METHOD("get_hand"), &ARVRPositionalTracker::get_hand);
49 	ClassDB::bind_method(D_METHOD("get_transform", "adjust_by_reference_frame"), &ARVRPositionalTracker::get_transform);
50 	ClassDB::bind_method(D_METHOD("get_mesh"), &ARVRPositionalTracker::get_mesh);
51 
52 	// these functions we don't want to expose to normal users but do need to be callable from GDNative
53 	ClassDB::bind_method(D_METHOD("_set_type", "type"), &ARVRPositionalTracker::set_type);
54 	ClassDB::bind_method(D_METHOD("_set_name", "name"), &ARVRPositionalTracker::set_name);
55 	ClassDB::bind_method(D_METHOD("_set_joy_id", "joy_id"), &ARVRPositionalTracker::set_joy_id);
56 	ClassDB::bind_method(D_METHOD("_set_orientation", "orientation"), &ARVRPositionalTracker::set_orientation);
57 	ClassDB::bind_method(D_METHOD("_set_rw_position", "rw_position"), &ARVRPositionalTracker::set_rw_position);
58 	ClassDB::bind_method(D_METHOD("_set_mesh", "mesh"), &ARVRPositionalTracker::set_mesh);
59 	ClassDB::bind_method(D_METHOD("get_rumble"), &ARVRPositionalTracker::get_rumble);
60 	ClassDB::bind_method(D_METHOD("set_rumble", "rumble"), &ARVRPositionalTracker::set_rumble);
61 
62 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "rumble"), "set_rumble", "get_rumble");
63 };
64 
set_type(ARVRServer::TrackerType p_type)65 void ARVRPositionalTracker::set_type(ARVRServer::TrackerType p_type) {
66 	if (type != p_type) {
67 		type = p_type;
68 		hand = ARVRPositionalTracker::TRACKER_HAND_UNKNOWN;
69 
70 		ARVRServer *arvr_server = ARVRServer::get_singleton();
71 		ERR_FAIL_NULL(arvr_server);
72 
73 		// get a tracker id for our type
74 		// note if this is a controller this will be 3 or higher but we may change it later.
75 		tracker_id = arvr_server->get_free_tracker_id_for_type(p_type);
76 	};
77 };
78 
get_type() const79 ARVRServer::TrackerType ARVRPositionalTracker::get_type() const {
80 	return type;
81 };
82 
set_name(const String & p_name)83 void ARVRPositionalTracker::set_name(const String &p_name) {
84 	name = p_name;
85 };
86 
get_name() const87 StringName ARVRPositionalTracker::get_name() const {
88 	return name;
89 };
90 
get_tracker_id() const91 int ARVRPositionalTracker::get_tracker_id() const {
92 	return tracker_id;
93 };
94 
set_joy_id(int p_joy_id)95 void ARVRPositionalTracker::set_joy_id(int p_joy_id) {
96 	joy_id = p_joy_id;
97 };
98 
get_joy_id() const99 int ARVRPositionalTracker::get_joy_id() const {
100 	return joy_id;
101 };
102 
get_tracks_orientation() const103 bool ARVRPositionalTracker::get_tracks_orientation() const {
104 	return tracks_orientation;
105 };
106 
set_orientation(const Basis & p_orientation)107 void ARVRPositionalTracker::set_orientation(const Basis &p_orientation) {
108 	_THREAD_SAFE_METHOD_
109 
110 	tracks_orientation = true; // obviously we have this
111 	orientation = p_orientation;
112 };
113 
get_orientation() const114 Basis ARVRPositionalTracker::get_orientation() const {
115 	_THREAD_SAFE_METHOD_
116 
117 	return orientation;
118 };
119 
get_tracks_position() const120 bool ARVRPositionalTracker::get_tracks_position() const {
121 	return tracks_position;
122 };
123 
set_position(const Vector3 & p_position)124 void ARVRPositionalTracker::set_position(const Vector3 &p_position) {
125 	_THREAD_SAFE_METHOD_
126 
127 	ARVRServer *arvr_server = ARVRServer::get_singleton();
128 	ERR_FAIL_NULL(arvr_server);
129 	real_t world_scale = arvr_server->get_world_scale();
130 	ERR_FAIL_COND(world_scale == 0);
131 
132 	tracks_position = true; // obviously we have this
133 	rw_position = p_position / world_scale;
134 };
135 
get_position() const136 Vector3 ARVRPositionalTracker::get_position() const {
137 	_THREAD_SAFE_METHOD_
138 
139 	ARVRServer *arvr_server = ARVRServer::get_singleton();
140 	ERR_FAIL_NULL_V(arvr_server, rw_position);
141 	real_t world_scale = arvr_server->get_world_scale();
142 
143 	return rw_position * world_scale;
144 };
145 
set_rw_position(const Vector3 & p_rw_position)146 void ARVRPositionalTracker::set_rw_position(const Vector3 &p_rw_position) {
147 	_THREAD_SAFE_METHOD_
148 
149 	tracks_position = true; // obviously we have this
150 	rw_position = p_rw_position;
151 };
152 
get_rw_position() const153 Vector3 ARVRPositionalTracker::get_rw_position() const {
154 	_THREAD_SAFE_METHOD_
155 
156 	return rw_position;
157 };
158 
set_mesh(const Ref<Mesh> & p_mesh)159 void ARVRPositionalTracker::set_mesh(const Ref<Mesh> &p_mesh) {
160 	_THREAD_SAFE_METHOD_
161 
162 	mesh = p_mesh;
163 };
164 
get_mesh() const165 Ref<Mesh> ARVRPositionalTracker::get_mesh() const {
166 	_THREAD_SAFE_METHOD_
167 
168 	return mesh;
169 };
170 
get_hand() const171 ARVRPositionalTracker::TrackerHand ARVRPositionalTracker::get_hand() const {
172 	return hand;
173 };
174 
set_hand(const ARVRPositionalTracker::TrackerHand p_hand)175 void ARVRPositionalTracker::set_hand(const ARVRPositionalTracker::TrackerHand p_hand) {
176 	ARVRServer *arvr_server = ARVRServer::get_singleton();
177 	ERR_FAIL_NULL(arvr_server);
178 
179 	if (hand != p_hand) {
180 		// we can only set this if we've previously set this to be a controller!!
181 		ERR_FAIL_COND((type != ARVRServer::TRACKER_CONTROLLER) && (p_hand != ARVRPositionalTracker::TRACKER_HAND_UNKNOWN));
182 
183 		hand = p_hand;
184 		if (hand == ARVRPositionalTracker::TRACKER_LEFT_HAND) {
185 			if (!arvr_server->is_tracker_id_in_use_for_type(type, 1)) {
186 				tracker_id = 1;
187 			};
188 		} else if (hand == ARVRPositionalTracker::TRACKER_RIGHT_HAND) {
189 			if (!arvr_server->is_tracker_id_in_use_for_type(type, 2)) {
190 				tracker_id = 2;
191 			};
192 		};
193 	};
194 };
195 
get_transform(bool p_adjust_by_reference_frame) const196 Transform ARVRPositionalTracker::get_transform(bool p_adjust_by_reference_frame) const {
197 	Transform new_transform;
198 
199 	new_transform.basis = get_orientation();
200 	new_transform.origin = get_position();
201 
202 	if (p_adjust_by_reference_frame) {
203 		ARVRServer *arvr_server = ARVRServer::get_singleton();
204 		ERR_FAIL_NULL_V(arvr_server, new_transform);
205 
206 		new_transform = arvr_server->get_reference_frame() * new_transform;
207 	};
208 
209 	return new_transform;
210 };
211 
get_rumble() const212 real_t ARVRPositionalTracker::get_rumble() const {
213 	return rumble;
214 };
215 
set_rumble(real_t p_rumble)216 void ARVRPositionalTracker::set_rumble(real_t p_rumble) {
217 	if (p_rumble > 0.0) {
218 		rumble = p_rumble;
219 	} else {
220 		rumble = 0.0;
221 	};
222 };
223 
ARVRPositionalTracker()224 ARVRPositionalTracker::ARVRPositionalTracker() {
225 	type = ARVRServer::TRACKER_UNKNOWN;
226 	name = "Unknown";
227 	joy_id = -1;
228 	tracker_id = 0;
229 	tracks_orientation = false;
230 	tracks_position = false;
231 	hand = TRACKER_HAND_UNKNOWN;
232 	rumble = 0.0;
233 };
234 
~ARVRPositionalTracker()235 ARVRPositionalTracker::~ARVRPositionalTracker(){
236 
237 };
238