[SOLVED] Position a sprite over a box2d circle shape

[SOLVED] Position a sprite over a box2d circle shape

I’m having problems positioning a sprite over a circular box2d body. The sprite is kind of orbiting the body’s center point from a distance of around 300 px. For non-circular bodies I don’t have any problems, so I’m confused and I don’t know what to do. Any ideas?

Here’s my code to position stuff:

for (b2Body *body = world->GetBodyList(); body != nullptr; body = body->GetNext()) {
		if (body->GetType() != b2BodyType::b2_staticBody && body->GetUserData()) {
			Sprite *spr = (Sprite*)body->GetUserData();
			spr->setPosition(Vec2(body->GetPosition().x * PTM_RATIO, body->GetPosition().y * PTM_RATIO));
			spr->setRotation(-1 * CC_RADIANS_TO_DEGREES(body->GetAngle()));

Are you specifying z-order anywhere in code?

Nope. I can see the sprite, it’s just not positioned properly.

Can you post a picture?


Oh I see over != on top of which is what I thought you meant.

I figured it out!
My mistake was setting the initial position of the shape (b2CircleShape in this case) instead of the body (b2BodyDef) when creating the body object. The shape’s position is in relation to the body, so setting its position to a high value made everything weird when I was trying to position the sprite later.

Oh, thanks. I hadn’t thought about that.