First of all, we must understand what the anchor is.
Referring to the “cocos2d-x authoritative guide”, some of the statements have been changed to words that I understand better.
The appearance of the anchor point is for the service of the sprite picture sprite. The anchor point specifies the position of the point on the sprite that coincides with the position set in the parent node coordinate system (the original sentence is: The anchor point specifies the point on the sprite that coincides with the origin of the node (that is, the point where the position is set).), so the anchor point is meaningful only when the Node node class uses a texture.
The Anchor Point of CCLayer in Cocos2d-x is the default value (0, 0), and the default value of other Nodes is (0.5, 0.5), which represents a multiplier factor. It means that the anchor point is at the center of the texture by multiplying the length of the texture by 0.5 and the width by 0.5.
Changing the value of the anchor point does not change the position of the node, but only the relative position of the texture relative to the setting, which is equivalent to the texture in the mobile node, not the node itself.
Attention! ! Understand again: when a child node is added to a parent node, it refers to the coordinate origin of the lower left corner of the parent node instead of the anchor point. Changing the anchor point in a node does not affect the origin of this node. But the position of the child node in the parent node is related to the anchor point of the child node, that is, the anchor point of the child node coincides with the origin of the parent node. If you don’t understand it, you must write a program! ! When adding a node to the parent node, you need to set its position on the parent node, essentially setting the node’s anchor point in the parent coordinate system.
convertToWorldSpace to world coordinates
Vec2 convertToWorldSpace(const Vec2 &nodePoint) const
Converts a Vec2 to world space coordinates.
The result is in Points.
Parameters
nodePoint A given coordinate.
Returns
:A point in world space coordinates.
< p>The documentation of cocos is written too concisely, and I am still very confused after reading it.
First of all, we have to figure out whether to transform the coordinates of the node object or the coordinates represented by the parameters passed into the function. Generally use this member function to transform the incoming coordinates into the world coordinate system through a certain node coordinate. Assuming that the role of the anchor is already clear (well, if you don’t know, then look back at the above). The following 4 sets of tests are used to analyze this function in detail.
Preparation work, create two picture sprites A and B. The initial size, position and anchor point are set as follows
auto A = Sprite::create("red.png") ;
auto B = Sprite::create("green.png");
A->setContentSize(Size(300,200));
B->setContentSize(Size( 200,100));
A->setAnchorPoint(Vec2(0,0));
B->setAnchorPoint(Vec2(1,1));
A->setPosition(300,300);
B->setPosition(200,200);
Test 1: B–>convertToWorldSpace(Vec2(10,10) )
Node A is not used here), pass in Vec2(10,10) for conversion.
Vec2 position1 = B->convertToWorldSpace(Vec2(10,10));
log("World Position = (%f,%f)",position1.x,position1.y);
The case where the anchor point of B is set to (1,1):
Output World Position = (10.000000,110.000000)
Set the anchor point of B to (0,0):
Output World Position = (210.000000,210.000000)
In one case, (again, the coordinates of the origin of the node coordinate system are always in the lower left corner and will not change!), the origin of node B in the world coordinate system is (0, 100), (0, 100) + (10, 10) = (10,110). In the second case, the origin of B is (200, 200) in the world coordinate system, and the result can be obtained by adding the same principle as above.
Why is addition? Decompose this function: ①Pass in the coordinates you want to change, ②With a certain node coordinate system, put the incoming coordinates in the node coordinate system of B, which is the coordinate system with the lower left corner of B as the origin. ③ Converted to the world coordinate system, the incoming coordinates in the node coordinate system of B are the coordinates he passed in. When zooming in to the world coordinate system, this coordinate plus the coordinates of the B origin in the world coordinate system is Pass in the coordinates of the coordinates in the world coordinate system, and draw a picture of this process to understand!
The coordinates passed in in the first step can be a direct point like the above, or the coordinates obtained through a certain node, such as the coordinates returned by A–>getPosition() (I have seen many bloggers When explaining that by passing in this kind of coordinates, the two nodes must be kneaded together, and each has its own explanation. For example, the anchor point of A is transformed relative to the origin of B; A is translated by B, etc. , I’m still going to explain it by myself in the end, please see below).
Test 2: B–>convertToWorldSpace(A–>getPosition()), B and A are sibling nodes
Vec2 position1 = B->convertToWorldSpace(A->getPosition ());
B anchor point setting (1, 1), A anchor point setting (0, 0):
Output World Position = (300.000000,400.000000)
B anchor If the point remains unchanged, the A anchor point is changed to (1,1):
Output World Position = (300.000000,400.000000)
The B anchor point is changed to (0,0), and the A anchor point is still (0 , 0):
Output World Position = (500.000000,500.000000)
In the example given, the parent node of node A and B is the scene node, and its coordinate system coincides with the world coordinate system.
We see that changing the anchor point of reference node A has no effect, while changing the anchor point of B results in a change. Since what we are passing in is only a Vec2 type of quantity, although it is obtained through the A node, it has little relationship with the A node. Because the position of the A node in its parent node has been determined from the beginning: (200, 200), and we said that the anchor point is a point that coincides with the position set in the parent node, no matter how you change the anchor point, it will not Possibly change this position.
What others said①: This transformation can be regarded as the position of the anchor point of A relative to the origin of B in the world coordinate system. This statement is not correct. If the conversion relationship is like this, the result should be the relative position of the origin of node B and the anchor point of node A, then it is the result of subtracting the origin of node B from the coordinates of the anchor point of node A. In the above case, the origin of B is at (0, 100), the anchor point of node A is at (300, 300), and the output result is (300, 400), which is obviously subtraction rather than addition. The same is true for the second and third cases.
What others say ②: A point can be regarded as a vector, and the coordinate system of node B is derived from the displacement of the coordinate system of its parent node. And A refers to the B node and performs the same displacement, that is, at the A vector + B position. The result is the result after displacement. This explanation can be. However, we might as well consider the incoming parameters as a single point instead of connecting to two nodes (if there is a problem with this understanding, please correct me!).
Test 3: B–>convertToWorldSpace(A–>getPosition()), B is a child node of A
auto A = Sprite::create("red.png ");
auto B = Sprite::create("green.png");
A->setContentSize(Size(300,200));
B->setContentSize( Size(200,100));
A->setAnchorPoint(Vec2(0,0));
B->setAnchorPoint(Vec2(1,1));
A->setPosition(300,300);
B->setPosition(200,200);this->addChild(A, 0);A->addChild(B);Vec2 position1 = B->convertToWorldSpace(Vec2( 0,0));log("World Position = (%f,%f)",position1.x,position1.y);
Result: World Position = (300.000000,400.000000)
Because B is a child node of A, set the position of B (200, 200) with the origin of the lower left corner of A (300, 300), and set the anchor point of B to (1, 1), so it is relative to the node of node A The origin of coordinate system B is at (0, 100), but in the world coordinate system it is (300, 300) + (0, 100) = (300, 400), the incoming point is (0, 0), which is equivalent to Not added, so the final result (0, 0) + (300, 400) = (300, 400)
Vec2 position1 = B->convertToWorldSpace(A->getPosition());
Result: B Node World Position = (600.000000,700.000000)
As calculated above, the origin position of B is (300, 400) in the world coordinate system, due to the obtained AgetPosition() The obtained coordinates are the coordinates (300, 300) set relative to the parent node at the beginning, then the result is the sum of these two.
The situation of changing the anchor point is the same as in Test 2. As long as you think as in Test 1, whether it is the anchor point change of A or B, it can be easily calculated.
Test 4: B–>convertToWorldSpace(A–>getPosition()), A is a child node of B
this->addChild(B, 0);
B->addChild(A, 0);
Vec2 position1 = B->convertToWorldSpace(origin);
log("World Position = (%f,%f)",position1.x, position1.y);
Result World Position = (0.000000,100.000000)
B is the parent node of A, and the origin of B is at coordinates (0, 100) in the world coordinate system , I won’t repeat it later.
Vec2 position1 = B->convertToWorldSpace(A->getPosition());
Result B Node World Position = (300.000000,400.000000)
B’s The origin is (0, 100) in the world coordinate system, A is a child node of B, the coordinates (300, 300) are set in the node coordinate system of B, and the anchor point of B is set to (0, 0), then B’s The anchor point coordinates coincide with its origin, the position obtained by AgetPosition() is relative to the coordinates (300, 300) set by its parent node, and the final result is the coordinates of the origin of A in the world coordinate system (0, 100)+ (300, 300) = (400, 400)
The situation of changing the anchor point will not be repeated.
convertToNodeSpace to node coordinates
Vec2 convertToNodeSpace(const Vec2 &worldPoint) const
Converts a Vec2 to node (local) space coordinates.
The result is in Points.
Parameters
worldPoint A given coordinate.
Returns
:A point in node (local) space coordinates .
This function transforms the incoming coordinates to the node coordinate system of a certain node. To understand this function is to draw the coordinate system with the lower left corner of the reference node coordinates as the origin, and observe the relative position of the incoming point, you can know that it is the incoming point coordinates minus the world coordinate system coordinates of the node origin. It can still be understood step by step. ①Pass in a node, it can be passed in any way, as long as it is of Vec2 type, ②The relative position of the incoming point and the origin of the node is compared through the world coordinate system. ③ Transform to the node coordinate system, draw the coordinates with the node origin, and get the position of the incoming point in the node coordinate system.
auto A = Sprite::create("red.png");
auto B = Sprite::create("green.png");
A ->setContentSize(Size(300,200));
B->setContentSize(Size(200,100));
A->setAnchorPoint(Vec2(0,0));
B->setAnchorPoint(Vec2(1,1));
A->setPosition(300,300);
B->setPosition(200,200);this->addChild(B, 0) ;this->addChild(A, 0);Vec2 position1 = B->convertToNodeSpace(Vec2(10,10));log("Node Position = (%f,%f)",position1.x,position1.y) ;
Result: Node Position = (10.000000,-90.000000)
There is no node A involved, and A is ignored.
The position of the origin of B in the world coordinate system is (0, 100), the incoming coordinates (10, 10), (10, 10)-(0, 100) = (10,- 90), the reason for subtraction can be understood by drawing a picture according to the above decomposition.
The process of changing the anchor point and parent-child node is the same as convertToWorldSpace as long as you understand why the subtraction is done.
convertToWorldSpaceAR and convertToNodeSpaceAR
Just change the reference node origin to the node anchor point. If you really understand the above content, understanding these two is easy. Therefore, I will not repeat it.