Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocos2d coordinate system

in cocos2d, the coordinate system is pretty straightforward.. bottom left is (0,0). whenever i set the position of anything it's fine.

lets say a layer is TouchEnabled.

when im setting the position of my sprite, it's fine..

[crab setPosition:ccp(20, 50)];

but if i get the x and y coordinate of a touch (and assume i clicked on the sprite..):

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches began!");
    NSArray *touchArray = [touches allObjects];
    UITouch *one = [touchArray objectAtIndex:0];
    pointOne = [one locationInView:[one view]];
    CCLOG(@"points are: %f and %f", pointOne.x, pointOne.y );
}

the y coordinate is the opposite of what cocos2d's coordinate system suggests as the y increases from top to bottom !

so im assuming the coordinate system of the view is different than mac's coord system.. which is quite counter intuitive.. is that correct?

thanks !

like image 312
penguinsource Avatar asked Oct 04 '12 06:10

penguinsource


2 Answers

OpenGL uses a (0, 0) of bottom left while iOS and therefore UIKit which handles the touches use top left as (0, 0). It is easy to convert your point over, you just have to remember to do it.

The correct method on cocos2d 2.0 is

CGPoint uiKitPoint = [touch locationInView:touch.view];
CGPoint cocos2dPoint = [[CCDirector sharedDirector] convertToGL:uiKitPoint];
like image 155
Ben Trengrove Avatar answered Sep 30 '22 09:09

Ben Trengrove


This cocos2d tutorial might help you

http://www.gamefromscratch.com/post/2012/06/08/Cocos2D-HTML-Tutorial-3All-about-sprites-and-positioning.aspx

But generally speaking in cocos2d point (0,0) is the left corner of the screen

like image 38
Ilker Baltaci Avatar answered Sep 30 '22 09:09

Ilker Baltaci