0
votes

I'm using Cocos2D with Box2D, and moving from bodies with a single fixture to bodies with multiple fixtures, each with a corresponding sprite. By using

body->GetTranform().p.x + shape->m_centroid.x
body->GetTranform().p.y + shape->m_centroid.y
body->GetTransform().q.GetAngle()

I am able to position the sprites properly, until the body is rotated. At that point, all goes awry, with each sprite spinning in relation to its siblings, and the whole group spinning off around what I think is a (0, 0) body anchor point.

My guess is that Cocos2D/Box2D has a simple, built-in method for performing the necessary transform, but I haven't been able to find it documented. What is the best way to position my sprites correctly?

2
Are you using radians or degrees? GetAngle gives you radians. - iforce2d
Yes, I left out the meters->pixels and radians->degrees conversions from the code sample. As I mentioned, I've done this using single-fixture bodies. The problem is that b2PolygonShape's m_centroid is not transformed. What I was expecting to find was something like shape->getTransform(), but that doesn't exist. I did finally find what I need, however. See my solution below. - user371320

2 Answers

0
votes

Try rotating sprites with respect to body center in update tick function.

0
votes

I finally located the transform function I was looking for.

b2Vec2 ptWorld =body->GetWorldPoint(shape->m_centroid);

transforms the fixture shape's coordinates, measured relative to the body's origin, into world coordinates. After converting from meters to pixels, ptWorld's x and y coordinates can be used to set the sprite's location.