Can any one explain difference between position and anchor point in cocos-2D with some example.I searched in google but cannot find good explanation,thanks in advance .
Suppose you have a square which is 10x10. If you say that you want to position it on your screen at position (50,40) then you need to know where that position refers to - the top left of your square, bottom left, etc.
The anchor point refers to this position. So, if your anchor point is (0,0) then the position (50,40) will be the position of the top left corner of your square.
If your anchor point is (10,0) then the position (50,40) will be the position of the top right corner of your square and so the top-left corner will be at (40,40).
So, the anchor point is the point that is positioned, and is then relative to your square.
Another example - suppose you have a building 100 floors high. Now, suppose you are a giant and you are 4 floors tall. If you are told to put your feet (that's your anchor point) on the 3rd floor, then your head will be on the 7th floor. If you were told to put your head (that's now your anchor point) on the 7th floor, then your feet would be on the 3rd. You are still in the same place, but your reference point (the anchor) has been changed.
The position property is a CGPoint that specifies the position of the layer relative to its superlayer, and is expressed in the superlayer's coordinate system.
The anchorPoint property is a CGPoint that specifies a location within the bounds of a layer that corresponds with the position coordinate. The anchor point specifies how the bounds are positioned relative to the position property, as well as serving as the point that transforms are applied around. It is expressed in the unit coordinate system-the (0.0,0.0) value is located closest to the layer’s origin and (1.0,1.0) is located in the opposite corner. Applying a transform to the layer’s parent (if one exists) can alter the anchorPoint orientation, depending on the parent’s coordinate system on the y-axis and also see this link
Related
I am new to unity. I am trying to figure out what is the difference between anchored position and local position and where can the both be applied?
Anchored Position
The Anchored Position is the position of the pivot of the RectTransform taking into consideration the anchor reference point. The anchor reference point is the position of the anchors. If the anchors are not together, Unity estimates the four anchor positions using the pivot placement as a reference.
Local Position
localPosition is the position of the GameObject with respect to its parent object. transform. position is the position of the GameObject with respect to the root. Within local space of a GameObject, the center (or pivot) is always (0, 0, 0).
Source: Unity Forum
In the scene, I have a long chain of children that are connected via hinge to their parent. For my code, I need the position of the hinge anchors in world space, so I use:
public Vector2 hingeVector => hinge.anchor + (Vector2)gameObject.transform.position;
For the first hinge, that code gives the correct position. But for the second hinge this happens:
The red point is the vector I get, the blue point is the actual position. As you can see, it's a somewhat small but still problematic difference.
Is there any way I can fix this? I couldn't find anything like this online.
You need to add the object's rotation
The anchor values are axis aligned and aren't affected by rotation, but in order to calculate the anchor point in world space, knowing the transform's position, you need to rotate the anchor point values by the object's rotation then add it to the position:
Vector2 p = hinge.anchor.Rotate(gameObject.transform.rotation.eulerAngles.z)
+ (Vector2)gameObject.transform.position;
I'm trying to position some gameObjects relative to it's parent. I'm using localposition, but the gameObjects are not placed well. If I use local position the parent's width is considered to be 1, right? So if I have a plane unity3d considers it to be a 1X1 square.
I tried to put some models in the local position (1, 1) but they are not placed in the top right corner of the plane...
Do you guys have some thoughts of what might be the problem?
You're incorrect: the parents' width is the x/y/z scale of the parent; local co-ordinates are still measured in world units. If your plane is 100x100 horizontally, and centred at 0,0,0 then you need to put the model to (50,0,50) to be at the corner.
I am new to Cocos2D on iPhone. I see that Cocos2D uses a coordinate axis where 0,0 is at the bottom left corner and X positives are to the right and Y positives up.
Now I have created a sprite and added several sprites as subsprites of this one. For my surprise the subsprites appear mirrored in Y!!! The point 10,10 will be on the upper left corner of the sprite!!!
I can simply flip the sprite in Y to make it follow the same screen coordinate, but then the content will be reversed.
Is this a bug or what?
thanks.
Without seeing any example code is a shot in the dark, but I think you need to use Anchor points.
Each sprite has an anchor point of x, y.
ccp(0.5f, 0.5f) would be the center of the sprite.
(0,0) is the bottom left....(1.0f,1.0f) is top right etc.... Over 1.0 goes outside the sprite.
Child nodes (CCSprite) will use their anchor point on the parent node coordinates.
MySprite.anchorPoint = ccp(0.5f,0.5f);
I have some problem with Anchor point..
i have a sprite and i need to calculate the anchor point for this sprite around the screen center.
How can i calculate the anchor point?
You can access the anchor point values using:
mySprite.anchorPoint.x
and
mySprite.anchorPoint.y
These are both floating point values so be aware of that.
EDIT
To set them you just do:
mySprite.anchorPoint = ccp(1.0f, 1.0f);
An anchor point of (1.0, 1.0) would be the upper right corner of your image, whereas the original anchor point is in the middle and therefore, (0.5, 0.5). To try and solidify the anchor point for the bottom left would be (0.0, 0.0). You can get any other anchor point within the image by understanding these.