Hi there,
I am having problems with rotating a cube object via accelerometer.
Here is my code:
transform.rotation = Quaternion.identity;
dir = iPhoneInput.acceleration;
var tiltX = -dir.z * degree;
var tiltZ = -dir.x * degree;
var target = Quaternion.Euler(tiltX, 0, tiltZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * 3.0);
I have a ball inside a cube and I would like to be able to rotate the cube upside-down when i turn the iphone upside-down, any suggestion?
At the moment the ball is just moving correctly but only on the floor!
Why are you not looking at the y-component of the acceleration? It’s value will range positive to negative as well indicating whether the phone is right-side-up or up-side-down.
Really? I am using the Y component in my XVaders and it is allowing me to move the ship left and right when the phone is on its side. I thought the X was the horizontal while the phone was in portrait and the Y was horizontal when the phone was in landscape per say. I have no clue what the Z is for in either mode myself.
I can’t find it in the Docs either so I would be interested in knowing this also.
Then it may be the z, I’m not remembering entirely. But one axis points up/down out of the face of the device, checking against that will easily tell you whether you’re up-side-down or right-side-up. Either way, there are three axes, use them!
Or just check the iPhoneInput.orientation value as it too tells you this information (face up/down status).
I tried to work with the orientation but no much luck.
The iPhone orientation change at 45 degree…?
My problem is to translate the 1 to -1 input of the iPhone to the 0 to 360 or 0 to -360 of Unity to have the object moving the same as the iphone.
That’s a problem inherent in the device. The data you are getting is from an accelerometer, so it’s measuring g-forces. These don’t translate directly into degrees. At about 45*, halfway from 1 - 0. or -1 - 0, you get a g-force of about 75%, or 0.75 (iirc). I’ve been able to run translations for the input I need by graphing the output from the accelerometer through equations against possible input ranges (excel, other spreadsheet or graphing program), but that is far from getting an absolute position from the device.
If you are serious, I’d look into all that angle calculation felgercarb like quaternions and dot products to look at all the axes and try to estimate the orientation of the device…
However, keep in mind that this is an estimate as the device contains an accelerometer. What you get is the g-force on the object, so any movement on the device returns g-force data that does not take into account the orientation of the device, just the force acting upon it.
If you look at the output of the device, and just shove it away from you, you’ll see what I mean.
Found this site, that has a good resource on the position of the device…
http://blog.digitalagua.com/2008/07/15/accelerometer-xyz-based-on-iphone-position/
What I think is interesting about this analysis is that looking at ALL of the axes, you can get an idea about the discreet orientation of the device.
Now you need someone who understands maths to belly up to the bar and talk about analyzing the axes data for that purpose (and my wife is out tonight - so no dice in that department…)
Thanks little Angel,
I don’t want to be so precise! I would like to be able to rotate my cube when I rotate the iPhone so that my ball, inside the cube, would follow.
The camera is attached to the cube so the only thing you’ll see moving is the ball…
P.S. I had a look at that site but at end I got my reading from the iPhone.
Take a look here if you haven’t already:
http://forum.unity3d.com/viewtopic.php?t=21161
I don’t think this is exactly what you are looking for, but it may be or it may be close.
As I suspected it seems to be a dot product solution.