Gyroscope and accelerometer processing library for game controllers, written in Odin.
Copy all .odin files to your project:
types.odin
gamepad_motion.odin
motion.odin
calibration.odin
settings.odin
Import in your code:
import gm "path/to/gyro"Initialize and process sensor data each frame:
gamepad := gm.Gamepad_Motion{}
gm.gamepad_motion_init(&gamepad)
gm.gamepad_motion_process(
&gamepad,
gyro_x, gyro_y, gyro_z,
accel_x, accel_y, accel_z,
delta_time,
)
orientation := gm.get_orientation(&gamepad)
gravity := gm.get_gravity(&gamepad)
accel := gm.get_processed_acceleration(&gamepad)
gyro := gm.get_calibrated_gyro(&gamepad)Gyro relative to the controller's orientation (pitch and roll):
pitch, yaw := gm.get_player_space_gyro(&gamepad)
pitch, yaw := gm.get_player_space_gyro(&gamepad, 1.5)Gyro relative to world gravity (useful for camera control):
pitch, yaw := gm.get_world_space_gyro(&gamepad)
pitch, yaw := gm.get_world_space_gyro(&gamepad, 0.1)Enable stillness detection (controller at rest):
gm.set_calibration_mode(&gamepad, {.Stillness})Enable sensor fusion (accelerometer-gyro fusion):
gm.set_calibration_mode(&gamepad, {.Sensor_Fusion})Enable both:
gm.set_calibration_mode(&gamepad, {.Stillness, .Sensor_Fusion})Start/pause/reset continuous gyro bias tracking:
gm.start_continuous_calibration(&gamepad)
gm.pause_continuous_calibration(&gamepad)
gm.reset_continuous_calibration(&gamepad)Set calibration offset directly:
gm.set_calibration_offset(&gamepad, offset_x, offset_y, offset_z, weight)
offset := gm.get_calibration_offset(&gamepad)Check calibration state:
confidence := gm.get_auto_calibration_confidence(&gamepad)
is_steady := gm.get_auto_calibration_is_steady(&gamepad)
gm.set_auto_calibration_confidence(&gamepad, 0.5)Adjust motion settings via Gamepad_Motion_Settings:
gamepad.settings.gravity_correction_still_speed = 1.0
gamepad.settings.gravity_correction_shaky_speed = 0.1
gamepad.settings.stillness_calibration_half_time = 0.1
gamepad.settings.sensor_fusion_confidence_rate = 1.0See settings.odin for all options. Use DEFAULT_SETTINGS for reasonable defaults.
Y-up (following PlayStation controller convention). Gyro in degrees per second. Accelerometer in g-force (1 g ≈ 9.8 m/s²).
Vec3 :: [3]f32
Quat :: struct {
w, x, y, z: f32,
}
Gamepad_Motion :: struct {
settings: Gamepad_Motion_Settings,
gyro: Vec3,
raw_accel: Vec3,
motion: Motion,
gyro_calibration: Gyro_Calibration,
auto_calibration: Auto_Calibration,
current_calibration_mode: Calibration_Mode_Set,
is_calibrating: bool,
}MIT License.