Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Benyam Ephrem](https://secure.gravatar.com/avatar/5b312688e777db6adb5bb11ffade12b9?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Benyam Ephrem
3,185 PointsHow 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!!!
2 Answers
![Ken Alger](https://secure.gravatar.com/avatar/cf2cad7e6e37acacfa5201068856ed3d?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Ken Alger
Treehouse TeacherBenyam;
The other alternative to the gyroscope is the accelerator sensor. There is a decent tutorial on the Tilt Ball Concept here.
Ken
Rebecca Rich
Courses Plus Student 8,592 PointsHere 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:
http://www.compiletimeerror.com/2013/09/introduction-to-2d-drawing-in-android.html#.VN6KcbDF8h4
or using a circle drawable:
As far as using the Android gryoscope, consider looking over the docs for the Android sensors:
http://developer.android.com/guide/topics/sensors/sensors_motion.html
or specifically about Sensor Event values for Sensor.TYPE_GYROSCOPE:
http://developer.android.com/reference/android/hardware/SensorEvent.html#values
(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.:
http://stackoverflow.com/questions/20693547/when-to-use-accelerometer-or-gyroscope-on-android