(1) 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 located in the lower left corner of the screen, with the X axis being right and the Y axis being up.
(2) screen Coordinate System
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.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 a Cocos2D-x, you must first convert the touch point to the OpenGL coordinate system. This will be detailed in the following touch screen information. You can use the converttogl method of ccdirector to complete this conversion.
In the callback method for processing touch events, we often encounter the transformation and processing problems between the two coordinate systems. In the cctouch file, we have encapsulated the coordinates of the touch point in the OpenGL and screen coordinate systems.
① Obtain coordinates from the touch point in the screen Coordinate System
// Returns the current Touch Location in screen coordinates
Ccpoint cctouch: getlocationinview () const
{
Return m_point;
}
② Obtain coordinates from the touch point in the OpenGL Coordinate System
// Returns the current Touch Location in OpenGL coordinates
Ccpoint cctouch: getlocation () const
{
Return ccdirector: shareddirector ()-> converttogl (m_point );
}
(3) 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 reference standards required to describe other coordinate systems. 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.The ccnode is used to set the position to use the local coordinate system relative to its parent node.Instead of the 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. The world coordinate system is in the same direction as the OpenGL coordinate system. The origin is in the lower left corner of the screen, the X axis is right, and the Y axis is up.
(2) 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 coordinate system (ITS subnode) associated with the node moves or changes its direction. All of this is relative. Compared with the benchmark, it is only meaningful 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 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 node coordinate system used by the parent node will overlap with the world coordinate system.
(4) anchor
The anchor specifies the position of the point that coincides with the origin point of the node (that is, the point where the position is set). Therefore, the anchor makes sense only when a ccnode node uses a texture.
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 point where the texture length is multiplied by 0.5 and the width is multiplied 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 relative position of the texture relative to the position you set is changed, it is equivalent to the texture in the moving node, not the node itself. If you set the anchor point to (0, 0), the lower-left corner of the texture will overlap with the node location, which may make element positioning more convenient, but will affect a series of transformations such as element scaling and rotation. Therefore, there is no specific set of anchor points, which should be defined based on your usage of this object. In the Cocos2D-x, the anchor is the default value (0.5, 0.5), and such an anchor setting places a node in the center of the texture.
Coordinate System Problems in Coco2d-x