diff --git a/App.js b/App.js index ceec8d0..369539b 100644 --- a/App.js +++ b/App.js @@ -55,6 +55,28 @@ const Game = () => { ], })); + // Setup the device motion sensor listener + React.useEffect(() => { + // Set the update interval to 16ms (60fps) + DeviceMotion.setUpdateInterval(16); + + const subscription = DeviceMotion.addListener((deviceMotionMeasurment) => { + // Update the ball position based on the device motion sensor. + ballAnimation.value = { + // Change the value of the ball's x position by the device motion sensor's gamma value + x: getConstrainedBallX( + ballAnimation.value.x + deviceMotionMeasurment.rotation.gamma * 12 + ), + // Change the value of the ball's y position by the device motion sensor's beta value + y: getConstrainedBallY( + ballAnimation.value.y + deviceMotionMeasurment.rotation.beta * 12 + ), + }; + }); + + return subscription.remove; + }, []); + return (