Cocos2dx coordinate system introduction, cocos2dx Coordinate System

Source: Internet
Author: User

Cocos2dx coordinate system introduction, cocos2dx Coordinate System
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.

Screen Coordinate System

Apple's 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.

World coordinate system

The world coordinate system is also called the absolute coordinate system, which is a concept in game development. It establishes a reference framework to describe other coordinate systems. We can use the world coordinate system to describe the positions of other coordinate systems, rather than the larger, external coordinate system to describe the world coordinate system. 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 world coordinate system is consistent with the GL coordinate system. The origin is in the lower left corner of the screen, the X axis is right, and the Y axis is up.

Local Coordinate System

The local coordinate system is also called the object coordinate system, which 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, when taking a taxi and saying "Turn left" to the driver, we use the car's object coordinate system, "before", "after", "Left", and "right" are meaningful only in the object coordinate system. But if we say "going East", we use the world coordinate system. People inside or outside the car know where to drive. The position of CCNode is the local coordinate system of the parent node. It is consistent with the GL coordinate system. The X axis is right, the Y axis is up, and the origin is in the lower left corner of the parent node. If the parent node is the top-level node in the scenario tree, the local coordinate system used by the parent node will overlap with the world coordinate system. There are several convenient functions in the CCNode object that can be used for Coordinate Transformation: The convertToWorldSpace method can be used to convert the coordinates in the Local Coordinate System of 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. Note that these methods are converted based on the coordinates of the current node, while the position coordinate of a node is based on the local coordinates of its parent node, therefore, we should call the convertToWorldSpace function [node. parent convertToWorldSpace: [node position]. Almost all game engines use the local coordinate system instead of the world coordinate system to specify the position of elements, the advantage of this is that when an object motion is calculated, the elements in the same local coordinate system can be calculated independently as a sub-system, and then the motion of the coordinate system can be added, this is a common concept in Physical Research. For example, if a person jumps up or down in a driving carriage, we only need to calculate his position in the carriage coordinate system when drawing each frame, then, the location of the car can be used to calculate the position of the person in the world coordinate system. If a single world coordinate system is used, the Human Motion Track becomes more complex.

Anchor

Each CCNode has an anchor point. The anchor point specifies the position where the texture and the node's origin point (that is, the point indicated by position) overlap, therefore, the anchor is meaningful only when texture is used for the node. The default value of the anchor is (0.5, 0.5). It indicates not a pixel, but a multiplier factor. (0.5, 0.5) indicates that the anchor is located in the center of texture, where the texture length is multiplied by 0.5 and the width is multiplied by 0.5. Changing the value of the anchor does not change the position of the node. Although it may seem that the image position of the node has changed, the change is only the position of texture relative to the position, it is equivalent to texture in a mobile node, not the node itself. If you set the anchor to (0, 0), the bottom-left corner of texture will overlap with the position of the node, which may make it easier to locate the element, but it will affect the scaling and rotation of elements and a series of transformations, so we do not recommend this. Therefore, when the anchor is the default value (0.5, 0.5), place a genie in the center of the bottom of the screen. Set the position as follows:

[Plain]View plaincopy
  1. CGSize screenSize = [[CCDirector shareddire] winSize];
  2. Float imageHeight = player. contentSize. height;
  3. Player. position = CGPointMake (screenSize. width/2, imageHeight/2 );
Affine Transformation

In general, the game will use a lot of affine transformations, such as rotation, scaling, and translation (the so-called affine transformation refers to the addition of translation on the basis of linear transformation, translation is not a linear transformation ). In 2D computer graphics, the affine transformation is usually achieved by multiplying the 3x3 homogeneous matrix. The affine transformation in cocos2d uses the CGAffineTransform class in Quartz 2D for representation:

[Cpp]View plaincopy
  1. Struct CGAffineTransform {
  2. CGFloat;
  3. CGFloat B;
  4. CGFloat c;
  5. CGFloat d;
  6. CGFloat tx;
  7. CGFloat ty;
  8. };
  9. Typedef struct CGAffineTransform;
The following is the homogeneous matrix represented by the CGAffineTransform class. Since the last column of the transformation matrix is always [0, 0, 1], it is omitted:


Since OpenglES is used for cocos2d rendering, CGAffineTransform is only used to represent 2D affination transformations. In the end, it is still necessary to convert it into a 4x4 transformation matrix of OpenglES (Opengl is a 3D world, therefore, it accepts a homogeneous matrix of 4x4 ). The conversion is completed by CGAffineToGL (const CGAffineTransform * t, GLfloat * m). The transformation matrix of Opengl is represented by a one-dimensional array. The ing relationship between it and CGAffineTransform is as follows:

[Plain]View plaincopy
  1. | M [0] m [4] m [8] m [12] | m11 m21 m31 m41 | a c 0 tx |
  2. | M [1] m [5] m [9] m [13] | m12 m22 m32 m42 | B d 0 ty |
  3. | M [2] m [6] m [10] m [14] | <=> | m13 m23 m33 m43 | <=> | 0 0 1 0 |
  4. | M [3] m [7] m [11] m [15] | m14 m24 m34 m44 | 0 0 0 1 |

PS about the 3x3 homogeneous matrix:
The transformation matrix (right multiplication) of points [x, y] and 2x2 on the 2D plane represents linear transformation without translation. This is easy to understand: according to the nature of matrix multiplication, zero vectors are always transformed into zero vectors, so any transformation expressed by matrix multiplication does not contain translation. In fact, translation is a matrix addition instead of a matrix multiplication. The introduction of homogeneous coordinate system is a mathematical technique. By combining multiplication and addition in matrix operations, the transformation matrix can process translation. Homogeneous coordinates are represented by an n + 1-dimension vector, which is originally an n-dimensional vector. The homogeneous representation of 2D vectors [x, y] is [x, y, h], and h is called homogeneous factor, which can be any non-zero value, therefore, the homogeneous representation of a vector is not unique. For example, the homogeneous coordinates [8, 4, 2] and [4, 2, 1] represent 2D points [4, 2]. H = 0 can be understood as an infinitely far point. It is not difficult to find that the translation is invalid if [x, y, 0] multiplied by 3x3 Translation transformation matrix. In other words, the h component has the function of switching the translation part of the 3x3 matrix. This phenomenon is very useful. Although the mathematical representation is the same, in the geometric sense, vectors and points are completely different: vectors only represent directions, no locations, and vertices represent positions, [x, y] can represent a vector or a point (when it represents a point, it actually refers to the displacement relative to the origin ). Points can be translated, but vectors cannot. Therefore, in the geometric sense, [x, y, h] indicates a point, when h = 0, it indicates a vector.


Cocos2dx is a suitable game type for development

Software.intel.com/..os2d-x
2d game development, online games, or standalone
 
Which company developed cocos2dx?

This is developed by an open-source community and is not developed by any company.
Go to its homepage and check the bottom line.
"Cocos2D-x open source developer community"
 

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.