-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add joypad motion sensors tutorial #11692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
Thank you for your review, AThousandShips! I will be able to fix the text in the PR soon! |
f2f997c to
0d68400
Compare
Co-Authored-By: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>
0d68400 to
8f5a6f0
Compare
|
Might be a bit before this gets merged since it's for 4.7 |
| That's because modern gyroscopes often need calibration. This is like how a weighing scale can need calibration to tell it what 'zero' is. | ||
| Like a weighing scale, a correctly calibrated gyroscope will give an accurate reading. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| That's because modern gyroscopes often need calibration. This is like how a weighing scale can need calibration to tell it what 'zero' is. | |
| Like a weighing scale, a correctly calibrated gyroscope will give an accurate reading. | |
| That's because modern gyroscopes often need calibration. This is like how a weighing scale can need calibration to tell it what "zero" is. | |
| Like a weighing scale, only a correctly calibrated gyroscope will give an accurate reading. |
| **Accelerometer** is a type of sensor that detects a device's acceleration in m/s², | ||
| which, in simpler terms, means it can detect if the device (in our case, a controller) has been physically moved quickly. | ||
| For example, it can detect if the player quickly raises their controller, quickly moves it to the side, | ||
| or if the player is shaking the controller. | ||
| Note that, unfortunately, it does not **not** mean that the device will be able to detect precise movements that, | ||
| for example, can be used to replicate a VR controller's movement. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| **Accelerometer** is a type of sensor that detects a device's acceleration in m/s², | |
| which, in simpler terms, means it can detect if the device (in our case, a controller) has been physically moved quickly. | |
| For example, it can detect if the player quickly raises their controller, quickly moves it to the side, | |
| or if the player is shaking the controller. | |
| Note that, unfortunately, it does not **not** mean that the device will be able to detect precise movements that, | |
| for example, can be used to replicate a VR controller's movement. | |
| **Accelerometer** is a type of sensor that detects a device's acceleration in m/s². | |
| In simpler terms, this means it can detect if the device (in our case, a controller) has been physically moved quickly. | |
| For example, it can detect if the player quickly raises their controller, quickly moves it to the side, | |
| or if the player is shaking the controller. | |
| Note that, unfortunately, it does not **not** mean that the device will be able to detect precise movements that can be used to replicate a VR controller's movement (for example). |
| The following example prints the controller movement when it's being quickly moved by using its accelerometer: | ||
| (If you feel like this code is too sensitive or not sensitive enough for your controller's physical movement, | ||
| you can also tweak the values used here however you feel like!) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| The following example prints the controller movement when it's being quickly moved by using its accelerometer: | |
| (If you feel like this code is too sensitive or not sensitive enough for your controller's physical movement, | |
| you can also tweak the values used here however you feel like!) | |
| The following example prints the controller movement when it's being quickly moved by using its accelerometer. | |
| If you feel like this code is too sensitive or not sensitive enough for your controller's physical movement, | |
| you can also tweak the values used here however you feel like. |
| var detect_accelerometer = true | ||
| func _ready(): | ||
| # In this example we only use the first connected joypad (id 0). | ||
| if 0 not in Input.get_connected_joypads(): | ||
| return | ||
| if not Input.has_joy_motion_sensors(0): | ||
| return | ||
| # We must enable the motion sensors before using them. | ||
| Input.set_joy_motion_sensors_enabled(0, true) | ||
| func _process(delta): | ||
| if Input.has_joy_motion_sensors(0): | ||
| accelerometer_example() | ||
| func accelerometer_example(): | ||
| if not detect_accelerometer: | ||
| return | ||
| var acceleration = Input.get_joy_accelerometer(0) - Input.get_joy_gravity(0) | ||
| if acceleration.length() > 10: | ||
| if acceleration.x > 10: | ||
| print("Moved left") | ||
| elif acceleration.x < -10: | ||
| print("Moved right") | ||
| if acceleration.y < -10: | ||
| print("Moved up") | ||
| elif acceleration.y > 10: | ||
| print("Moved down") | ||
| # After detecting movement in one direction, the accelerometer sensor | ||
| # will briefly report movement in the opposite direction, even though the controller only moved once. | ||
| # So we need to ignore these reported values for a short amount of time. | ||
| detect_accelerometer = false | ||
| await get_tree().create_timer(0.5, false).timeout | ||
| detect_accelerometer = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use spaces for indentation in docs:
| var detect_accelerometer = true | |
| func _ready(): | |
| # In this example we only use the first connected joypad (id 0). | |
| if 0 not in Input.get_connected_joypads(): | |
| return | |
| if not Input.has_joy_motion_sensors(0): | |
| return | |
| # We must enable the motion sensors before using them. | |
| Input.set_joy_motion_sensors_enabled(0, true) | |
| func _process(delta): | |
| if Input.has_joy_motion_sensors(0): | |
| accelerometer_example() | |
| func accelerometer_example(): | |
| if not detect_accelerometer: | |
| return | |
| var acceleration = Input.get_joy_accelerometer(0) - Input.get_joy_gravity(0) | |
| if acceleration.length() > 10: | |
| if acceleration.x > 10: | |
| print("Moved left") | |
| elif acceleration.x < -10: | |
| print("Moved right") | |
| if acceleration.y < -10: | |
| print("Moved up") | |
| elif acceleration.y > 10: | |
| print("Moved down") | |
| # After detecting movement in one direction, the accelerometer sensor | |
| # will briefly report movement in the opposite direction, even though the controller only moved once. | |
| # So we need to ignore these reported values for a short amount of time. | |
| detect_accelerometer = false | |
| await get_tree().create_timer(0.5, false).timeout | |
| detect_accelerometer = true | |
| var detect_accelerometer = true | |
| func _ready(): | |
| # In this example we only use the first connected joypad (id 0). | |
| if 0 not in Input.get_connected_joypads(): | |
| return | |
| if not Input.has_joy_motion_sensors(0): | |
| return | |
| # We must enable the motion sensors before using them. | |
| Input.set_joy_motion_sensors_enabled(0, true) | |
| func _process(delta): | |
| if Input.has_joy_motion_sensors(0): | |
| accelerometer_example() | |
| func accelerometer_example(): | |
| if not detect_accelerometer: | |
| return | |
| var acceleration = Input.get_joy_accelerometer(0) - Input.get_joy_gravity(0) | |
| if acceleration.length() > 10: | |
| if acceleration.x > 10: | |
| print("Moved left") | |
| elif acceleration.x < -10: | |
| print("Moved right") | |
| if acceleration.y < -10: | |
| print("Moved up") | |
| elif acceleration.y > 10: | |
| print("Moved down") | |
| # After detecting movement in one direction, the accelerometer sensor | |
| # will briefly report movement in the opposite direction, even though the controller only moved once. | |
| # So we need to ignore these reported values for a short amount of time. | |
| detect_accelerometer = false | |
| await get_tree().create_timer(0.5, false).timeout | |
| detect_accelerometer = true |
Partially addresses #11466
Follow-up to godotengine/godot#111679
The gyroscope calibration explanation was borrowed from https://github.com/JibbSmart/GamepadMotionHelpers?tab=readme-ov-file#gyro-calibration
TODO: