y values swapped when displayed

y values swapped when displayed
0.0 0


Hey guys, I am just getting started with Cocos2d-x and airplay. I was looking for exactly this solution, so thanks a lot for your work adding airplay to cocos2d.

The issue I am seeing is that the Y values at which sprites are rendered are different than the Y-values at which touches are registered and at which setPosition() sets them.

For example, if I touch the top left corner of my screen, it registers as 0,0, and I use setPosition() to put a sprite there and output the location of that sprite. At each point in the process, it has the right point data, 0,0.
However, when it displays the sprite, it is in the bottom left corner of the screen, rather than the top left.

Any ideas what might be going wrong?


Wait for Max’s reply :slight_smile:


I figured it out to some extent, but would still appreciate some explanation/clarification.

I ended up having to use “convertToGL()” to get the points to have the right Y-Values. I assume this is because of the difference between Cocos2d-x and the iphone’s axes (Cocos2d-x origin is at the bottom left, and the iphone origin is at the top left) but I am obviously pretty hazy on the exact details. Like, what situations would require the use of convertToGL, convertToUI, and convertToWorldSpace.

Is this the intended way to deal with this issue?


I have the same problem.
starting point in the lower left corner.
how can I fix it?


OpenGL/GL ES origin is at the bottom left, the mobile screen is in the 1st quadrant of rectangular coordinate system. Both cocos2d-iphone & cocos2d-x inherit this coordinate system. It’s different from the common window systems.

Ray Wenderlich’s blog explained this http://www.raywenderlich.com/352/how-to-make-a-simple-iphone-game-with-cocos2d-tutorial

Note that in Cocos2D the bottom left corner of the screen has coordinates of (0,0) and the x and y values increase as you move to the upper right.

I used to call convertToUI & convertToGL in each touch event, but seldom use convertToWorldSpace.

// for multi-touch
void MyLayer::ccTouchesEnded(CCSet* touches, CCEvent* event)
    CCSetIterator it;
    CCTouch* touch;

    for( it = touches->begin(); it != touches->end(); it++) 
        touch = (CCTouch*)(*it);


        CCPoint location = touch->locationInView(touch->view());

        location = CCDirector::sharedDirector()->convertToGL(location);

        // do your work with the "location" param
        addNewSpriteWithCoords( location );