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


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?


2 Answers

Ken Alger
Ken Alger
Treehouse Teacher


The other alternative to the gyroscope is the accelerator sensor. There is a decent tutorial on the Tilt Ball Concept here.


Rebecca Rich
Rebecca Rich
Courses 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.: