Cocos2d-x happy to make people happy game 2: large collection of Coordinate Systems

Source: Internet
Author: User

Cocos2d involves four coordinate systems:


  • GL Coordinate SystemCocos2D uses OpenglES as the graphics library, so it uses the OpenglES coordinate system. The GL coordinate system origin is in the lower left corner of the screen,X axis to the right, Y axis to the up.
    • Screen Coordinate SystemApple's Quarze2D uses different coordinate systems, with the origin in the upper left corner of the screen,X axis to the right, Y axis down.
      This coordinate system is used for the position information transmitted by the ios screen touch event CCTouch. Therefore, before responding to a touch event in cocos2d, you must first convert the touch point to the GL coordinate system. You can use convertToGL of CCDirector to complete this conversion.

      • World coordinate systemIt is also called the absolute coordinate system. The world coordinate system is consistent with the GL coordinate system,The origin is in the lower left corner of the screen..
        Elements in cocos2d are hierarchical structures with parent-child relationships. We set the position of an element through CCNode to use the local coordinate system relative to its parent node rather than the world coordinate system. Finally, cocos2d maps the local coordinates of these elements to the coordinates of the world coordinate system.
        • Local Coordinate SystemCoordinates relative to the parent object.
          [Obj. parent convertToWorldSpace: [obj position]; // obtain the obj world coordinate [obj. parent convertToNodeSpace: [obj position]; // obtain the local coordinates of obj [[CCDirector sharedDirector] convertToGL: ***** ()]; // obtain the GL coordinates [[CCDirector shareddire] convertToUI: ***** ()]; // obtain the screen coordinates

          Whether it is 2d or 3d development, the most important thing to understand is the coordinate system. If this is a mess, there will be no rush. So when we play cocos2d, we first need to figure out all the things related to coordinates.
          Two basic coordinate systems: the screen coordinate system and the GL coordinate system.
          The X axis of the screen coordinate system is right-facing, and the Y axis is down. The default origin is in the upper left corner.
          The X axis of the GL coordinate system is right-facing, and the Y axis is facing up. The default origin is in the lower left corner.
          Before calling any function that requires location setting or obtaining location information from the function, you must specify the coordinate system used by the function. For example, to call the setPosition function of the CCNode class, it uses the GL coordinate system. For example, when processing a touch event, the coordinates in the CCTouch object are the screen coordinate system.
          The most commonly used coordinate systems are Node-related local coordinate systems. This structure is the same as that of the scenario tree used in 3D. Therefore, the location obtained from the Node is the local coordinate of the Node. You must use a specific function to convert the local coordinate to the world coordinate. In addition, the coordinates here use the GL coordinate system. There are several convenient functions in the CCNode object for coordinate transformation. The convertToWorldSpace method can convert coordinates in the local coordinate system based on the current node to the world coordinate system. The convertToNodeSpace method can convert the world coordinates to the local coordinate system of the current node.
          In addition, by the way, the click hit problem will involve the mutual conversion of some coordinate systems. At the same time, we have to mention the size of various objects in cocos2d. In cocos2d, The CCNode object has the scaling methods setScaleX and setScaleY. Therefore, when obtaining the object size, you must specify the original size of the object as needed, or the scaled size. Of course, cocos2d provides corresponding functions to complete these operations.
          The getContentSize function is used to obtain the original node size.
          The boundingBox function is used to obtain the size of the box after resizing and rotation.
          A simple example:
          Bool ret = CCRect: CCRectContainsPoint (
          This-> boundingBox (), this-> getParent ()-> convertTouchToNodeSpace (pTouch ));
          The function of this example is to determine whether the current touch operation occurs on your node object. PTouch is the pointer to the CCTouch object, which contains the coordinates of the occurrence points of the current touch event.
          The CCRectContainsPoint function is used to determine whether a vertex is within a rectangle. We want to use this function to determine whether the point of the current touch operation is within the range of the current node.
          This-> boundingBox () method obtains the local coordinate size after the current node object is scaled under the parent node object and is expressed in the GL coordinate system.
          The coordinates in the pTouch object are the screen coordinate system, so they must be converted to the GL coordinate system and then to the local coordinates of the parent node. Fortunately, the convertTouchToNodeSpace function completes the two conversions at a time. You can refer to the source code of the database, which has a specific computing process.
          After all the data is converted to the same coordinate system, you can use the CCRectContainsPoint function to complete the final judgment operation.
          The last point is to use relative coordinates as much as possible. In other words, when setting the size and position of all objects in the program, the size and position of the parent object should be taken as the basis. In this way, you only need to adjust the size of the root object when the program is published at different resolutions.


          By the way, cocos2dx can be used to determine click hits:

          [Cpp]View plaincopy
          1. // Reload
          2. Virtual bool ccTouchBegan (CCTouch * touch, CCEvent * pEvent );
          3. Virtual void ccTouchMoved (CCTouch * touch, CCEvent * pEvent );
          4. Virtual void ccTouchEnded (CCTouch * touch, CCEvent * pEvent );
          5. Virtual void onEnter ();
          6. Virtual void onExit ();
          7. // Add support for touch events
          8. Void CTestLayer: onEnter ()
          9. {
          10. CCLayer: onEnter ();
          11. This-> setTouchEnabled (true );
          12. CCDirector: sharedDirector ()-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );
          13. }
          14. Void CTestLayer: onExit ()
          15. {
          16. CCLayer: onExit ();
          17. CCDirector: sharedDirector ()-> getTouchDispatcher ()-> removeDelegate (this );
          18. }
          19. //////////////////////////////////////// //////////////////////////////////////// ////////////////
          20. // Use your own coordinate system to determine relative to the origin
          21. Bool checkTouchInSelf (CCTouch * touch );
          22. // Use your coordinate system to judge relative to the anchor.
          23. Bool checkTouchInSelf_AR (CCTouch * touch );
          24. // Use the coordinate system of the parent element and its position in the parent coordinate to determine
          25. Bool checkTouchInSelf_Parent (CCTouch * touch );
          26. //______________________________________________________________________________________________
          27. // Use your own coordinate system to determine relative to the origin
          28. Bool CTestLayer: checkTouchInSelf (CCTouch * touch)
          29. {
          30. // Solution 1
          31. // Convert the click point to the coordinates in the coordinate system.
          32. CCPoint pt = convertTouchToNodeSpace (touch );
          33. Printf ("pt. x = %. 1f pt. y = %. 1f \ n", pt. x, pt. y );
          34. Int nw = getContentSize (). width;
          35. Int nh = getContentSize (). height;
          36. CCRect rc (0, 0, nw, nh );
          37. If (rc. containsPoint (pt ))
          38. {
          39. // Obtain the world coordinate value of the clicked OpenGL
          40. CCPoint touchPoint = touch-> getLocation ();
          41. Printf ("ccTouchBegan x = %. 1f y = %. 1f \ n", touchPoint. x, touchPoint. y );
          42. Return true;
          43. }
          44. Return false;
          45. }
          46. //______________________________________________________________________________________________
          47. // Use your coordinate system to judge relative to the anchor.
          48. Bool CTestLayer: checkTouchInSelf_AR (CCTouch * touch)
          49. {
          50. // Solution 2
          51. // Convert the click point to the coordinates in the coordinate system, relative to the anchor.
          52. CCPoint ptAR = convertTouchToNodeSpaceAR (touch );
          53. Printf ("ptAR. x = %. 1f ptAR. y = %. 1f \ n", ptAR. x, ptAR. y );
          54. CCPoint pp = this-> getAnchorPoint ();
          55. Int nw = getContentSize (). width;
          56. Int nh = getContentSize (). height;
          57. Int nx =-(nw * pp. x );
          58. Int ny =-(nh * pp. y );
          59. CCRect rcar (nx, ny, nw, nh );
          60. If (rcar. containsPoint (ptAR ))
          61. {
          62. // Obtain the world coordinate value of the clicked OpenGL
          63. CCPoint touchPoint = touch-> getLocation ();
          64. Printf ("ccTouchBegan x = %. 1f y = %. 1f \ n", touchPoint. x, touchPoint. y );
          65. Return true;
          66. }
          67. Return false;
          68. }
          69. //______________________________________________________________________________________________
          70. // Use the coordinate system of the parent element and its position in the parent coordinate to determine
          71. Bool CTestLayer: checkTouchInSelf_Parent (CCTouch * touch)
          72. {
          73. // Solution 3
          74. // Obtain the world coordinate value of the clicked OpenGL
          75. CCPoint touchPoint = touch-> getLocation ();
          76. // Convert the clicked position to the relative coordinates in the coordinate system of the parent Element
          77. CCPoint pt = getParent ()-> convertToNodeSpace (touchPoint );
          78. Printf ("pt. x = %. 1f, pt. y = %. 1f \ n", pt. x, pt. y );
          79. // Obtain the range of position in the coordinate system of the parent element.
          80. CCRect rect = boundingBox ();
          81. Printf ("rect. l = %. 1f, rect. B = %. 1f, rect. r = %. 1f, rect. t = %. 1f \ n ",\
          82. Rect. getMinX (), rect. getMinY (), rect. getMaxX (), rect. getMaxY ());
          83. // Determine whether the cursor is in its own range. All the above judgments are calculated in the parent element coordinate system.
          84. If (rect. containsPoint (pt ))
          85. {
          86. Printf ("ccTouchBegan x = %. 1f y = %. 1f \ n", touchPoint. x, touchPoint. y );
          87. Return true;
          88. }
          89. Return false;
          90. }

Related Article

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.