understanding cocos2d anchorpoint/position - cocos2d-iphone

Please tell me if I got this wrong.
Anchor point is like a movable center of a sprite.
You specify a sprite's center by setting an anchor point.
Now you can place the sprite by specifying the position of the center.
ex)
anchor point = (1,1) : sprite's center is its right upper corner(I can think of it as "pinned at the right upper corner")
position = (screenWidth, screenHeight) : place the center(anchor point) at the (screenWidth, screenHeight)
Resulting in a sprite aligned at the top-right corner of the screen.

strange name ("movable center"), but you are almost right. the only thing, that it is property of CCNode class, not CCSprite. Just by default CCNode has no contentSize and has isRelatieveAnchorPoint property to NO.
Another one point is that all transformations will be done relatieve to the anchor point. I mean, if you will set anchor point to, for example, (1.f, 1.f),place tour node(sprite) to the center of the screen and begin to change it's rotation(scale, etc.), it will rotate around it's anchor point.
And one interesting trick with anchor point. Nobody says that it's x and y values must be between 0.f and 1.f =) So, you can rather simply make your node(sprite) to move by circle, setting it's anchor point to the out part (for example, to (0.5f, -10.f)) and begin to rotate it. Your node(sprite) will move by circle with center in node.position(sprite.position) and radius of (10.5f * it's contentSize.height)

Related

Move the box2d body to center of screen

I am having an circle in the center of the world. I add some balls to the world in the form of b2Body. Now i want to move or throw the ball to the center of the screen. The effect should be like the balls are colliding with the circle.
The ball are positions randomly, so they can be at any were on the screen and the need to travel to center of the screen to the circle
Can any one tell me how to do this because i have no idea to move the b2Body object.
I want blue circle to attract red circles. Or in other words i want red circles to move to blue circle.
Finally got the solution of my problem. I use the concept of radial gravity.
http://www.vellios.com/2010/06/06/box2d-and-radial-gravity-code/
In this case, I am not sure about this way but you can try this way. I guess you are adding b2body by touching on the screen. Now i don't know what is the type of your B2body.
There are two ways to move the b2body.
check this link:http://www.cocos2d-iphone.org/forum/topic/21620
From this link,
I am guessing, in your game ,body is b2static body,What you can do is Move your sprite to center of the screen, with respect to that change the position of the corresponding body in tick method.
And you need to stop moving the sprite when it hits the center ball so stop moving corresponding sprite when it hits the center ball.
This may be the possible way if i understood your question.

the order of translate and scale for zoom and pan

first thing I want to do is translating to the center of the screen and draw all of the objects from there.
then I would like to apply tranlsate for panning and scale for zoom. I want to zoom relative to a center point ! so how should be the order of them so that it works ?
glTranslatef(width/2, height/2, 0);
gltranslate(centerX,centerY); // go to center point
glscale(zoom);
glTranslatef(offset.x/zoom, offset.y/zoom, offset.z/zoom); // pan
I tried the above order but it doesn't go to the center point and it always zoom relative to (0,0).
I suppose you are drawing a square with both x and y between 0,1.
first you have to translate to the point the scaled object should be:
glTranslate3f(centerX,centerY,0);
glScale(zoom);
glTranslatef(-0.5f, -0.5f,0); // to the middle
draw stuff
opengl executes the transformations in reverse order since it's a pipeline.
reading the above sequence in the bottom-up direction will give the key.

Cocos2D 2.0 - adding a sprite to a layer... what about the center?

I have a sprite added to a layer. I am having a lot of problems when I transform the layer. As far as I know a layer has its center (anchorPoint) on the bottom left corner and a layer has it in the middle (right?) - I am not totally sure about that.
On the figures below, I represent a CClayer in pink and a CCSprite in purple. See where I think the centers are.
When I add a sprite to a layer, I think Cocos will do like in A, but I want it like in B. How do I do that? Another possibility is C, that I think is better, but that would involve moving the anchorPoint of the layer to the middle and put the sprite there... I don't a have a clue on how to do that.
Change anchor point of CCSprite.
CCSprite *sprite = [CCSprite spriteWithFile:#"sprite.png"];
//For case A
sprite.anchorPoint = ccp(0.0f,0.0f);
sprite.position = ccp(0.0f,0.0f);
//For case B
sprite.anchorPoint = ccp(0.5f,0.5f);
sprite.position = ccp(0.0f,0.0f);
//For case c
sprite.position = ccp(ScreenWidth/2.0f, ScreenHeight/2.0f);
sprite.anchorPoint = ccp(0.5f,0.5f);
Anchor point is relatieve coordinate. (0.f, 0.f) is left-bottom corner of the node, (1.f, 1.f) is right-top corner. All transformations are make relatieve to the anchor point. Positioning is also transform. It means that in case of anchor point (0.5f, 0.5f) all positioning and other transforms will be applied relatieve to the center of the node. If you want to place your sprite to the left-bottom corner of your layer, you can simply set it's anchor point to (0.f, 0.f) and set position (0.f, 0.f). It means that left-bottom corner of your sprite will be placed to the (0.f, 0.f) coordinate of your parent layer.
[sprite setAnchorPoint:ccp(0.f, 0.f)];
[sprite setPosition:ccp(0.f, 0.f)];
[layer addChild:sprite];
When I add a sprite to a layer, I think Cocos will do like in A, but I
want it like in B.
Don't try to guess behaviour. Add sprite to the layer and see what happens.
Also, CCNode and all its subclasses, including CCSprite, have position property, which represents node's position relative to its parent's origin.

Given the following information, how is my sprite on the screen?

I'm really struggling to understand cocos2d 2.0 and how its layers and coordinates work. I have a single scene with a single layer that contains a single sprite. Here are the stats:
Layer: Position: (0,0)
Origin: (-384, -430)
Size: (768, 860) (same as the view)
AnchorPoint: (0.5, 0.5)
ignoreAnchorPointForPosition: NO
Scale: 1.0
Sprite: Position: (768, 860)
Origin: (499.5, 413.5)
Size: (537, 893)
AnchorPoint: (0.5, 0.5)
I add the sprite in the init of the layer.
How is it that the sprite is positioned at the center of the view in the iPad simulator? If someone could help me draw a mental picture (or an actual one :)) of this, that would really help.
This image shows how I understand the placement and it doesn't make any sense. If this is a bad question or not the correct forum, please let me know and I'll move it to the correct place.
Anchor point is relative point on the node. (0.f, 0.f) corresponds to left-bottom corner and (1.f, 1.f) is for right-top corner. The position, that you set to the node, is set for anchor point. Of course, if it's property isRelatieveAnchorPoint is YES.
So, if anchor point is (0.5f, 0.5f), you set the position of center of the node. Thats why your layer is placed in (0.f, 0.f) of world coordinates with it's center.
But inner coordinates are always count from (0.f, 0.f) of current node. So, if you add your sprite to (768.f, 860.f) with sprite's anchor point (0.5, 0.5f), sprite's center will be positioned to this coordinates relatieve to the parent layer's origin.
I hope i described it clear enough =)

opengl rotate tangent to a circle

I have an object right now which I have moving in a circle around the vertical (Y) axis. I want to rotate this object so it is always aligned with the tangent of the circle, how do I do this? Not sure what combination of sin/cosine/tan to use as the first argument of glRotatef...
Thanks!
The first argument of glRotatef is the angle, in radians (so 0 is no rotation, PI is flipped around end for end, and 2*PI is rotated all the way back to the original orientation).
You probably could have answered this yourself through trial and error in less time than it took to askk the question.
Note that if you choose the center of rotation to be the center of the circle instead of the center of the object, you won't need a separate translation step.