Cocos2D CCNode position in absolute screen coordinates

14,473

Every CCNode, and descendants thereof, has a method named convertToWorldSpace:(CGPoint)p

This returns the coordinates relative to your scene.

When you have this coordinate, flip your Y-axis, as you want 0,0 to be in the top left.

CCNode * myNode = [[CCNode alloc] init];
myNode.position = CGPointMake(150.0f, 100.0f);

CCSprite * mySprite = [[CCSprite alloc] init]; // CCSprite is a descendant of CCNode
[myNode addChild: mySprite];
mySprite.position = CGPointMake(-50.0f, 50.0f);

// we now have a node with a sprite in in. On this sprite (well, maybe
// it you should attack the node to a scene) you can call convertToWorldSpace:
CGPoint worldCoord = [mySprite convertToWorldSpace: mySprite.position];

// to convert it to Y0 = top we should take the Y of the screen, and subtract the worldCoord Y
worldCoord = CGPointMake(worldCoord.x, ((CGSize)[[CCDirector sharedDirector] displaySizeInPixels]).height - worldCoord.y);

// This is dry coded so may contain an error here or there.
// Let me know if this doesn't work.

[mySprite release];
[myNode release];

Share:
14,473
Tovi7
Author by

Tovi7

Updated on June 04, 2022

Comments

  • Tovi7
    Tovi7 almost 2 years

    I've been looking around for a while, and I have not been able to find an answer to this for some reason. It seems simple enough, but maybe I just can't find the right function in the library.

    I have a scene with a layer that contains a bunch of CCNodes with each one CCSprite in them.

    During the application, I move around the position of the main layer, so that I "pan" around a camera in a way. (i.e. I translate the entire layer so that the viewport changes).

    Now I want to determine the absolute position of a CCNode in screen coordinates. The position property return the position relative to the parent node, but I really would like this transformed to its actual position on screen.

    Also, as an added bonus, it would be awesome if I could express this position as coordinate system where 0,0 maps to the upper left of the screen, and 1,1 maps to the lower right of the screen. (So I stay compatible with all devices)

    Edit: Note that the solution should work for any hierarchy of CCNodes preferably.

  • Tovi7
    Tovi7 about 13 years
    Ah brilliant. Almost got there on my own, stupid mistake though: Did not take the position of the sprite itself, but rather of the node, which does not work. Apparently. But this is brilliant, as it also takes into account scaling and such. Great. Five stars for you.
  • Jonny
    Jonny about 11 years
    CCDirector does not have displaySizeInPixels in recent versions of cocos2d. Any fix for this? (Found winSizeInPixels, might be it.)
  • gdbj
    gdbj almost 10 years
    I know this is old, but is it correct that I get a worldCoord = (50, 200) (line 6)? I would expect worldCoord = (100, 150). When I step through, I am seeing tx = 100, and ty = 150 as the transform... the axes are switched? Am I missing something obvious?