Experience on cocos2d Coordinate System

Source: Internet
Author: User

 

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.

Another important coordinate system is the local coordinate system related to node. 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.

Another key issue is 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 obtainsZoomAfterLocal coordinatesIn 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,ProgramWhen setting the size and position of all objects, 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.

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.