Welcome to the Treehouse Community
Looking to learn something new?
How do I use a device's gyroscope to manipulate a ball on a screen?
I'm making an app that will manipulate a ball by having the player tilt the screen to move the ball. Code wise how do i achieve this? How do I attach a ball (how do I make a 2D ball to be manipulated in the first place?) to the position data getting abilities of a phone's gyroscope to move the ball according to how the player tilts the screen?
THANK YOU SOOOO MUCH!!!
Rebecca RichCourses Plus Student 8,592 Points
Here are a few things you can check out to maybe get you stared in this direction.
For drawing a 2D Ball (or circle) on the screen, you could consider just drawing the circle shape:
or using a circle drawable:
As far as using the Android gryoscope, consider looking over the docs for the Android sensors:
or specifically about Sensor Event values for Sensor.TYPE_GYROSCOPE:
(You might also consider Sensor.TYPE_ROTATION_VECTOR .. can help with data accuracy and offers some sensor fusion.)
Whatever you do, keep in mind that constant use of the gyroscope events could drain device batteries.
There is some useful info here on gyroscope sensitivities and trying to limit your balls movement for only significant movements of the phone, etc.: