manish ghildiyal
Ranch Hand
Posts: 136
Hi,

I am trying to develop a simple game where players just tap a ball around.
So 4 players are positioned on a circle at gap of 90 degree each. What I want
to have is that for passing the ball from one player to another player user needs to tilt the device
in appropriate direction.
So my plan is to somehow determine the direction of tilt so that I can determine to which player
the ball would move next.
I have gone through articles and forum posts, and understand that to determine tilt direction
I need to make use of rotation matrix.
But I am not able to really understand how to go through it all. Android documentation seems to be sparse.
Can someone give me some direction on this?

Manish

manish ghildiyal
Ranch Hand
Posts: 136
....sorry to forget to mention that I am working on Android.

Karthik Shiraly
Bartender
Posts: 1208
25
If player holds the phone flat with screen facing up and then rotates the phone around z-axis (ie, "yaws" on the axis coming out of screen) while still keeping it flat, should the ball move?
If not, then I feel accelerometer sensor values are enough; you don't really need a rotation matrix.
I've written an app which visualizes accelerometer readings on a graphical x,y,z axis and I can confirm that accelerometer values change when phone is tilted while stationary. But they'll also change if phone
is moved along one of the axes while not changing the tilt. So your app may need some logic to differentiate between the two.
The image below shows which axis is which:

manish ghildiyal
Ranch Hand
Posts: 136
Hi Karthik,

Thanks for inputs.

So, when I have accelerometer values with me then how can tilt data be extracted from it? Do I need to use
trigonometry for it or is it much more involved?

Manish

Karthik Shiraly
Bartender
Posts: 1208
25
You don't need any trigonometry at all, just division.
Imagine the diagram below is a phone kept flat with screen pointing up, and you're looking at the screen directly vertically down.

Documentation is here.
From observations, accelerometer values for a stationary phone that is tilted are:
- phone is flat with screen pointing up: x=0, y=0, z=+g (g = accel due to gravity, ~9.8)
- BC is tilted down towards the floor 45 degrees clockwise so that AD moves up (ie, clockwise rotation around y-axis): x = -g/2, y=0, z=+g/2
- BC is tilted down towards the floor 90 degrees clockwise so that AD is directly above it (clockwise rotation around y-axis): x = -g, y=0, z=0
- AD is tilted down towards the floor 45 degrees counterclockwise so that BC moves up (ie, CCW rotation around y-axis): x = +g/2, y=0, z=+g/2
- AD is tilted down towards the floor 90 degrees counterclockwise so that BC is directly above it (CCW rotation around y-axis): x = +g, y=0, z=0
- AB is tilted down towards the floor 45 degrees clockwise so that DC moves up (ie, clockwise rotation around x-axis): x=0, y=-g/2, z=g/2
- AB is tilted down towards the floor 90 degrees clockwise so that DC moves directly above it (ie, clockwise rotation around x-axis): x=0, y=-g, z=0
- DC is tilted down towards the floor 45 degrees counterclockwise so that AB moves up (ie, CCW rotation around x-axis): x=0, y=+g/2, z=g/2
- DC is tilted down towards the floor 90 degrees counterclockwise so that AB moves directly above it (ie, CCW rotation around x-axis): x=0, y=+g, z=0

So you can see that 45 degree angle corresponds to g/2 (I can see it's approximate; not sure why - but probably good enough for a game). You can use simple proportion of measured value / g to get angles.