Jump to content

View more

Image of the Day

WIP title screen for #DeathOfAPartisan #screenshotsaturday #gamedev https://t.co/qJNhfZCvd4
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Accelerometer and Gyroscope as joysticks

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 ill   Members   


Posted 06 March 2012 - 10:58 AM

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.

#2 JoJoSim1   Members   


Posted 11 April 2012 - 06:45 AM

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.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.