CCLayerPanZoom for cocos2dx

Hello.

Have smb ported already this extension for cocos2dx? Or maybe some part of it.

Thanks.

I saw the video, it’s definitely brilliant.
Anyone who can port this extension will be appreciated!

Can’t you do the same thing simply with CCCamera + (targeted) touch(es) already?

I’ve ported this extension. You can get it here - https://github.com/gameslovin/cocos2dx_extension . Btw, i’ve found some bug in cctouch. previousLocationInView function doesn’t return right value. I’ve done some changes in EAGLView.mm

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     cocos2d::CCSet set;
     cocos2d::CCTouch *pTouch;

    for (UITouch *touch in touches) {
                NSNumber *index = (NSNumber*)CFDictionaryGetValue(touchesIntergerDict, touch);
                int unUsedIndex = 0;

                // it is a new touch
                if (! index) {
                    unUsedIndex = [self getUnUsedIndex];

                    // The touches is more than MAX_TOUCHES ?
                    if (unUsedIndex == -1) {
                        return;
                    }

                    float x = [touch locationInView: [touch view]].x;
                    float y = [touch locationInView: [touch view]].y;
//Here I replace constructor for cctouch
                    pTouch = s_pTouches[unUsedIndex] = new cocos2d::CCTouch(0,x,y);
                }else{

                    float x = [touch locationInView: [touch view]].x;
                    float y = [touch locationInView: [touch view]].y;
                    pTouch->SetTouchInfo(0, x, y);
                }

                CFDictionaryAddValue(touchesIntergerDict, touch, [NSNumber numberWithInt:unUsedIndex]);

        set.addObject(pTouch);
    }

    cocos2d::CCDirector::sharedDirector()->getOpenGLView()->touchesBegan(&set);
}

Dany Menko wrote:

Can’t you do the same thing simply with CCCamera + (targeted) touch(es) already?

Maybe you can provide some example?

No, I just thought I’d translate pinch into CCCamera zoom in/out, then realized it would work differently.

Give me the solution about CCcamera and zooming sprites

CCLayerPanZoom not working for newest Cocos2d-x version. Can you update it?

Did it for myself. It’s mainly untested, since I have no multitouch device at hand. But it works in my emulator, so maybe you could just give it a try. Instead of a round function I am just casting to int though, which MAY be not that accurate. But again, just give it a try (and change it as you need).

Andreas, does rubber banding work with this code? I am not sure how it is supposed to be used (i.e. what values to set). So I am wondering if I am using the class wrong or if it’s not implemented.

I also cant make rubber effect working with this version posted by Andreas and scaling also does not work 100% properly.

Andreas, I meet the trouble like this using your class: put one finger without moving, and then put the second finger. the Layer jumps to the right bottom corner. what can I do to avoid this. thank a lot.

I have added the CCLayerPanZoom to a sample HelloWorld cpp project. The class HelloWorld is inheriting from the CCLayerPanZoom class. But the pinch zoom / pan is not working. Do I need to do anything more to make this work?

Asloob Qureshi wrote:

I have added the CCLayerPanZoom to a sample HelloWorld cpp project. The class HelloWorld is inheriting from the CCLayerPanZoom class. But the pinch zoom / pan is not working. Do I need to do anything more to make this work?

I just bug fixed the problem.

In ccOnTouchesMoved function:

float prevScale = this~~>getScale;
float curScale = this~~>getScale() * ccpDistance(curPosTouch1, curPosTouch2) / ccpDistance(prevPosTouch1, prevPosTouch2);

When you started to move the layer, the calculation of curScale will be wrong. Instead, you could fix the scale like this:

if(!zoomStart){
zoomStart = true;
curScale = prevScale;
}

When zooming is ended, just set zoomStart to false in ccOnTouchEnded.

" I meet the trouble like this using your class: put one finger without moving, and then put the second finger. the Layer jumps to the right bottom corner. what can I do to avoid this. thank a lot." i have the same issue has anyone solution?

And it is working fine in android but not in IOS…

I am facing same issue with CCLayerPanZoom, It is working fine on Android devices but on IOs facing this issue “put one finger without moving, and then put the second finger. the Layer jumps to the right bottom corner”. whats wrong on IOs devices??

game dev wrote:

I am facing same issue with CCLayerPanZoom, It is working fine on Android devices but on IOs facing this issue “put one finger without moving, and then put the second finger. the Layer jumps to the right bottom corner”. whats wrong on IOs devices??

In CCLayerPanZoom::ccTouchesMoved:

Comment out these buggy lines:

// If current and previous position of the multitouch's center aren't equal -> change position of the layer
        if (!prevPosLayer.equals(curPosLayer))
        {            
            this->setPosition(ccp(this->getPosition().x + curPosLayer.x - prevPosLayer.x,
                this->getPosition().y + curPosLayer.y - prevPosLayer.y));
        }

Hope it works!:wink:

I’ve tried this for wp8 , it works apart the same issue others are experiencing, I’ve tried the mentioned fixes, but they don’t seem to work, does anyone else have an alternative fix, for the zoom, returning to the bottom right?

Thanks!

If you comment this lines you can’t move layer with two fingers.
In iOS this problem happens because touch1->getPreviousLocationInView() returns (0, 0) if touch1 has no previous touch.
I has corrected CCTouch.cpp

// returns the previous touch location in screen coordinates
CCPoint CCTouch::getPreviousLocationInView() const 
{
    if (m_prevPoint.x==0 && m_prevPoint.y==0)
        return m_point;
    return m_prevPoint;
}

OR if you don’t want change CCTouch.cpp, you can add next lines in PanZoomLayer.cpp

	// Get the two first touches
	CCTouch *touch1 = (CCTouch*)_touches->objectAtIndex(0);
	CCTouch *touch2 = (CCTouch*)_touches->objectAtIndex(1);

	// Get current and previous positions of the touches
	CCPoint curPosTouch1 = CCDirector::sharedDirector()->convertToGL(touch1->getLocationInView());
	CCPoint curPosTouch2 = CCDirector::sharedDirector()->convertToGL(touch2->getLocationInView());
	CCPoint prevPosTouch1 = CCDirector::sharedDirector()->convertToGL(touch1->getPreviousLocationInView());
    CCPoint prevPosTouch2= CCDirector::sharedDirector()->convertToGL(touch2->getPreviousLocationInView());
    //======= add next lines to correct scale ===========
    if (touch1->getPreviousLocationInView().x==0 && touch1->getPreviousLocationInView().y==0)
        prevPosTouch1= CCDirector::sharedDirector()->convertToGL(touch1->getLocationInView());
    
    if (touch2->getPreviousLocationInView().x==0 && touch2->getPreviousLocationInView().y==0)
        prevPosTouch2= CCDirector::sharedDirector()->convertToGL(touch2->getLocationInView());