Cocos2d-x development game coordinate system knowledge introduction

Whether you are developing 2D games or developing 3D games, you must first understand the concept of coordinate systems. In Cocos2d-x, you need to know the OpenGL coordinate system, the world coordinate system, and the nodal coordinate system.

1.UI coordinate system

The default origin coordinates (x=0, y=0) of the common UI coordinate system in the IOS/Android/Windows SDK are located at the upper corner, and the x-axis starts from the leftmost side of the screen and gradually increases from left to right; the Y-axis coordinates are from the screen Start at the top and gradually increase from top to bottom as shown in the figure:

Cocos2d-x development game coordinate system knowledge introduction

The UI coordinate system is also called the texture coordinate system. In Cocos2d-x, this coordinate system is used only when a partial rectangle is cut from the texture, such as the Sprite's TextureRect property.

2. OpenGL coordinate system

The OpenGL coordinate system is also called the drawing coordinate system. Cocos2d-x is based on OpenGL and OpenGL ES, so the OpenGL coordinate system is naturally supported. The default origin coordinates (X=0, Y=0) of the OpenGL coordinate system are in the lower left corner of the screen. The X axis starts from the leftmost side of the screen and gradually increases from left to right. The Y axis starts from the bottom of the screen and gradually increases from bottom to top.

The IOS device's screen coordinate system (ie, the UI coordinate system) defaults to the upper left corner, X axis to the right, Y axis down (Y axis and OpenGL coordinates are just opposite). IOS screen touch time Touch input location information is the screen coordinate system, so when dealing with touch time in Cocos2d-x, you need to first convert the location of the touch point to OpenGL coordinates, you can use the convertToGL Director category Functions to convert.

3. World coordinate system

The world coordinate system, also known as the absolute coordinate system, is a concept in game development. It establishes 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. The position of the element set by Node's posiTIon uses the local coordinate system relative to its parent node, not the world coordinate system. Finally, when drawing the screen, Cocos2d-x will map the local node coordinates of these elements to world coordinate coordinates. The world coordinate system and the OpenGL coordinate system are the same. The default point of origin is at the lower left corner of the screen. The X axis is right and the Y axis is upward.

4. Node coordinate system

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

For example, when you take a taxi, you say “turn left” to the driver and use the node (taxi) coordinate system. The “front”, “back”, “left”, and “right” are only relative to the coordinates of the taxi. Department makes sense. But if we say "open eastward," we are using the world coordinate system. People in and out of the car know where they should go.

5.Vec2 and Point

In Cocos2d-x3.0 version, use Point to represent a coordinate point, Point contains an x ​​and a y coordinates, are floating point numbers. Starting from Cocos2d-x version 3.1, Vec2 is used instead of Point, and Point exists as a typedef, which is defined in the Vec2.h source code:

Typedef Vec2 Point;

6. Anchor

Anchors are a very important concept in Cocos2d-x. Each Node has an anchor point, which specifies the position of the point where the texture image and the origin of the node (the point represented by posiTIon) coincide. The anchor value ranges from (0,0) to (1,1). By default, the anchor point is at the geometric center of the texture image (0.5, 0.5). This value does not represent a pixel point. But a multiplier factor. (0.5, 0.5) means that the anchor point is located at the length of the texture image multiplied by 0.5, which is the center of the texture image. Therefore, the anchor point makes sense only if the Node class node uses a texture image.

The biggest role of the anchor is to assist the node in positioning the interface. The anchor can be thought of as using a push pin to fix a photo on a blank wall. The pinned photo is equivalent to the node, the wall is the screen of the device, and the push pin is the anchor. In Cocos2d-x, the anchor's default position is at the geometric center of the texture image. For example, when the position of a sprite image is set to (50, 50), the sprite image's geometric center position should also be (50, 50) by default. But if you set the anchor point in the lower left corner (0,0) of the sprite image and set the position of the sprite image to (50,50), then the lower left corner of the sprite image should be at (50,50). Instead of the geometry center of the sprite image, it is in the (50,50) position.

Node's posiTIon uses the parent's node coordinate system, which is also consistent with the OpenGL coordinate system. The X axis is right and the Y axis is on. The origin is at the left lower corner of the parent node by default. If the parent node is the top node in the scene tree, then the node coordinate system it uses coincides with the world coordinate system.

The Node class has two convenient functions for coordinate conversion:

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

*convertToWorldSpace; Converts the coordinates in the nodal coordinate system based on the current node to world coordinates.

It should be noted that these two conversions are based on the current node coordinates, and do not consider the anchor point, and the coordinates used by a node's posiTIon are based on the coordinates of its parent node. Therefore, we must convert the position of the Node. The parent node's convertToWorldSpace function should be called in the world coordinate system. The Node class also provides convertToNodeSpaceAR and convertToWorldSpaceAR functions. These two functions perform the same conversion function, but they are based on coordinate anchors.

Understanding and mastering the knowledge of the above basic coordinate systems enables efficient and efficient use of Cocos2d-x to develop games.

AMD Gaming PC

Amd Gaming Pc,Gaming Computer,Gaming Pc I7,Pc Gamer

Shenzhen Innovative Cloud Computer Co., Ltd. , https://www.xcypc.com

Posted on