Quantix Games


Accelerometer in the iPhone Simulator

  • 24 June 2010 ·· 18:24
  • Marcelo Catach
  • No comments
  • It can be a frustrating and time consuming task to work with the accelerometer feature in iPhone development. The iPhone Simulator does not come with any built-in capabilities to receive accelerometer events from factory.

    Some developers accomplished a way to simulate it in the Simulator. One technique involves using a third party library that generate events and send them to your program. Another technique is kind of esoteric, as it reads your internal MacBook accelerometer and handle them as they would come from an iPhone. I just can’t imagine myself turning my MacBook upside-down just to test our game :o

    I got a simple solution to simulate it. First I call the accelerometer from my update function (notice that I’m using a preprocessor macro), using nil as parameters:

    #ifdef ACCELEROMETER_SIM
    	[self accelerometer:nil didAccelerate:nil];
    #endif
    

    Then, on the accelerometer function, I test the program directly inputting the vectors that I need to test. In this case I want to test the Y axis:

    - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
    {
    	// Use a basic low-pass filter to keep only the gravity component of each axis.
        mAccelX = (acceleration.x * FILTER) + (mAccelX * (1.0 - FILTER));
        mAccelZ = (acceleration.z * FILTER) + (mAccelZ * (1.0 - FILTER));
    
    #ifdef ACCELEROMETER_SIM
    	mAccelY = (-1.0f * FILTER) + (mAccelY * (1.0 - FILTER));
    #else
    	mAccelY = (acceleration.y * FILTER) + (mAccelY * (1.0 - FILTER));
    #endif
    
    }
    

    It’s super simple, and you can have your tests right from your iPhone Simulator. No more uploading to device just to test :)

  • Languages: English
  • No comments

    No comments at this moment.

    RSS feed for comments on this post.

    Sorry, the comment form is closed at this time.



    Latest updates

    April 2014
    M T W T F S S
    « Feb    
     123456
    78910111213
    14151617181920
    21222324252627
    282930  

    Copyright © 2008-2012 Quantix Games ·· All rights reserved.
    Rua Itamatai 17, Cj. 62 ·· Sao Paulo, SP, Brazil ·· 55 11 9426-6570