greg rock
greg rock

Reputation: 239

Rotate a sprite in cocos2d using accelerometer

I'm on cocos2d and I have a sprite that I would like to rotate with the accelerometer.

I've heard about CMMotionManager. I would like to know if it is possible to use it just for 2D rotation, and, if yes, how?

Upvotes: 0

Views: 1379

Answers (1)

skytz
skytz

Reputation: 2201

put this in onEnter:

UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.updateInterval = 1.0/50.0; //update interval in sec...so 1/50= 20 ms
accelerometer.delegate = self;

you need to conform to UIAccelerometerDelegate like so:

@interface MyClass:CCLayer <UIAccelerometerDelegate>

and implement this in MyClass.m:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration    *)acceleration {
CCLOG(@"x = %f y = %f z = %f",acceleration.x,acceleration.y,acceleration.z);
mysprite.rotation=acceleration.x*20;
}

edit: almost forgot...put accelerometer.delegate = nil; in onExit

note that the method is called every time the accelerometer changes value..in all 3 vectors

cards on the table..i didnt use accelerometer ...ever..but it should look something like this...check the rotation property in the documentation and play a little with it

hope it helps

PS: loved the "sorry for my english i'm french" part...hilarious

edit: here is my test of that code..and made a few modifications..it works fairly smooth..if you dont like it play around with the values.

#import "cocos2d.h"

// HelloWorldLayer
UIAccelerationValue accelerationX;
UIAccelerationValue accelerationY;
float currentRawReading;
float calibrationOffset;

@interface HelloWorldLayer : CCLayer <UIAccelerometerDelegate>
{
    CCLabelTTF *label;
}

// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;

@end





#import "HelloWorldLayer.h"

// HelloWorldLayer implementation
@implementation HelloWorldLayer

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

#define kFilteringFactor .05


CGFloat RadiansToDegrees(CGFloat radians) {return radians *180/M_PI;};



// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {

        UIAccelerometer *accel= [UIAccelerometer sharedAccelerometer];
        accel.delegate=self;
        accel.updateInterval=1/60;



        // create and initialize a Label
        label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];

        // ask director the the window size
        CGSize size = [[CCDirector sharedDirector] winSize];

        // position the label on the center of the screen
        label.position =  ccp( size.width /2 , size.height/2 );
        label.flipY=YES; //i have absolutly no idea why the label is fliped :/
        label.flipX=YES;
        label.rotation=0;
        // add the label as a child to this Layer
        [self addChild: label];
    }
    return self;
}

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{

    CCLOG(@"acc called");
    accelerationX=acceleration.x *kFilteringFactor +accelerationX *(1-kFilteringFactor);
    accelerationY=acceleration.y*kFilteringFactor +accelerationY *(1-kFilteringFactor);
    currentRawReading=atan2(accelerationY, accelerationX);

    label.rotation=-RadiansToDegrees(currentRawReading);

}


// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    // don't forget to call "super dealloc"

    [super dealloc];
}
@end

Upvotes: 1

Related Questions