1
votes

I have a problem when rotating a triangular image like a radar detecting enemies.

b2Body *body;
CCSprite *actor;



if (b->GetType() == b2_kinematicBody && myActor == [loader spriteWithUniqueName:@"radar."]){

        radar = b;
        radarSprite = myActor;
    }

It is a kinematic Body made with LevelHelper, and I want to rotate it. So I need it to rotate around a static point, and repeat the movement backwards. Since it is a Body and not a Sprite, I don't know how to make it to rotate around the top point.

I have an idea, but I'm not sure. What about if I rotate the CCSprite and move the Body to sprite.position? Is it possible?

Thank you very much

1

1 Answers

2
votes

Here's how I update position and rotation of box2d bodies acording to theirs sprites:

// Box2d updates.
// We set position of b2Bodies according to their sprites.
_world->Step(delta, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext())
{
    if (b->GetUserData() != NULL)
    {
        GameObject *sprite = (GameObject *)b->GetUserData();
        b2Vec2 b2Position = b2Vec2(sprite.position.x/PTM_RATIO,
                                   sprite.position.y/PTM_RATIO);

        float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(sprite.rotation);
        b->SetTransform(b2Position, b2Angle);
    }
}