Search code examples
cocos2d-iphonekobold2d

KKInput panningGesture giving wrong values when Iphone is tilted


I'm using KKInput from kobold2d to do some drag and drop using the panning Gesture recognizer. When the iphone is flat on the table it works perfectly, but if I tilt the phone towards me the translation seems completely wrong and no longer behaves normally, in fact it seems to think my IPhone is upside down I think.

Am I doing something wrong?

Sample code:

if([input gesturePanBegan])
    {
        for( CCSprite* item in self.View.children )
        {
            bool result = [input isAnyTouchOnNode:item touchPhase: KKTouchPhaseAny];
            if (result)
            {
                itemPanning = item;
                originalPostion = item.position;

            }
        }
        CCLOG(@"%f y translation %f x translation", input.gesturePanTranslation.y , input.gesturePanTranslation.x);
        if(itemPanning != NULL)
        {
            [itemPanning setPosition:ccp(input.gesturePanTranslation.x + originalPostion.x, originalPostion.y)];
            if(input.gesturePanTranslation.x > 70)
            {
                [View Select: [itemPanning tag]];

                SelectAttackCommand * command = [SelectAttackCommand new];
                command.SelectedAttack = [itemPanning tag];

                itemPanning = NULL;

                NOTIFY(command);
            }
        }
    }
    else if(![input gesturePanBegan] && itemPanning != NULL)
    {
        itemPanning = NULL;
        [View Open];
    }

Solution

  • This is an answer, but maybe not the best one. In KKInputGesture, under handlePanGesture, the values for translation is calculated as follows:

    gesturePanTranslation = [panRecognizer translationInView:glView]; gesturePanTranslation = [self convertRelativePointToGL:gesturePanTranslation];

    the second call here translates a value based on the orientation of the device. this may be fine for some scenarios, but in my case it wasn't needed so I commented it out, now my translation values are always correct regardless of how I tilt the IPhone.

    I may be missing something here though, so I don't want to mark this as the answer just yet.