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.
Related
What is the significance of anchorPoint property in SpriteKIT?
I used it to position a specific node above the lower border of the screen. However, is that the only way to make sure that the objects don't cross the lower borders of the screen in a world where gravity is set to true?
What should the anchor point be set to to ensure that a specific node doesn't go out of horizontal/vertical bounds in case an impulse is applied?
Check out Apple's docs on the anchorPoint. Basically, the anchorPoint is used to define the center of an object. By that, I mean when you set the position of the node, it sets the position of the node's anchorPoint in the scene. From there, the anchorPoint with tell the node (I'm going to use Apple's spaceship example) that the image should be shifted so that the anchorPoint is wherever you defined. anchorPoint is a 1.0 by 1.0 size area, with 0.0, 0.0 being the bottom left of the sprite, and 1.0, 1.0 being the top right. It scales for however wide your sprite is. When you adjust the zRotation of your sprite, it will rotate around the anchorPoint.
So if I say, have a sprite with an anchorPoint at 0.5, 1.0, and I set the position to the exact center of the screen, the sprite will actually hang down, as the anchorPoint has been moved up to the top of the node.
Anyway, it should not have an effect on physics. I would recommend using position for setting the position of the node instead. If you are looking to make sure a physics body won't go through a wall/phsyicsBody, then run usesPreciseCollisionDetection = YES (or whatever language you are using's true statement) on your node's physicsBody.
I noticed that in Sprite Kit the coordinate system is flipped.
For example, here is a SKSpriteNode:
SKSpriteNode *car = [SKSpriteNode spriteNodeWithImageNamed:#"car"];
car.position = CGPointMake(0, 0);
[road addChild:car];
The car is positioned in the center of it's parent.
When I set position to:
car.position = CGPointMake(-50, 0);
then it is positioned more to the left.
But when I want to move it down, and increase Y, it moves UP!
car.position = CGPointMake(-50, 20);
In UIKit increasing Y always means something moves down. It feels more logical to me. Is there a way to flip the coordinate system in Sprite Kit?
You can set your scene's yScale property to -1. Everything will render upside down, but you can also set the yScale property for each node to -1, which will cause them to render right side up.
You could also make a class category and create a property called invertedPosition with a custom getter/setter that inverts the default position property.
Sprite Kit uses a coordinate orientation that starts from the bottom left corner of the scene (0,0), and the values increase as you move to the right (increasing x) and up (increasing y).
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
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 try to set the anchorPoint property, in order to rotate a view by a well defined axis.
But:
myView.layer.anchorPoint = CGPointMake(myView.layer.anchorPoint.x - 1.0, myView.layer.anchorPoint.y);
Wenn I shift it by -1.0, it will not just move 1 unit to left. Instead, my whole view moves by the width of the view to right.
What kind of coordinate system is that? It seems inverted. But also the units don't match with those of for example myView.frame.size.width ?
anchorPoint is a normalized position within your layer. That is, (0.0, 0.0) is the upper-left corner of your layer (in the iPhone's flipped UIView layer coordinate system) and (1.0, 1.0) is the lower-right corner. (0.5, 0.5) is the middle of your layer. These positions are size-independent, and thus merely relative locations.
This makes it possible to scale about that anchorPoint, which would be confusing to do if anchorPoint was an absolute coordinate.