- Home /
3D camera relatively using gyro a la N.O.V.A. 2
Hello awesome people! :) I am trying to make a 3D camera that uses iphone 4's gyro. I've researched quite a bit on google and unity forum and unity answers and found this http://forum.unity3d.com/threads/98828-sharing-gyroscope-controlled-camera-on-iPhone-4/page2?highlight=gyroscope and this http://forum.unity3d.com/threads/75288-Gyroscope-Quaternion-to-Unity-Camera-%28HELP-PLEASE!%29 and read whole bunch of other links and I have the basic idea about how to use gyro for the game's main camera. Basically, offset the raw data(attitude) gotten from gyro and apply a fixed quaternion rotation to make it meaningful (although I don't have a slightest idea how phoberman came up with the numbers in his script) then apply that rotation to camera. I've gotten the script to work in my prototype for now.
My problem is this. I am trying to make a camera that uses gyro, but relative to the current position(or rotation) of the phone and also incorporate a finger control , a la N.O.V.A. 2. The camera in that game starts looking "forward (vector3(0, 0, 1) in the game world, if you will)" regardless of the position you are holding the phone, then moves according to the gyro using that "forward" as a reference. Then you can still look around by touching anywhere on the screen with one finger and moving it around.
The first step would be making the gyro relative, not absolute (as in making the camera point where the gyro is pointing) as what my current prototype is doing. Right now if I start my game holding the phone downward, it will start looking down, not "forward." I have been thinking about it and thought if I applied a quaternion rotation from the gyro's attitude to "forward", the game would start and the camera would look "forward." It did, but the movement update was not really working as I expected. Then I tried getting two directional vectors from where the camera is looking(governed by gyro) and the initial forward, got a quaternion rotation from them using fromtorotation function, and tried using it but it didn't work either.
What I had in my mind was to get some sort of fixed rotation or a fixed angle between the initial gyro lookat and the initial "forward" vector then to apply it constantly whenever the camera's moving, but it failed. I realized that that way of thinking was wrong as initially I thought of moving camera like this: lock your thumb(gyro's lookat) and index finger(initial"forward") and move them together. Now I realize it's more like two index fingers, independent from each other but still related. However this is further confusing me.
Now I think I should be doing the following:
Start() {
get the gyro's lookat.
get the initial forward and name it "forward" or something
}
Update() {
get the gyro's new position and compare it to 1 to get a new rotation. then assign old lookat = new lookat.
somehow convert it to a relative rotation on the game world => <<< lost here >>>
applyl 2 to "forward" and update the camera using camera.lookat(forward) or something like that
}
More on 2 in update(). I am using LFR as in unity. So If i am looking down to start the game, my initial lookat is down(0, -1, 0), but my "forward" is (0, 0, 1). If I move my phone to the right, the gyro will think it's moving from down -> right(1, 0, 0) but my "forward" should also look at right(1, 0, 0). Now if I initially started the game holding my phone left(-1, 0, 0) then moved 90 degrees to right(at this point lookat would be looking at (0, 0, 1)), "forward" should be looking at right(1, 0, 0) after that rotation. Does this make sense?
Am I wrong in thinking that? Can anyone please shed some light on this issue? Quaternion is driving me crazy :( And I haven't even begun to think about applying the finger control...
I also asked this question on the forum if you don't have the definite answer but wanna discuss about it here: http://forum.unity3d.com/threads/129540-3D-camera-relatively-using-gyroscope-a-la-N.O.V.A.2 Thank you! :)
So I've been fiddling with some stuff, and made a bit of progress, if not much. I made two game objects and applied gyro's rotation to the first one and saved the initial lookat Vector on the second one as the forward vector of that object, then found a rotation between those two objects then applied it on the camera. It's still buggy for some reason, and far from being completed but if anyone's interested here's a package file : http://www.bigupload.com/en/file/36474/iphonegyrocamtest.unitypackage.html If you don't have an iphone to build this on, you can still use the editor to see what I was trying to do.
This is working for the most of the part as far as detecting the starting position of gyro and applying camera accordingly so it will start looking at "front." However I find it beco$$anonymous$$g irregular after building a couple of times as the script doesn't detect the forward vector properly? However this is a start. If anyone's got any idea as to what's causing that, please let me know. I also need to work on saving the camera's lookat so when the game's paused and gyro's moved, the camera's still looking at the same position. Finger control can come after that.
Yeah, I'd also like to know if you got a solution for this