0

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 Answer 1

1

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)];
3
  • It looks very nice, I have to download LHSprite.h because I'm not working with that class. I will try it later. Thank you very much!
    – mursang
    Apr 24, 2013 at 18:00
  • Now it doesn't work here: if (b->GetType() == b2_dynamicBody && myActor == [loader spriteWithUniqueName:@"radar."]){ radarSprite = (LHSprite * )b->GetUserData(); }
    – mursang
    Apr 24, 2013 at 22:38
  • the method spriteWithUniqueName return a LHSprite, but if you use CCSprite you can use the second option and replace the "transform" for "set".... edit the message.
    – busta117
    Apr 25, 2013 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.