mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-04 15:00:09 +01:00
Format files using updated file_format.sh
This commit is contained in:
@@ -14,7 +14,7 @@ func _unhandled_input(event):
|
||||
else: # Up.
|
||||
state.erase(event.index)
|
||||
get_tree().set_input_as_handled()
|
||||
|
||||
|
||||
elif event is InputEventScreenDrag: # Movement.
|
||||
state[event.index] = event.position
|
||||
get_tree().set_input_as_handled()
|
||||
|
||||
@@ -13,52 +13,52 @@ extends Node
|
||||
# care about the rotation around this axis.
|
||||
func get_basis_for_arrow(p_vector):
|
||||
var rotate = Basis()
|
||||
|
||||
|
||||
# as our arrow points up, Y = our direction vector
|
||||
rotate.y = p_vector.normalized()
|
||||
|
||||
|
||||
# get an arbitrary vector we can use to calculate our other two vectors
|
||||
var v = Vector3(1.0, 0.0, 0.0)
|
||||
if abs(v.dot(rotate.y)) > 0.9:
|
||||
v = Vector3(0.0, 1.0, 0.0)
|
||||
|
||||
|
||||
# use our vector to get a vector perpendicular to our two vectors
|
||||
rotate.x = rotate.y.cross(v).normalized()
|
||||
|
||||
|
||||
# and the cross product again gives us our final vector perpendicular to our previous two vectors
|
||||
rotate.z = rotate.x.cross(rotate.y).normalized()
|
||||
|
||||
|
||||
return rotate
|
||||
|
||||
# This function combines the magnetometer reading with the gravity vector to get a vector that points due north
|
||||
func calc_north(p_grav, p_mag):
|
||||
# Always use normalized vectors!
|
||||
p_grav = p_grav.normalized()
|
||||
|
||||
|
||||
# Calculate east (or is it west) by getting our cross product.
|
||||
# The cross product of two normalized vectors returns a vector that
|
||||
# is perpendicular to our two vectors
|
||||
var east = p_grav.cross(p_mag.normalized()).normalized()
|
||||
|
||||
|
||||
# Cross again to get our horizon aligned north
|
||||
return east.cross(p_grav).normalized()
|
||||
|
||||
# This function creates an orientation matrix using the magnetometer and gravity vector as inputs.
|
||||
func orientate_by_mag_and_grav(p_mag, p_grav):
|
||||
var rotate = Basis()
|
||||
|
||||
|
||||
# as always, normalize!
|
||||
p_mag = p_mag.normalized()
|
||||
|
||||
|
||||
# gravity points down, so - gravity points up!
|
||||
rotate.y = -p_grav.normalized()
|
||||
|
||||
|
||||
# Cross products with our magnetic north gives an aligned east (or west, I always forget)
|
||||
rotate.x = rotate.y.cross(p_mag)
|
||||
|
||||
|
||||
# And cross product again and we get our aligned north completing our matrix
|
||||
rotate.z = rotate.x.cross(rotate.y)
|
||||
|
||||
|
||||
return rotate
|
||||
|
||||
# This function takes our gyro input and update an orientation matrix accordingly
|
||||
@@ -66,28 +66,28 @@ func orientate_by_mag_and_grav(p_mag, p_grav):
|
||||
# rotational velocity. This is why we multiply our values with delta.
|
||||
func rotate_by_gyro(p_gyro, p_basis, p_delta):
|
||||
var rotate = Basis()
|
||||
|
||||
|
||||
rotate = rotate.rotated(p_basis.x, -p_gyro.x * p_delta)
|
||||
rotate = rotate.rotated(p_basis.y, -p_gyro.y * p_delta)
|
||||
rotate = rotate.rotated(p_basis.z, -p_gyro.z * p_delta)
|
||||
|
||||
|
||||
return rotate * p_basis
|
||||
|
||||
# This function corrects the drift in our matrix by our gravity vector
|
||||
func drift_correction(p_basis, p_grav):
|
||||
# as always, make sure our vector is normalized but also invert as our gravity points down
|
||||
var real_up = -p_grav.normalized()
|
||||
|
||||
|
||||
# start by calculating the dot product, this gives us the cosine angle between our two vectors
|
||||
var dot = p_basis.y.dot(real_up)
|
||||
|
||||
|
||||
# if our dot is 1.0 we're good
|
||||
if dot < 1.0:
|
||||
# the cross between our two vectors gives us a vector perpendicular to our two vectors
|
||||
var axis = p_basis.y.cross(real_up).normalized()
|
||||
var correction = Basis(axis, acos(dot))
|
||||
p_basis = correction * p_basis
|
||||
|
||||
|
||||
return p_basis
|
||||
|
||||
func _process(delta):
|
||||
@@ -96,12 +96,12 @@ func _process(delta):
|
||||
var grav = Input.get_gravity()
|
||||
var mag = Input.get_magnetometer()
|
||||
var gyro = Input.get_gyroscope()
|
||||
|
||||
|
||||
# Show our base values
|
||||
get_node("Control/Accelerometer").text = "Accelerometer: " + str(acc) + ", gravity: " + str(grav)
|
||||
get_node("Control/Magnetometer").text = "Magnetometer: " + str(mag)
|
||||
get_node("Control/Gyroscope").text = "Gyroscope: " + str(gyro)
|
||||
|
||||
|
||||
# Check if we have all needed data
|
||||
if grav.length() < 0.1:
|
||||
if acc.length() < 0.1:
|
||||
@@ -111,27 +111,27 @@ func _process(delta):
|
||||
# The gravity vector is calculated by the OS by combining the other sensor inputs.
|
||||
# If we don't have a gravity vector, from now on, use accelerometer...
|
||||
grav = acc
|
||||
|
||||
|
||||
if mag.length() < 0.1:
|
||||
mag = Vector3(1.0, 0.0, 0.0)
|
||||
|
||||
|
||||
# Update our arrow showing gravity
|
||||
get_node("Arrows/AccelerometerArrow").transform.basis = get_basis_for_arrow(grav)
|
||||
|
||||
|
||||
# Update our arrow showing our magnetometer
|
||||
# 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
|
||||
get_node("Arrows/MagnetoArrow").transform.basis = get_basis_for_arrow(mag)
|
||||
|
||||
|
||||
# Calculate our north vector and show that
|
||||
var north = calc_north(grav,mag)
|
||||
get_node("Arrows/NorthArrow").transform.basis = get_basis_for_arrow(north)
|
||||
|
||||
|
||||
# Combine our magnetometer and gravity vector to position our box. This will be fairly accurate
|
||||
# but our magnetometer can be easily influenced by magnets. Cheaper phones often don't have gyros
|
||||
# so it is a good backup.
|
||||
var mag_and_grav = get_node("Boxes/MagAndGrav")
|
||||
mag_and_grav.transform.basis = orientate_by_mag_and_grav(mag, grav).orthonormalized()
|
||||
|
||||
|
||||
# Using our gyro and do a drift correction using our gravity vector gives the best result
|
||||
var gyro_and_grav = get_node("Boxes/GyroAndGrav")
|
||||
var new_basis = rotate_by_gyro(gyro, gyro_and_grav.transform.basis, delta).orthonormalized()
|
||||
|
||||
Reference in New Issue
Block a user