Log in or register to post comments

Mapping Input.acceleration to Physics.gravity

June 23, 2011 - 2:44pm #1

What I'm trying to accomplish is keeping in-game gravity [approximately] aligned with actual gravity, regardless of camera or ImageTarget orientation. It seems like it should be a simple feature to implement -- the Input.acceleration values should give you the direction of gravity in local space, and TransformDirection() should transform the direction to world space. So far, the best I've been able to come with as far as getting the direction is:
Camera.main.transform.TransformDirection ( Input.acceleration )

This is producing the wrong direction, and I'm not entirely sure why. Any insight?

Re: Mapping Input.acceleration to Physics.gravity

June 23, 2011 - 10:37pm #3

After messing around with every possible combination of values, I finally found one that seems to have produced the desired effect:
Camera.main.transform.TransformDirection ( new Vector3 ( -Input.acceleration.y , Input.acceleration.x , -Input.acceleration.z ) )

I'm still not entirely sure why some of the axis had to be inverted or even swapped. I'll have to work on my 3D math skills.

Since the objects are moved exclusively by gravity and not directly by accelerometer values, object motions don't appear jittery in this situation. Jitters in the Physics.gravity vector seem to average themselves out as far as their affect on object velocities. Without any tweaking, my test case of balancing a marble on an ImageTarget already seems to approximate what you'd expect to see if you were rolling an actual marble on the ImageTarget. But I imagine your input will prove very helpful as I go further into this project. Thank you very much.

Re: Mapping Input.acceleration to Physics.gravity

June 23, 2011 - 7:38pm #2

I believe that in order to get a gravity vector from accelerometer values you want to apply a low pass filter. The values coming from the accelerometer detect instantaneous motion and are quite jittery, you have to average over time and remove the noise to get a nice gravity vector. This might be a helpful Unity thread on the subject: http://forum.unity3d.com/threads/55573-Accelerometer-and-Low-Pass-Filtering

- Kim

Log in or register to post comments