A deep understanding of the cocos2d-x Coordinate System

Source: Internet
Author: User

A deep understanding of the cocos2d-x Coordinate System

 

Understanding the coordinate system is an important step to start development. In order not to make everyone dizzy, I will give you an in-depth explanation,

 

First, we add two test genie (width: 27, height: 40) to the scenario:

 

CCSprite *sprite1 = CCSprite::create(player.png);sprite1->setPosition(ccp(20, 40));sprite1->setAnchorPoint(ccp(0, 0));this->addChild(sprite1); CCSprite *sprite2 = CCSprite::create(player.png);sprite2->setPosition(ccp(-15, -30));sprite2->setAnchorPoint(ccp(1, 1));this->addChild(sprite2);


 

After debugging, it is displayed like this in the scenario (in the lower left corner as the coordinate origin, from left to right as the x direction, from bottom to y direction, nonsense :)):

In the cocos2d-x, each genie has an anchor, and subsequent operations on the genie (such as rotation) will be carried out around the anchor. We can now be seen as the central position of the genie, generally, there are three possible values in each direction: 0, 0.5, and 1. The middle-red dots are their respective anches. The sprite1 anchor is () in the lower left corner, and the sprite2 anchor is () in the upper right corner.

Now let's take a look at coordinate system transformation. Similarly, let's first write the point test code:

 

CCPoint p1 = sprite2->convertToNodeSpace(sprite1->getPosition());CCPoint p2 = sprite2->convertToWorldSpace(sprite1->getPosition());CCPoint p3 = sprite2->convertToNodeSpaceAR(sprite1->getPosition());CCPoint p4 = sprite2->convertToWorldSpaceAR(sprite1->getPosition());


 

Then, print the x and y values of each point again:

 

CCLog(p1:%f,%f, p1.x, p1.y);CCLog(p2:%f,%f, p2.x, p2.y);CCLog(p3:%f,%f, p3.x, p3.y);CCLog(p4:%f,%f, p4.x, p4.y);


 

Analyze the converted values of the four common coordinate system conversion functions (if you are interested, calculate the values first ).

Since the coordinate system (local coordinate system) of the cocos2d-x is based on the coordinate origin in the lower left corner, the coordinates of sprite1 and sprite2 are (20, 40), (-42,-70 ), obviously:

P1 is the position of the sprite1 anchor in the sprite2 coordinate system relative to the sprite2 origin. After comparison, we can get (20-(-42), 40-(-70) (62,110)

P2 is the position of the sprite1 anchor in the coordinate system relative to the sprite2 origin, so that we can calculate the position of sprite1 in the sprite2 coordinate system: (20 + (-42), 40 + (-70 )), that is, (-22,-30)

P3 is the position of the sprite1 anchor in the sprite2 coordinate system relative to the sprite2 anchor (20-(-15), 40-(-30), that is (35, 70)

P4 is the position of the sprite1 anchor in the coordinate system relative to the sprite2 anchor, that is, (20 + (-15), 40 + (-30), that is, (5, 10)

Now we can know that the calculation method is to use the sprite1 coordinates to add and subtract the sprite2 coordinates, subtract the local coordinate system, and add the global coordinate system.

 

Well, the method has come out. If you are interested, you can calculate the values of the following coordinates (do not debug the machine first) and then reply to me. After about 10 replies, I will post the correct answer:

CCPoint p1 = sprite1->convertToNodeSpace(sprite2->getPosition());CCPoint p2 = sprite1->convertToWorldSpace(sprite2->getPosition());CCPoint p3 = sprite1->convertToNodeSpaceAR(sprite2->getPosition());CCPoint p4 = sprite1->convertToWorldSpaceAR(sprite2->getPosition());

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.