Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); 
like image 408
SamBo Avatar asked Dec 19 '25 20:12

SamBo


1 Answers

Also update b2body's rotation.

   body->SetTransform([self toB2Meters: sprite.position], -1*CC_DEGREES_TO_RADIANS(20));
like image 62
Guru Avatar answered Dec 21 '25 08:12

Guru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!