Home > iPhone Development > Cocos2d Example – Accelerometer

Cocos2d Example – Accelerometer

In this tutorial, we will utilize Accelerometer in the Bouncing Ball example. The ball will move when you rotate or tilt your iPhone. To complete this tutorial, you need to get the certificate to put your app on a device. It is because when your application asks for updates from Accelerometer, the simulator will not response.

Generally, you need three steps to make your Cocos2d app to have accelerometer capability. Before that, we need to make walls and roof to prevent the ball moving out of the screen.  Add the following codes at the end of setChipmunk function:

floorShape = cpSegmentShapeNew(floorBody, cpv(0,0), cpv(0,480), 0);
floorShape->e = 0.5;
floorShape->u = 0.1;
floorShape->collision_type = 0;
cpSpaceAddStaticShape(space, floorShape);

floorShape = cpSegmentShapeNew(floorBody, cpv(0,480), cpv(320,480), 0);
floorShape->e = 0.5;
floorShape->u = 0.1;
floorShape->collision_type = 0;
cpSpaceAddStaticShape(space, floorShape);

floorShape = cpSegmentShapeNew(floorBody, cpv(320,0), cpv(320,480), 0);
floorShape->e = 0.5;
floorShape->u = 0.1;
floorShape->collision_type = 0;
cpSpaceAddStaticShape(space, floorShape);
Step 1: Enable accelerometer in Cocos2d app. Add the following code at the end of init method of BallLayer class.

isAccelerometerEnabled = YES;

Step 2: Set update interval. This interval determines how frequently we get the data from accelerometer. It is also how frequently the updating function (implemented in Step 3) will be called. In this example, we choose 0.033. You should choose appropriate interval based on the types of your application. Add the following code at the end of init method of BallLayer class.

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 30)];

Step 3: Implement updating function. You should acquire the data from accelerometer in this function. Add the following function to the implementation of class BallLayer.

#define kFilterFactor 0.05
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{
static float prevX=0, prevY=0;

float accelX = acceleration.x * kFilterFactor + (1- kFilterFactor)*prevX;
float accelY = acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY;

prevX = accelX;
prevY = accelY;

cpVect v = cpv( accelX, accelY);
space->gravity = cpvmult(v, 2000);
}

Line 2 and 3 of the codes should be noted. Here we use low-pass filter (0.05) to reduce the influence of sudden movement (i.e., less sensitive). For more information about low-pass and high-pass filter, please refer to iPhone Application Programming Guide or related books. In this function, Z axis is ignored. According to the new values of X and Y, the gravity of ChipMunk space is calculated. Note: The value of gravity determines the direction which the ball moves to.

Now, the Bouncing Ball app owns accelerometer capability.

Categories: iPhone Development
  1. May 27th, 2009 at 10:35 | #1

    this one helped me get around my previous problem of defining the boundry bodies. I was defining different ones for each (top,bottom,left,right) instead of defining one and adding onto it, like you did. Thanks. works great.

  2. Austin
    May 28th, 2009 at 18:23 | #2

    Quick question about chipmunk. I have tried to do your first tutorial but use chipmunk physics. Everything works fine except when I tap the screen the image will move to the tap and then move back to its previous position (it wont stay where I tap). Any Ideas?

  3. Andrew
    June 23rd, 2009 at 20:34 | #3

    Thanks for the tutorial, this was a great starting point for me. I am wonder if you could help me with a quick problem. I am trying to get the accelerometer to apply a force to the cpBody instead of changing the global gravity. I know to use the cpBodyApplyForce or cpBodyApplyImpulse methods, but I do not know what I should put as the gravity offset vector. Any help?
    Thanks.

  4. jose
    June 27th, 2009 at 15:50 | #4

    HI !

    thanks for sharing this, im haveing serious troubles because im trying to implement my game in landscape mode, and i mean testing on simulator running in landscape.

    until i tell shareddirector chipmunk doesnt seems to rotate their coordinate system.

    any help ?

  5. Pravin
    July 7th, 2009 at 00:47 | #5

    Thanks, for this tutorial It’s working great in portrait mode.
    Here we are setting floor Shape to move accelerometer
    But in Landscape mode ship movement does not work properly.In this case how to set floor shape.

  6. Pravin
    July 10th, 2009 at 03:29 | #6

    Thanks for the tutorial, Now I have question that is it possible to create different floor for different object in same space.Please give sample example.

  7. September 26th, 2009 at 16:01 | #7

    Hey thanks for the tutoral puts me on the way for my first game, by the way, do you know how to deal with collision filters on chipmunk like box2d does?

  8. hart7ae
    December 31st, 2009 at 12:15 | #8

    Newbie question here.. I’ve got this example working in portrait mode but when moving to Landscape (i.e. [[Director sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft];) my Layer still seems to be in Portrait and my world is all screwed up, I assumed my floorShape in my ballLayer needed updated but that doesn’t seem to work. Not much documentation around on this… any suggestions?

  9. hart7ae
    January 11th, 2010 at 21:20 | #9

    Disregard. I worked on this a bit more and and now understand the boundary concept and the Chipmunk coordinate system.

  1. May 20th, 2010 at 12:14 | #1