Cocos2D Rotation and Anchor point
Asked Answered
B

4

9

The problem that I have is that when ever I change the anchor point sprite automatically rotates with respect to the current anchor point. And I don't want that to happen.

The steps that I followed

  1. create a sprite with anchor point (0.5, 0.5)
  2. Changed the anchor point to (0,1)
  3. Rotated the sprite to 90 degree. (Using CCRotateBy. Sprite rotated correctly)
  4. Changed the anchor point to (0.5, 0.5) (Every thing is fine till now. And this is the position that I need to keep). Now sprite.rotation is 90.
  5. I changed the anchor point to (1,0) (Sprite automatically rotates to 90 degree with respect to the given anchor point - I need to stop this behavior)

Is there any way to reset the rotation of sprite to 0, without actually rotating the texture(ie., to keep the texture in its current form - actual texture rotated to 90 degrees) and changing anchor point or position along with step 4, so that I can continue from point 5.

Bourke answered 7/6, 2011 at 16:25 Comment(0)
S
8

As Lukman says, the anchor point will always affect rotation, since your goal is to be able to specify the sprite position with a different anchor point from the rotation I would suggest making an empty CCNode as a parent of your sprite.

This way, you can set the position on sprite to be relative to this parent node to compensate for your anchor point change and then keep the anchor point for rotation on the sprite but use the parent node for position.

Stevenage answered 9/6, 2011 at 16:9 Comment(0)
R
5

anchorPoint affects both position and rotation. You cannot stop it from affecting either one of them.

But from reading your question, since you want to prevent anchorPoint from affecting the rotation, I'm assuming here that the reason you change the anchorPoint is for the position, for example you are setting it to be ccp(1, 0) because you want to the sprite bottom right corner, instead of the sprite center, to be where you set the position is.

My suggestion is: don't change the anchorPoint at all, but change the way you set the sprite position. You can use this small function to adjust the position:

CGPoint adjustedPosition(const CGPoint position, const CGPoint anchor, const CGSize size) {
    return CGPointMake(position.x - (anchor.x - 0.5) * size.width, position.y - (anchor.y - 0.5) * size.height);
}

Now, assuming you wanted to use anchorPoint of (1,0) when doing the positioning, instead of sprite.position = ccp(200, 300), you just need to do:

sprite.position = adjustedPosition(ccp(200, 300), ccp(1.0, 0.0), sprite.contentSize);

If you want, I'll post the logic behind the math later. Otherwise, I hope this will help.

Roque answered 8/6, 2011 at 16:25 Comment(0)
C
1

Maybe it will help you to install an anchor for the sprites in the correct coordinate.

void SetAnchorPosition(CCSprite * sprite, const CCPoint & point)
{
    static CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    double x = ((double)1/(double)winSize.width)*(double)point.x;
    double y = ((double)1/(double)winSize.height)*(double)point.y;

    sprite->setAnchorPoint(ccp(x,y));
    sprite->setPosition(point);
}
Crus answered 11/7, 2012 at 8:13 Comment(0)
L
0

You can add a line in the touchEnded method as a forceful alternative:

-(void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event{

    _yourSprite.rotation = 90;

}
Lahnda answered 28/5, 2014 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.