- Home /
Using acceleratorometer, manipulate the data to trick phone into thinking gravity is pointing in a different direction than down.
I’ve been trying to solve this problem for quite some time. I’ve tried multiple times with each one getting close to what I want except with some problems.
Basically imagine that the mobile device is attached to a box and the screen is a window into that box. Inside that box is a ball.
If you were to tilt and move the Phone/Box around, the ball would move according to physics. Now I want to recreate this virtually. Even though it would be cool, I don’t need it to be able to capture and respond to all types of movement. All I care about is tilt and the ball moving on only the x and z axis in Unity.
The obvious solution is to just take data from the accelerometer and set its x value to x and its y value to z. (Unity’s x,y,z doesn’t match up with how mobile devices x,y,z. For Unity, Y is up, for a mobile device, Z is up).
This would work however there is one more requirement. I want to in theory change the direction of gravity.
So in the natural position, flat on a table with the screen facing up, down in the game is down in real life, and up in the game is up in real life. Now if you were to tilt your device towards you by 90 degrees so it is in a vertical position, down in the game would be forward and in real life and up would be backwards.
So obviously the ball would roll backwards. (Think back to the ball in the box and how tilting the iphone like I said would move the ball backwards).
Now the tricky part is I want to change the direction of gravity. So when the phone is in that upright position, it could be set to neutral. So basically it’s as if gravity is now pulling down in unity, yet forward in the real world.
Since gravity in real life can’t be changed, I know there must be some way of manipulating the accelerometer to accomplish what I am trying to do.
Also, using the accelerometer data, if I could get the degree the Iphone is in reference to the natural position I could easily work with that to get what I need. After lots of work with this method, I was able to get both directions of rotating the device to work. However, when you rotate both directions at the same time, they get messed up for reasons that were beyond me. If you would like I could post that code.
I am pretty knowledgeable about coding; it’s just the logic that is troubling me. So you can write the sudo code of how to do it, or even the logical process of how to do it and I should be able to write my code based of that. But I would still appreciate the code. I am writing in javascirpt, but I can understand c# and boo (If anybody actually writes boo).
This has been troubling me for days and countless hours. I’ve made many attempts that have gotten close to what I want but haven’t quite accomplished it.
If you need more clarification please feel free to ask. It is really hard for me to translate what is in my mind to written English. Oh and by the way I am using 4.6 if that will make any difference (I doubt it will, but who knows).
Answer by meat5000 · Nov 27, 2014 at 03:27 AM
It doesn't matter which way the gravity goes, it matters how the rigidbody physics of the object reacts to it.
Also, do you know you can just change the direction of gravity?
http://docs.unity3d.com/ScriptReference/Physics-gravity.html
I think your misunderstanding my question. I am not necessarily changing changing gravity in the scene. Eventfully I may move my ball using addForce or change gravity. Right now I am mostly concerned about getting data assigned in a variable, which I then can later use to move my ball in the scene. Sorry, my question is probably really confusing.
As you know real gravity is always down you have your reference direction. I assume if you hold your device portrait at your face you want your gravity to be going straight ahead (toward the 'bottom of the box').
You have your ref direction, you know exactly which way the device is being held and you have the ability to change the physics gravity direction. That's all you need man.
You should sketch it out on paper marking clearly the accelerometer directions etc, then sketch it again in a different position noting only the changes.
The other thing that may help is a 'recalibration' button or method of sorts, to re-zero your reference down point.
Your answer
Follow this Question
Related Questions
I'm trying to display my ammo; not working 2 Answers
bouncing circle off another circle 2 Answers
How to set GUI X and Y positions from accelerometer.x and accelerometer.y ? 1 Answer
Is it possible to switch off forced strong typing for ios/android? 2 Answers
How to detect swipe on an Android device using javascript? 0 Answers