0
votes

I have a question that is killing me...

I'm using LevelHelper to make a level in Box2d. So I have a triangular sprite attached to a triangular body.

Now the problem is that I want to rotate that body to a position, and rotate the attached sprite too.

Here you have some code:

//Detecting my actor and my sprite
if (b->GetType() == b2_dynamicBody && myActor == [loader spriteWithUniqueName:@"radar."]){
        radar = b;
        radarSprite = myActor;
    }

Just wanted to try with 20 degrees, but it's not rotating.

 //trying to rotate inside TICK: Method

    float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(20);
    radar->SetTransform(radar->GetPosition(), b2Angle);
    radarSprite.position = ccp(radar->GetPosition().x,radar->GetPosition().y);

Any tips?

Thank you very much in advance!!

1

1 Answers

1
votes

you have an error, you rotate only the physic body, levelhelper have a method in the LHSprite called tranformRotation, so you need do this:

this method rotate the body and the sprite (if the body attach to sprite):

//radarSprite is a LHSprite
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(20);
[radarSprite transformRotation:b2Angle];
[radarSprite transformPosition:ccp(radar->GetPosition().x,radar->GetPosition().y)];

if you have the body separate from sprite you can do this:

//radarSprite is a LHSprite
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(20);
radar->SetTransform(radar->GetPosition(), b2Angle);
[radarSprite transformRotation:b2Angle];
[radarSprite transformPosition:ccp(radar->GetPosition().x,radar->GetPosition().y)];


EDIT:

the method spriteWithUniqueName return a LHSprite, but if you use CCSprite you can do this:

//radarSprite is a CCSprite
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(20);
radar->SetTransform(radar->GetPosition(), b2Angle);
[radarSprite setRotation:b2Angle];
[radarSprite setPosition:ccp(radar->GetPosition().x,radar->GetPosition().y)];