SamBo
SamBo

Reputation: 551

Sprite.rotation Not working in box2d

I have an array of Sprites, that the user is allowed to move that is handled within ccTouchesMoved.

When the user has one of the sprites selected (by touching it) they then have the option to rotate the sprite using the ccTouchesMoved method, until they unselect the rotation feature.

The sprite rotates correctly, however I have another method that creates edges around the sprites, so that they will have the power for collisions and what not.

When this method is called, it is resetting the rotation of all my Sprites setting them back to 0 and I do not know why.

 for (CCSprite *sprite in bounceBarriers)
    {
        curBarrier = sprite;
        curBarrier.rotation = 20;
        [self bounceCreator];
    }

//bounceCreator:

b2BodyDef barrierBodyDef;
barrierBodyDef.type = b2_staticBody;
barrierBodyDef.position.Set(curBarrier
                            .position.x/PTM_RATIO, curBarrier.position.y/PTM_RATIO);
barrierBodyDef.userData = curBarrier; //the userData is set to the current sprite

_body2 = _world->CreateBody(&barrierBodyDef);
barrierBodyDef.position.Set(0,0);

b2Body *barrierBody;
barrierBody = _world->CreateBody(&barrierBodyDef);

b2EdgeShape barrierEdge;
b2FixtureDef barrierShapeDef;
barrierShapeDef.shape = &barrierEdge;
barrierShapeDef.friction = 1.0f;
barrierShapeDef.restitution = 1.0f;
barrierEdge.Set(b2Vec2((curBarrier.position.x-70)/PTM_RATIO, (curBarrier.position.y+10)/PTM_RATIO),
                b2Vec2((curBarrier.position.x-70)/PTM_RATIO, (curBarrier.position.y-10)/PTM_RATIO));
barrierBody->CreateFixture(&barrierShapeDef);
barrierEdge.Set(b2Vec2((curBarrier.position.x-70)/PTM_RATIO, (curBarrier.position.y-10)/PTM_RATIO),
                b2Vec2((curBarrier.position.x+70)/PTM_RATIO, (curBarrier.position.y-10)/PTM_RATIO));
barrierBody->CreateFixture(&barrierShapeDef);
barrierEdge.Set(b2Vec2((curBarrier.position.x+70)/PTM_RATIO, (curBarrier.position.y-10)/PTM_RATIO),
                b2Vec2((curBarrier.position.x+70)/PTM_RATIO, (curBarrier.position.y+10)/PTM_RATIO));
barrierBody->CreateFixture(&barrierShapeDef);
barrierEdge.Set(b2Vec2((curBarrier.position.x+70)/PTM_RATIO, (curBarrier.position.y+10)/PTM_RATIO),
                b2Vec2((curBarrier.position.x-70)/PTM_RATIO, (curBarrier.position.y+10)/PTM_RATIO));
barrierBody->CreateFixture(&barrierShapeDef); 

Upvotes: 3

Views: 101

Answers (1)

Guru
Guru

Reputation: 22042

Also update b2body's rotation.

   body->SetTransform([self toB2Meters: sprite.position], -1*CC_DEGREES_TO_RADIANS(20));

Upvotes: 2

Related Questions