Coordinate System in cocos2dx

Source: Internet
Author: User
Tags addchild togl

1. UI coordinate system and GL Coordinate System

2. Local coordinates and world coordinates

The local coordinate is a relative coordinate, which is relative to the parent node or the relative position of a node you specify. The origin of the local coordinate is in the lower left corner of the reference node.

The world coordinate is an absolute coordinate. It is based on the coordinate origin in the lower left corner of the screen and overlaps with the GL coordinate.

3. UI coordinates and GL coordinates and node coordinates

Ui coordinates are calculated in the UI coordinate system, also known as screen coordinates. the Y axis is down. In some games, you need to use screen coordinates to convert GL coordinates.

The GL coordinate is the world coordinate. It is an absolute coordinate, and the Y axis is up.

Node coordinates are node coordinates, also known as local coordinates. They are calculated based on the parent node or the origin point in the lower left corner of the reference node.

4. Related conversion functions:

Ccdirector: shareddirector ()-> converttoui (); // convert to screen coordinates
Ccdirector: shareddirector ()-> converttogl (); // convert to world coordinates

Converttonodespace (); // convert to local coordinates, also called node coordinates
Converttoworldspace (); // converts it to the world coordinate, also known as the GL Coordinate

5. Code processing:

. H file

# Ifndef _ t05coordinate_h __# DEFINE _ t05coordinate_h __# include "cocos2d. H "using_ns_cc; Class t05coordinate: Public cclayer {public: static ccscene * scene (); create_func (t05coordinate); bool Init (); /* Touch event processing function */virtual bool cctouchbegan (cctouch * ptouch, ccevent * pevent);}; # endif

. Cpp File

# Include "t05coordinate. H "/* create scenario */ccscene * t05coordinate: Scene () {ccscene * scene = ccscene: Create (); t05coordinate * layer = t05coordinate: Create (); scene-> addchild (layer); Return scene;} bool t05coordinate: Init () {cclayer: Init (); settouchenabled (true ); // open the touch switch settouchmode (kcctouchesonebyone); // set the single point of touch/* Create an empty genie as the background */ccsprite * big = ccsprite: Create (); /* set the color */big-> setcolor (ccred); big-> setanchorpoint (CCP (0, 0 )); /* set the texture size */big-> settexturerect (ccrectmake (0, 0,150,150); big-> setposition (CCP (100,100); addchild (BIG ); ccsprite * Little = ccsprite: Create (); little-> setcolor (ccyellow); little-> setanchorpoint (CCP (0, 0 )); little-> settexturerect (ccrectmake (0, 0, 50, 50); little-> setposition (CCP (100,100); // reference the parent class object, set to the position of CCP (100,100) Big-> addchild (little);/* print the UI coordinates, also known as local coordinates, which is relative to its parent class object, is a relative coordinate */cclog ("little X = % F, y = % F", little-> getpositionx (), little-> getpositiony ()); /* the parent class converts the subclass object to the world coordinate */ccpoint toworld = big-> converttoworldspace (little-> getposition (); cclog ("toworld x = % F, y = % F ", toworld. x, toworld. y); ccsprite * little2 = ccsprite: Create (); little2-> setcolor (ccgreen); little2-> setanchorpoint (CCP (0, 0 )); little2-> settexturerect (ccrectmake (0, 0, 50, 50); little2-> setposition (CCP (0, 0); addchild (little2 ); // its parent class object is cclayer, so its position is relative to the whole screen. /* the parent class converts the subclass object to the local coordinate, also known as the node coordinate */ccpoint tonode = big-> converttonodespace (little2-> getposition ()); /* compared with the big object, little2 is equivalent to in the CCP (-100,-100) */cclog ("little2 x = % F, y = % F ", little2-> getpositionx (), little2-> getpositiony (); cclog ("tonode x = % F, y = % F", tonode. x, tonode. y);/* Create A Moving action. By indicates absolute displacement */ccmoveby * by = ccmoveby: Create (2, CCP (200, 0 )); ccmoveby * by2 = (ccmoveby *) by-> reverse (); // turn around, reverse reversal/* create an action sequence */ccsequence * seq = ccsequence based on the action :: create (by, by2, null);/* The genie executes the action * // big-> runaction (ccrepeatforever: Create (SEQ); ccmoveby * LBY = ccmoveby :: create (2, CCP (0,-100); // downward motion ccmoveby * lby2 = (ccmoveby *) LBY-> reverse (); /* Variable Parameter Function ends with null */ccsequence * lseq = ccsequence: Create (LBY, lby2, null); // little-> runaction (ccrepeatforever :: create (lseq); Return true;}/* processing function of the touch event */bool t05coordinate: cctouchbegan (cctouch * ptouch, ccevent * pevent) {cclog ("cctouchbegan");/* getlocation is only used in touch events, and others are getposition */ccpoint PGL = ptouch-> getlocation (); // obtain the world coordinates, it is also called GL coordinate cclog ("Gl: x = % F, y = % F", PGL. x, PGL. y); ccpoint Pui = ptouch-> getlocationinview (); // obtain the UI coordinate. The UI coordinate is also called the screen coordinate system. It can be used to convert cclog ("UI: X = % F, y = % F ", Pui. x, Pui. y); ccpoint toui = ccdirector: shareddire()-> converttoui (PGL); // convert the world coordinates to the UI coordinates cclog ("touix = % F, y = % F ", toui. x, toui. y); ccpoint togl = ccdirector: shareddire()-> converttogl (Pui); // convert the UI coordinates to the world coordinates cclog ("toglx = % F, y = % F ", togl. x, togl. y); ccpoint node = This-> converttonodespace (PGL); // convert the GL coordinate to the node coordinate, also known as the local coordinate cclog ("node: x = % F, y = % F ", node. x, node. y); Return false ;}

 

Coordinate System in cocos2dx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.