0
votes

I'm creating a static shape in chipmunk (using SpaceManager) and attaching a cpCCSprite to it.

However, I need the anchor point of the sprite to be off center, but when I change the anchor point of the sprite, the shape & sprite no longer align.

So if I change the anchor point like this

[sprite setAnchorPoint:ccp(0.5, 0.3)];

The sprite is drawn accordingly, but I expected the shape to "move" with it. Here's a picture of what I mean.

how the cannon should look http://www.tomelders.com/bin/cannon.png

On the left is the shape and the sprite aligned properly. I've not changed the anchor point.

On the right is the sprite with an anchor point of ccp(0.5, 0.3)

I am also rehashing the static shape every frame.

Here's how it's created

// create the sensor
sensor = [spaceMgr addRectAt:pPoint mass:STATIC_MASS width:53 height:81 rotation:0];
  sensor->sensor = YES;
  sensor->collision_type = 2;

  //Create the sprite
  CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"bownce-sprites-comic-sized.png"];

  barrel = [[cpCCSprite node] initWithShape:sensor texture:texture rect:CGRectMake(3, 428, 53, 82)];
  [self addChild:barrel];

  // set the ancor point
  [barrel setAnchorPoint:ccp(0.5, 0.3)];
  [barrel setPosition:pPoint];
1

1 Answers

7
votes

This is a common misconception of how anchorPoints work. The anchorPoint has nothing to do with the node's position. Instead, it is the offset for the texture of the node object!

The default anchorPoint is at 0.5, 0.5 which means the texture is centered on the node's position. If you set the anchorPoint to 0,0 then the lower left corner of the texture aligns with the node's position. Effectively the texture moves to the right and up. The node's position has not changed however.

Since the shape is aligned with the node's position, it will not move when you change the anchorPoint. The easiest way to fix this is to change the image, for example by increasing its dimenions to one side, using only the transparent color. I hope that's understandable.