gluLookAt accelerometer

Tag: opengl-es Author: richardlin99 Date: 2010-06-15

I am currently coding a little AR-Game for myself on the iPhone 3GS ;-)

I want to use the accelerometer & compass data for rotating my camera in opengl. The camera has a fixed position and can only rotate due to the accelerometer. The iPhone is initially rotated 90° to have a bigger widescreen ;-) So the axes are switched...

When I hold the iPhone straigt in front of me I get these values:

  • accel.x = 1
  • axxel.y = 0
  • accel.z = 0

When move the iPhone straight to the top of me (over my head) I get these values:

  • accel.x = 0
  • accel.y = 0
  • accel.z = 1

So the values are between :

  • x: 1 straight ahead and 0 over my head
  • y: 0 straight ahed and 0 over my head
  • z: 0 straight ahead and 1 over my head

I want to use x, y, z for my camera world coordinates. E.g. accel.x = 0.5 and accel.z = 0.5

The camera should change the centerX, centerY and centerZ values based on the values I get from the accelerometer.

How can I manage this?

Thanks ;-)

Other Answer1

If you're getting X=1 with the phone straight ahead of you, it sounds like you're holding the phone in landscape, with the button to the right, is that correct?

The accelerometer reports gravitational force, so X=1 means that you're going DOWN the X-axis on the screen.

This means that your +X direction (pixels) on the screen is really the +Y direction (gl drawing) in your OpenGL world, which corresponds to a -X accelerometer reading.

In this orientation, moving the phone to your left & right will give +Y and -Y accelerometer readings, respectively. How strong the readings are depend on with how much force you move the phone. If you move slowly, the reading may be indistinguishable from normal noise in the data. If you move at about the speed that the phone would fall if you dropped it, your reading will be close to "1" ("1 G-force" or "1 gravity.") If you move it faster than that, you will get higher readings.

NOTE: if you just turn, then stop moving, the accelerometer reading will revert to 0. Gravity is an measure of acceleration ("the acceleration due to gravity"!), not facing. This is why the compass became important.

If you tilt the phone screen-down or screen-up, the accelerometer's Z-reading will change, as gravity pulls "into" or "away from" the screen. With the screen perfectly vertical (as holding the phone straight in front of your face, any orientation), the accelerometer Z-reading should be 0.

How you translate that orientation data to your camera depends on your app. Just keep in mind that "steering" the camera by moving the phone will be tricky to get right, as you have to account for both acceleration AND deceleration of the user's movements!

To help you visualize this, check out XCode's sample-project "Accelerometer." Just search that word in the documents, and you'll see a sample project you can build and play with to get an idea of what each axis means.