views:

26

answers:

1

I'm working on an iPhone game that uses the accelerometer for control. I don't have an Apple Developer Program user yet, so I can't test it on my device, so I'm trying to give the game some accelerometer values depending on where I tap on the screen.

What I tried to do: I tried to have the middle of the screen as a mid point, then the offset of the tap from the midpoint would be the accelerometer's XY. I know these values would be over 1, and that the values I need are from 1 to -1, so I tried to do a lot of stuff to get it working. I was often close, but I couldn't get it to work.

Also, I want there to be a limit, if the distance from the midpoint is more than 100, (you know, sqrt(dx*dx+dy*dy), then it would take the distance as 100, instead of like 193 or something.

Here's my code so far:

- (void) ccTouchesMoved: (NSSet *)touches withEvent: (UIEvent *)event
{
    if(IS_SIMULATOR)
    {
        for( UITouch *touch in touches ) {
            CGPoint location = [[CCDirector sharedDirector] convertToGL: [touch locationInView: [touch view]]];
            CGFloat dx = location.x - middleOfScreen.x;
            CGFloat dy = location.y - middleOfScreen.y;
            CGFloat d = sqrtf(dx*dx+dy*dy);
            CGFloat a = atan2(dy, dx);
            CGFloat accX = /*acceleration value x*/
            CGFloat accY = /*acceleration value y*/

            //[mlRef setAcceleration: ccp(accX, accY)];
        }
    }
}

I hope you understand what I want, please help. :)

Also, before anyone recommends it, no I don't want to use iSimulate, my home internet dislikes it. :(

+1  A: 

So your question is really just how to normalize dx and dy? Just devide them by their maximum values, which should be 160 for dx and 240 for dy.

Otherwise, just NSLog or printf all values and just trace the values. If you still can't figure it out, post the logging code and it's output for a single touch.

mvds