main.gd 5.7 KB

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