1

In Cocos2D I am creating my CCSprites with anchor points of (0,1) which is similar to the way UIKit does it. Anyway, I am trying to change the anchor point in Box2D, is this possible? If so how would I do it with an anchor point of (0,1)?

Thanks!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

2 Answers2

4

Box2D bodies don't have an anchor point.

The anchorPoint is an offset of a node's texture relative to the node's position. Box2D bodies don't have a texture, hence no anchor point.

Generally speaking you're going to make a lot of things more difficult by changing the anchorPoint from its default 0.5,0.5 position. Like, for example, the issue that lead you to ask this very question.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Hmm I ran by this link: http://www.box2d.org/forum/viewtopic.php?f=3&t=3612&p=17877#p17877 And they say its possible, so after you look at that link, do you think it is still possible? – SimplyKiwi Nov 06 '11 at 19:04
  • Yes, you can offset the shapes, but there's no simple property for that, and you'd have to add this offset to every Box2D body to match the anchorPoint offset of the corresponding sprite. It is going to be a lot easier to just stick with the cocos2d anchorPoint as it is rather than trying to make cocos2d behave like UIKit. See also this explanation: http://stackoverflow.com/questions/7808981/moving-a-stick-figure-anchorpoints-animation-or-something-else/7810180#7810180 – CodeSmile Nov 06 '11 at 21:48
  • I see. That was a very good explanation. So if I use the regular 0.5,0.5 anchor point, i will mess up the position of my sprite because I am used to UIKit. Is there any took or any way to convert UIKit position coordinates to Cocos2D coordinates? – SimplyKiwi Nov 07 '11 at 00:15
  • You can convert a uikit coordinate to cocos2d like this: node.position = CGPointMake(uikitPos.x + node.contentSize.width * node.anchorPoint.x, uikitPos.y - node.contentSize.height * node.anchorPoint.y); You might want to wrap that into a helper method or a CCNode category method. – CodeSmile Nov 07 '11 at 12:30
  • Thanks! I will definitely do that and leave the anchor point at .5,.5. Thanks for the help and accepted answer! – SimplyKiwi Nov 07 '11 at 12:38
0

Changing anchor point of shapes in body :

do a for loop to get the smallest x vertice and the biggest, substract to get the width, you can do in the same loop get smallest y vertice and the biggest and substract,

if you then move all vertices with the differences you will position the shape in the 0,0.

it works not sure if the best solution.

freezing_
  • 984
  • 1
  • 9
  • 13