Cocos2dx coordinate system details

Source: Internet
Author: User

1. In cocos2dx, the following coordinate systems are supported:

● Screen coordinate system: the origin is in the upper left corner, the X axis is right, and the Y axis is down. (For example, when processing a touch event, the coordinates in the CCTouch object are the screen coordinate system)

● OpenGL coordinate system: the origin is in the lower left corner, the X axis is right, and the Y axis is up. (For example, the setPosition function call of the CCNode class is the coordinate system)

● World coordinate system: refers to the coordinate system (0, 0) relative to the whole screen, which is the lower left corner of the screen.

● Local coordinate system: Coordinates relative to the parent object.


2. AnchorPoint)


● In one sentence, we can refer to the anchor as nailing a piece of paper or photo to the point on the wall.


● The value range of x and y of the anchor is [0, 1.


● The default anchor of Sprite is (0.5, 0.5). The anchor is not ignored by default.

Virtual bool isIgnoreAnchorPointForPosition (); // false is returned by default.

● For other nodes such as CCLayer and CCSense, the default value of the anchor is (0, 0). The default value is ignore anchor.

Virtual bool isIgnoreAnchorPointForPosition (); // returns true by default.


Therefore, if you want to set the anchor in CCLayer and CCSense, you need to set the following

CCLayer * layer1 = CCLayerColor: create (ccc4 (0,255, 300,200),); addChild (layer1); layer1-> ignoreAnchorPointForPosition (false); // you can specify not to ignore the anchor, at this time, the anchorpoint will change to the default (0.5, 0.5) layer1-> setAnchorPoint (ccp (); // then set the custom anchor here




<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + release + MyDX + release + rPuL3iys3I58/Co6zTprjD0tG + release "brush: java;"> CCSprite * sprite1 = CCSprite :: create ("CloseNormal.png"); CCLOG ("sprite1 = (% f, % f)", sprite1-> getContentSize (). width, sprite1-> getContentSize (). height); sprite1-> setAnchorPoint (ccp (1, 1); sprite1-> setPosition (ccp (20, 40); CCSprite * sprite2 = CCSprite: create ("CloseNormal.png "); sprite2-> setPosition (ccp (-5,-20); sprite2-> setAnchorPoint (ccp (1, 1 )); CCPoint point1 = sprite1-> convertToNodeSpace (sprite2-> getPosition (); // use the lower left corner of sprite1 as the reference point (0, 0) to locate the coordinates of the sprite2 anchpoint, return CCPoint point2 = sprite1-> convertToWorldSpace (sprite2-> getPosition (); // locate (-5, 20) with the reference point () in the lower left corner of sprite1) the vertex is the coordinate of the sprite2 anchor, and the CCPoint point3 = sprite1-> convertToNodeSpaceAR (sprite2-> getPosition () is returned. // use the sprite1 anchor as the reference point (0, 0 ), locate the position of the sprite2 anchor and return CCPoint point4 = sprite1-> convertToWorldSpaceAR (sprite2-> getPosition (); // use the sprite1 anchor as the reference point (0, 0 ), locate (-5, 20) The point is the sprite2 anchor coordinate and return CCLOG ("point1 = (% f, % f)", point1.x, point1.y ); CCLOG ("point2 = (% f, % f)", point2.x, point2.y); CCLOG ("point3 = (% f, % f)", point3.x, point3.y ); CCLOG ("point4 = (% f, % f)", point4.x, point4.y );


The output result is as follows:

Sprite1 = (40.000000, 40.000000) (image size)
Point1 = (15.000000,-20.000000)
Point2 = (-25.000000,-20.000000)
Point3 = (-25.000000,-60.000000)
Point4 = (15.000000, 20.000000)




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.