Talking about the coordinate system of game in "Cocos2d-x game development"

Source: Internet
Author: User

Whether developing 2D or developing 3D games, the concept of a coordinate system must first be clarified. In Cocos2d-x, you need to understand the OpenGL coordinate system, the world coordinate system, and the node coordinate system.

1.UI coordinate system

 The default start-point coordinates (x=0,y=0) of the common UI coordinate system in the Ios/android/windows SDK are located in the upper corner, with the x-axis starting from the far left of the screen, increasing from left to right, and the y-axis starting from the top of the screen, increasing from top to bottom:

The UI coordinate system is also called the texture coordinate system, and in cocos2d-x, this coordinate system is used only when a portion of the rectangle is truncated from the texture, such as the Texturerect property of the sprite.

2.OpenGL coordinate system

The OpenGL coordinate system, also known as the drawing coordinate system, is based on OpenGL and OpenGL ES, so the cocos2d-x is supported by the OpenGL coordinate system. OpenGL coordinate system default origin coordinates (x=0,y=0) in the lower left corner of the screen, the X-axis starts from the far left of the screen and increases from left to right, and the Y-axis starts from the bottom of the screen and increases gradually from bottom to top.

The default origin of the iOS device's screen coordinate system (that is, the UI coordinate system) is the upper-left corner, the x-axis to the right, and the y-axis downward (the y-axis and OpenGL coordinates are just opposite). iOS touchscreen touch Time touch incoming location information uses a screen coordinate system, so when processing the touch time in cocos2d-x, you need to first convert the position of the touch point to the OpenGL coordinates. You can use the CONVERTTOGL function in the Director class to convert.

3. World coordinate system

The world coordinate system, also known as the absolute coordinate system, is a concept in game development, and it establishes the reference standards needed to describe other coordinate systems. We can use the world coordinate system to describe the position of other coordinate systems.

The element structure in Cocos2d-x is a hierarchical structure with a parent-child relationship, and the location of the position setting element through node uses the local coordinate system relative to its parent node, not the world coordinate system. Finally, when drawing the screen, cocos2d-x maps the local node coordinates of these elements into world coordinate coordinates. The world coordinate system is consistent with the OpenGL coordinate system, which defaults to the lower left corner of the screen, the x-axis to the right, and the y-axis upward.

4. Node coordinate system

A node coordinate system, also known as a local coordinate system, is the coordinate system associated with a particular node. Each node has a separate coordinate system. When a node moves or changes direction, the coordinate system (its child nodes) associated with that node will also move or change direction. This is relative.

For example, when taking a taxi, the driver said "turn left", using a node (taxi) coordinate system, "front", "Back", "left", "right" only in the relative to the taxi's coordinate system is meaningful. But if we say "eastward", we are using the world coordinate system, whether in the car or outside the people know what direction to open.

5.VEC2 and Point

  In the cocos2d-x3.0 version, a point is used to represent a coordinate dot, which contains an x and a y-coordinate, all floating-point numbers. Starting with Cocos2d-x version 3.1, VEC2 is used instead of point, and Point as a typedef exists, as defined in the Vec2.h source code:

typedef VEC2 Point;

6. Anchor Point

Anchor points are a very important concept in cocos2d-x, each node has an anchor point (anchor Point), which specifies the location of the point where the texture image and the origin of the node (that is, the point represented by position) are coincident. The anchor value ranges from (0,0) to (to), by default, the anchor is located in the geometric center of the texture Image (0.5,0.5), which represents a multiplier factor instead of a pixel point. (0.5,0.5) indicates that the anchor point is at the center of the texture image, where the length of the texture image is multiplied by 0.5. Therefore, the anchor point only makes sense if the node class nodes use texture images.

The most important role of the anchor point is the interface layout and positioning of the auxiliary node. You can think of an anchor point as using a pushpin to fix a photo on a blank wall, where the pinned photo is the equivalent of a node, and the wall is the device's screen, and the Pushpin is the anchor point. In Cocos2d-x, the default position of the anchor point is in the geometric center position of the texture image. For example, when you set the location of an sprite image (50,50), the geometric center position of the sprite image should be (50,50) by default. However, if you set the anchor point to the lower-left corner of the sprite image (0,0) and set the sprite image position to (50,50), it should be at (50,50) position in the lower-left corner of the sprite image, not in the location of the geometric center of the sprite Image (50,50).

Node's position uses the parent node's coordinate system, which is consistent with the OpenGL coordinate system, the x-axis to the right, the y-axis upward, and the origin by default in the lower-left corner of the parent node. If the parent node is the top-level node in the scene tree, the node coordinate system it uses is coincident with the world coordinate system.

The node class has two convenient functions that can be used for coordinate transformations:

*converttonodespace: Converts the world coordinates to the node coordinates of the current node.

*converttoworldspace: The coordinates of the node coordinate system based on the current node are converted to world coordinates.

It is important to note that both conversions are based on the current node coordinates and do not take into account the anchor point, whereas the coordinates used by the position of a node are based on the coordinates of its parent node, so We're going to switch the position of node to the Converttoworldspace function in the world coordinate system where the parent node should be called. The node class also provides two functions for converttonodespacear and converttoworldspacear. The two functions perform the same conversion function, but they are based on the coordinate anchor point.

Understand and master the above several basic coordinate system knowledge, can use Cocos2d-x to develop the game efficiently and quickly.

Talking about the coordinate system of game in "Cocos2d-x game development"

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.