Introduction to Coordinate System
Through the above learning, I believe you have some knowledge about the attributes and methods of the CCNode class, but some terms may confuse you, such as OpenGL coordinates.
System, world coordinate system, node relative coordinate system, and affine transformation. Then we will solve these problems.
① OpenGL Coordinate System
Cocos2D-X is based on OpenGL and OpenGL ES, so it naturally supports OpenGL coordinate system. The origin of the coordinate system is in the lower left corner of the screen and the X axis is displayed.
Right, Y axis up.
The screen coordinate system 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. Transfer the iOS screen touch time to CCTouch
The coordinate system is used for the input position information. Therefore, before responding to the touch event in the Cocos2D-X, You need to first convert the touch point to OpenGL
Standard. This will be detailed in the following touch screen information. You can use the convertToGL method of CCDirector to complete this conversion.
② World coordinate system
The world coordinate system is also called the absolute coordinate system. It is a concept established in game development. Therefore, the world is the game world. It establishes the description of other Coordinate Systems
Required reference standards. We can use the world coordinate system to describe the positions of other coordinate systems. It is a big concept in Cocos2D-X.
Elements in the Cocos2D-X are parent-child hierarchical structures. You can use CCNode to set the position using the local coordinate system relative to its parent node, instead
World coordinate system. Finally, when drawing the screen, the Cocos2D-X maps the local node coordinates of these elements into the world coordinate system coordinates. World coordinate system
Consistent with the OpenGL coordinate system direction, the origin is in the lower left corner of the screen, the X axis is right, and the Y axis is up.
Let's draw out the chaotic Coordinate System for the moment and look at an important concept:Anchor.
③ Anchor
The anchor specifies the position of the point where the texture and the node origin (that is, the point where the position is set) overlap.
In the case of an image, the anchor is meaningful.
The default value of the anchor is (0.5, 0.5), indicating not a pixel, but a multiplier factor. (0.5, 0.5) indicates that the anchor is located at the texture Length
Degrees multiply by 0.5 and width multiply by 0.5, that is, the center of the texture.
Although it may seem that the image position of the node has changed, changing the value of the anchor does not actually change the position of the node. In fact, only the texture map is changed.
Relative to the position you set, it is equivalent to the texture in the moving node, rather than the node itself. If you set the anchor to (0, 0), paste
In the lower left corner of the graph, it will overlap with the node location, which may make element positioning more convenient, but it will affect a series of transformations such as element scaling and rotation. Because
There is no specific set of anchor points, which can be defined according to the usage of your object. In the Cocos2D-X, the anchor is the default
(0.5, 0.5), such an anchor setting should place a node in the center of the texture.
④ Node Coordinate System
The node coordinate system is the coordinate system associated with a specific node. Each node has an independent coordinate system. When a node moves or changes its direction
The coordinated coordinate system (ITS subnode) moves or changes the direction accordingly. All of this is relative. Relative to the benchmark, it is only intended in the node coordinate system.
.
The CCNode class uses the node Coordinate System of the parent node. It is consistent with the direction of the OpenGL coordinate system. The X axis is directed to the right and the Y axis is directed
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 node coordinate system used by the parent node will overlap with the world coordinate system.
There are two convenient functions in the CCNode Class Object for Coordinate Transformation:
<1> convertToWorldSpace: converts the coordinates in the Local Coordinate System of the current node to the world coordinate system.
<2> convertToNodeSpace: coordinates the world to the local coordinate system of the current node.
Both types of conversions do not consider the anchor, and are based on the coordinates in the lower left corner of the parent class of the current node. In addition, CCNode provides
ConvertToWorldSpaceAR and convertToNodeSpaceAR. These two methods perform the same function, but their reference coordinates are based on coordinates.
. Almost all game engines use a similar local coordinate system instead of a world coordinate system to specify the position of an element.
The advantage of this is that when an object motion is calculated, the elements in the same local coordinate system can be independently calculated as a sub-system.
With the motion of the coordinate system, this is a common concept in Physical Research. For example, a person in a driving carriage jumps up or down only needs to draw each frame
When calculating his position in the carriage coordinate system, and then adding the location of the car can calculate the location of the person in the world coordinate system. If you use a single world
In the bounded coordinate system, the Human Motion Track becomes more complex, and involves the synthesis and decomposition of the learned motion track.
⑤ Affine Transformation
In the end, we will introduce the affine transformation. The rotation, scaling, and translation of games are all affinsic changes. The so-called affine changes refer to the basis of linear transformation.
Add translation on. Translation is not linear translation.
In two-dimensional computer graphics, the affine transformation is usually achieved by multiplying the 3x3 homogeneous matrix. The definition of the CCAffineTransform struct of the Cocos2D-X is shown in the code list.
A, B, c, d, tx, and ty are defined, and their positions in the matrix are shown in.