# Accelerometer and Gyroscope as joysticks

This topic is 2435 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I basically want the game to play similar to the Doom Resurrection game, only instead of being on a rails, you get full movement. Also you are a hovering space ship so you have 6 degrees of movement that match the device movement.

I'm getting accelerometer input to get the device acceleration. I'm trying to convert that to velocity so I can know the position of the device relative to where the user initially held it.

This way holding the device still acts like no joystick input. If I move it to the left a little, it should act like I moved the joystick to the left a bit, then if I move it back it should be back to no input.

This is turning out to be VERY difficult due to the imprecision of the accelerometer. I guess the best way is to find the right things to dampen. I'd like to find out how other people did this kind of thing, if it's even possible. The problem is that when the device stops moving the velocity doesn't always go back to zero. This can happen if the device didn't read the decelleration causing the velocity to go back to zero.

Here is some code, it's in C++ though since I'm porting a C++ engine to iphone. I just mix in the Obj C wherever I need to.

m_deviceOffset is supposed to be the joystick offset position.
 glm::vec3 deviceAccel; iosToGlm(iosMotionManager.deviceMotion.userAcceleration, deviceAccel); deviceAccel = highPassFilterVec(deviceAccel, m_previousAccel, ACCEL_FILTER); m_previousAccel = deviceAccel; deviceAccel = dampenVec(deviceAccel, ACCEL_DEADZONE); m_deviceVelocity += deviceAccel; m_deviceOffset += m_deviceVelocity; m_deviceOffset = dampenVec(m_deviceOffset, OFFSET_DEADZONE); 

Some of the utility functions I use up there...
 /** Snaps the value to 0.0 if it's between -dampFactor and +dampFactor */ template <typename T> inline T dampen (T value, T dampFactor) { if(value > (T)0.0 && value < dampFactor) { value = (T)0.0; } else if(value < (T)0.0 && value > -dampFactor) { value = (T)0.0; } return value; } /** Does a high pass filter on a value */ template <typename T> inline T highPassFilter(T value, const T& previousValue, T filterFactor) { return (value * filterFactor) + (previousValue * ((T)1.0 - filterFactor)); } 

I'm guessing I might have issues similar to this when I start getting device rotation to work as well. I wouldn't mind any preemptive advice If anyone has any tips on reading rotation as input for games.

##### Share on other sites
Hi
In our game BallAHolic we use gyroscope and accelerometer. We first used the same highpassfilter you have in your code but we noticed that the noise was stil there and controlling the game was too frustrating. The gyroscope has less noise and can be used out of the box. For accelerometer only devices we came up with a hysteresis filter.

accelerationVector currentVector if(angle(accelerationVector,currentVector) > threshold) currentVector = accelerationVector;

To get this transition smooth we interpolate between those two vectors.
Of course you loose responsiveness but also loose noise.
I'm not quite sure if that works for you as well, since you want to estimate the translation of players device.
Another thing that could be helpfull is not to use clamping in your dampen function. It would be much smoother if you would use a weighting function and multiply your value by it.
I hope this gives you new ideas.
Good luck

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633661
• Total Posts
3013219
×