Cocos-2d coordinate system and coordinate transformation

Source: Internet
Author: User

In the Cocos-2d, four coordinate systems are involved:

The GL coordinate system Cocos2D 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, the X axis is right, and the Y axis is up.


In the screen coordinate system, Apple Quarze2D uses different coordinate systems. The origin is in the upper left corner of the screen, the X axis is right, and the Y axis is 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.

 


The world coordinate system is also called the absolute coordinate system. The world coordinate system is the same as the GL coordinate system, with the origin 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 .)

 


The local coordinate system is also called the object coordinate system. It is the coordinate system associated with a specific object. Each object has its own coordinate system. When the object moves or changes its direction, the coordinate system associated with the object moves or changes its direction. For example, you have created a rectangle colorLayer: CCRect (10, 10, 100,100) with a cocos2d-x, which is the local coordinate system with (10, 10) as the coordinate origin, X axis to the right, Y axis to the top. If a CCSprite is created, the anchor is (0.5, 0.5), the position is (100,100), and the size is (40, 40), then the local coordinate system uses (80, 80) as the coordinate origin, the X axis is right and the Y axis is up.

 


In Cocos-2d, Coordinate System Conversion:

 


CCPoint convertToNodeSpace (const CCPoint & worldPoint );
CCPoint convertToWorldSpace (const CCPoint & nodePoint );
CCPoint convertToNodeSpaceAR (const CCPoint & worldPoint );
CCPoint convertToWorldSpaceAR (const CCPoint & nodePoint );

 

 

1. CCPoint convertToNodeSpace (const CCPoint & worldPoint );


CGPoint nodeSpace = [spriteParentconvertToNodeSpace: orignPosition];

Convert orignPosition to the local coordinate relative to spriteParent

 

2. CCPoint convertToWorldSpace (const CCPoint & nodePoint );


CGPoint wordeSpace = [spriteParentconvertToWorldSpace: orignPosition];

Convert orignPosition to the world coordinate relative to spriteParent

 


3. CCPoint convertToNodeSpaceAR (const CCPoint & worldPoint );


CGPoint nodeSpaceAR = [spriteParentconvertToWorldSpace: orignPosition];

Set the Coordinate System Origin of spriteParent to the position of the spriteParent anchor.
Then orignPosition is converted to the local coordinates relative to spriteParent.

 


4. CCPoint convertToWorldSpaceAR (const CCPoint & nodePoint );


CGPoint wordeSpaceAR = [spriteParentconvertToWorldSpace: orignPosition];

Set the Coordinate System Origin of spriteParent to the position of the spriteParent anchor.

Then orignPosition is converted to the world coordinates relative to spriteParent.

 

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.