Skip to content

Commit 49b0f87

Browse files
authored
Merge pull request #90 from BastiaanOlij/sensors
Sensor example
2 parents 3616322 + 262faa2 commit 49b0f87

File tree

6 files changed

+860
-0
lines changed

6 files changed

+860
-0
lines changed

misc/sensors/cube_6.png

11 KB
Loading

misc/sensors/default_env.tres

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
[gd_resource type="Environment" load_steps=2 format=2]
2+
3+
[sub_resource type="ProceduralSky" id=1]
4+
5+
radiance_size = 4
6+
sky_top_color = Color( 0.0470588, 0.454902, 0.976471, 1 )
7+
sky_horizon_color = Color( 0.556863, 0.823529, 0.909804, 1 )
8+
sky_curve = 0.25
9+
sky_energy = 1.0
10+
ground_bottom_color = Color( 0.101961, 0.145098, 0.188235, 1 )
11+
ground_horizon_color = Color( 0.482353, 0.788235, 0.952941, 1 )
12+
ground_curve = 0.01
13+
ground_energy = 1.0
14+
sun_color = Color( 1, 1, 1, 1 )
15+
sun_latitude = 35.0
16+
sun_longitude = 0.0
17+
sun_angle_min = 1.0
18+
sun_angle_max = 100.0
19+
sun_curve = 0.05
20+
sun_energy = 16.0
21+
texture_size = 2
22+
23+
[resource]
24+
25+
background_mode = 2
26+
background_sky = SubResource( 1 )
27+
background_sky_custom_fov = 0.0
28+
background_color = Color( 0, 0, 0, 1 )
29+
background_energy = 1.0
30+
background_canvas_max_layer = 0
31+
background_camera_feed_id = 1
32+
background_camera_feed_h_flip = false
33+
background_camera_feed_v_flip = true
34+
ambient_light_color = Color( 0, 0, 0, 1 )
35+
ambient_light_energy = 1.0
36+
ambient_light_sky_contribution = 1.0
37+
fog_enabled = false
38+
fog_color = Color( 0.5, 0.6, 0.7, 1 )
39+
fog_sun_color = Color( 1, 0.9, 0.7, 1 )
40+
fog_sun_amount = 0.0
41+
fog_depth_enabled = true
42+
fog_depth_begin = 10.0
43+
fog_depth_curve = 1.0
44+
fog_transmit_enabled = false
45+
fog_transmit_curve = 1.0
46+
fog_height_enabled = false
47+
fog_height_min = 0.0
48+
fog_height_max = 100.0
49+
fog_height_curve = 1.0
50+
tonemap_mode = 0
51+
tonemap_exposure = 1.0
52+
tonemap_white = 1.0
53+
auto_exposure_enabled = false
54+
auto_exposure_scale = 0.4
55+
auto_exposure_min_luma = 0.05
56+
auto_exposure_max_luma = 8.0
57+
auto_exposure_speed = 0.5
58+
ss_reflections_enabled = false
59+
ss_reflections_max_steps = 64
60+
ss_reflections_fade_in = 0.15
61+
ss_reflections_fade_out = 2.0
62+
ss_reflections_depth_tolerance = 0.2
63+
ss_reflections_roughness = true
64+
ssao_enabled = false
65+
ssao_radius = 1.0
66+
ssao_intensity = 1.0
67+
ssao_radius2 = 0.0
68+
ssao_intensity2 = 1.0
69+
ssao_bias = 0.01
70+
ssao_light_affect = 0.0
71+
ssao_color = Color( 0, 0, 0, 1 )
72+
ssao_quality = 0
73+
ssao_blur = 3
74+
ssao_edge_sharpness = 4.0
75+
dof_blur_far_enabled = false
76+
dof_blur_far_distance = 10.0
77+
dof_blur_far_transition = 5.0
78+
dof_blur_far_amount = 0.1
79+
dof_blur_far_quality = 1
80+
dof_blur_near_enabled = false
81+
dof_blur_near_distance = 2.0
82+
dof_blur_near_transition = 1.0
83+
dof_blur_near_amount = 0.1
84+
dof_blur_near_quality = 1
85+
glow_enabled = false
86+
glow_levels/1 = false
87+
glow_levels/2 = false
88+
glow_levels/3 = true
89+
glow_levels/4 = false
90+
glow_levels/5 = true
91+
glow_levels/6 = false
92+
glow_levels/7 = false
93+
glow_intensity = 0.8
94+
glow_strength = 1.0
95+
glow_bloom = 0.0
96+
glow_blend_mode = 2
97+
glow_hdr_threshold = 1.0
98+
glow_hdr_scale = 2.0
99+
glow_bicubic_upscale = false
100+
adjustment_enabled = false
101+
adjustment_brightness = 1.0
102+
adjustment_contrast = 1.0
103+
adjustment_saturation = 1.0
104+

misc/sensors/icon.png

3.42 KB
Loading

misc/sensors/main.gd

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
extends Node
2+
3+
# Below are a number of helper functions that show how you can use the raw sensor data to determine the orientation
4+
# of your phone/device. The cheapest phones only have an accelerometer only the most expensive phones have all three.
5+
# Note that none of this logic filters data. Filters introduce lag but also provide stability. There are plenty
6+
# of examples on the internet on how to implement these. I wanted to keep this straight forward.
7+
8+
# We draw a few arrow objects to visualize the vectors and two cubes to show two implementation for orientating
9+
# these cubes to our phones orientation.
10+
# This is a 3D example however reading the phones orientation is also invaluable for 2D
11+
12+
# This function calculates a rotation matrix based on a direction vector. As our arrows are cylindrical we don't
13+
# care about the rotation around this axis.
14+
func get_basis_for_arrow(p_vector):
15+
var rotate = Basis()
16+
17+
# as our arrow points up, Y = our direction vector
18+
rotate.y = p_vector.normalized()
19+
20+
# get an arbitrary vector we can use to calculate our other two vectors
21+
var v = Vector3(1.0, 0.0, 0.0)
22+
if (abs(v.dot(rotate.y)) > 0.9):
23+
v = Vector3(0.0, 1.0, 0.0)
24+
25+
# use our vector to get a vector perpendicular to our two vectors
26+
rotate.x = rotate.y.cross(v).normalized()
27+
28+
# and the cross product again gives us our final vector perpendicular to our previous two vectors
29+
rotate.z = rotate.x.cross(rotate.y).normalized()
30+
31+
return rotate
32+
33+
# This function combines the magnetometer reading with the gravity vector to get a vector that points due north
34+
func calc_north(p_grav, p_mag):
35+
# Always use normalized vectors!
36+
p_grav = p_grav.normalized()
37+
38+
# Calculate east (or is it west) by getting our cross product.
39+
# The cross product of two normalized vectors returns a vector that
40+
# is perpendicular to our two vectors
41+
var east = p_grav.cross(p_mag.normalized()).normalized()
42+
43+
# Cross again to get our horizon aligned north
44+
return east.cross(p_grav).normalized()
45+
46+
# This function creates an orientation matrix using the magnetometer and gravity vector as inputs.
47+
func orientate_by_mag_and_grav(p_mag, p_grav):
48+
var rotate = Basis()
49+
50+
# as always, normalize!
51+
p_mag = p_mag.normalized()
52+
53+
# gravity points down, so - gravity points up!
54+
rotate.y = -p_grav.normalized()
55+
56+
# Cross products with our magnetic north gives an aligned east (or west, I always forget)
57+
rotate.x = rotate.y.cross(p_mag)
58+
59+
# And cross product again and we get our aligned north completing our matrix
60+
rotate.z = rotate.x.cross(rotate.y)
61+
62+
return rotate
63+
64+
# This function takes our gyro input and update an orientation matrix accordingly
65+
# The gyro is special as this vector does not contain a direction but rather a
66+
# rotational velocity. This is why we multiply our values with delta.
67+
func rotate_by_gyro(p_gyro, p_basis, p_delta):
68+
var rotate = Basis()
69+
70+
rotate = rotate.rotated(p_basis.x, -p_gyro.x * p_delta)
71+
rotate = rotate.rotated(p_basis.y, -p_gyro.y * p_delta)
72+
rotate = rotate.rotated(p_basis.z, -p_gyro.z * p_delta)
73+
74+
return rotate * p_basis
75+
76+
# This function corrects the drift in our matrix by our gravity vector
77+
func drift_correction(p_basis, p_grav):
78+
# as always, make sure our vector is normalized but also invert as our gravity points down
79+
var real_up = -p_grav.normalized()
80+
81+
# start by calculating the dot product, this gives us the cosine angle between our two vectors
82+
var dot = p_basis.y.dot(real_up)
83+
84+
# if our dot is 1.0 we're good
85+
if (dot < 1.0):
86+
# the cross between our two vectors gives us a vector perpendicular to our two vectors
87+
var axis = p_basis.y.cross(real_up).normalized()
88+
var correction = Basis(axis, acos(dot))
89+
p_basis = correction * p_basis
90+
91+
return p_basis
92+
93+
func _process(delta):
94+
# Get our data
95+
var acc = Input.get_accelerometer()
96+
var grav = Input.get_gravity()
97+
var mag = Input.get_magnetometer()
98+
var gyro = Input.get_gyroscope()
99+
100+
# Show our base values
101+
get_node("Control/Accelerometer").text = 'Accelerometer: ' + str(acc) + ', gravity: ' + str(grav)
102+
get_node("Control/Magnetometer").text = 'Magnetometer: ' + str(mag)
103+
get_node("Control/Gyroscope").text = 'Gyroscope: ' + str(gyro)
104+
105+
# Check if we have all needed data
106+
if grav.length() < 0.1:
107+
if acc.length() < 0.1:
108+
# we don't have either...
109+
grav = Vector3(0.0, -1.0, 0.0)
110+
else:
111+
# The gravity vector is calculated by the OS by combining the other sensor inputs.
112+
# If we don't have a gravity vector, from now on, use accelerometer...
113+
grav = acc
114+
115+
if mag.length() < 0.1:
116+
mag = Vector3(1.0, 0.0, 0.0)
117+
118+
# Update our arrow showing gravity
119+
get_node("Arrows/AccelerometerArrow").transform.basis = get_basis_for_arrow(grav)
120+
121+
# Update our arrow showing our magnetometer
122+
# Note that in absense of other strong magnetic forces this will point to magnetic north, which is not horizontal thanks to the earth being, uhm, round
123+
get_node("Arrows/MagnetoArrow").transform.basis = get_basis_for_arrow(mag)
124+
125+
# Calculate our north vector and show that
126+
var north = calc_north(grav,mag)
127+
get_node("Arrows/NorthArrow").transform.basis = get_basis_for_arrow(north)
128+
129+
# Combine our magnetometer and gravity vector to position our box. This will be fairly accurate
130+
# but our magnetometer can be easily influenced by magnets. Cheaper phones often don't have gyros
131+
# so it is a good backup.
132+
var mag_and_grav = get_node("Boxes/MagAndGrav")
133+
mag_and_grav.transform.basis = orientate_by_mag_and_grav(mag, grav).orthonormalized()
134+
135+
# Using our gyro and do a drift correction using our gravity vector gives the best result
136+
var gyro_and_grav = get_node("Boxes/GyroAndGrav")
137+
var new_basis = rotate_by_gyro(gyro, gyro_and_grav.transform.basis, delta).orthonormalized()
138+
gyro_and_grav.transform.basis = drift_correction(new_basis, grav)
139+
140+
141+

0 commit comments

Comments
 (0)