1
votes

I have made softball body by following this link: http://www.uchidacoonga.com/2012/04/soft-body-physics-with-box2d-and-cocos2d-part-44/

now i want to rotate ball's texture to show the ball rotating.

How can i rotate the texture of it?

i have refer this link:http://www.cocos2d-iphone.org/forums/topic/soft-body-chipmunk-physics/page/2/

But it's for chipmunk. i want to rotate texture in box2d. any idea or suggestions are appreciated.

I have tried this

- (void) draw {
          if(draw){

              [self swap];

               printf("%d\n",R);
              for(int i=0;i<NUM_SEGMENTS+2;i++)
              {   

                  if((i+R)%12<=R && R<=12){
                      printf("%d--->%d\n",i,(i+R+1)%12);
                      triangleFanPos[i]=triangleFanPos[(i+R+1)%12];
                       textCoords[i] = textCoords[(i+R+1)%12];
              }else{
                  printf("%d--->%d\n",i,(i+R+1)%12);
                triangleFanPos[i]=triangleFanPos[(i+R+1)%12];
                   textCoords[i] = textCoords[(i+R+1)%12];
              }

          }
          if(R==12){
              R=0;
          }
        triangleFanPos[NUM_SEGMENTS+1]=triangleFanPos[1];
        textCoords[NUM_SEGMENTS+1]=textCoords[1];  


    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
    glBindTexture(GL_TEXTURE_2D, [texture name]);
    glTexCoordPointer(2, GL_FLOAT, 0, textCoords);
    glVertexPointer(2, GL_FLOAT, 0, triangleFanPos);
    glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_SEGMENTS+2);
    glEnableClientState(GL_COLOR_ARRAY);

}

& in my swap method i recalculate triangleFanPos & textCoords

-(void)swap
{
    printf("Recalculation Starts\n");   
    triangleFanPos[0] = Vertex2DMake(innerCircleBody->GetPosition().x * PTM_RATIO - self.position.x, 
                                     innerCircleBody->GetPosition().y * PTM_RATIO - self.position.y);
    Fanposition[0]=triangleFanPos[0];
    for (int i = 0; i < NUM_SEGMENTS; i++) 
    {

        b2Body *currentBody = (b2Body*)[[bodies objectAtIndex:i] pointerValue];
        Vertex2D pos = Vertex2DMake(currentBody->GetPosition().x * PTM_RATIO - self.position.x, 
                                    currentBody->GetPosition().y * PTM_RATIO - self.position.y);
        triangleFanPos[i+1] = Vertex2DMake(pos.x, pos.y);
        Fanposition[i+1]=triangleFanPos[i+1];




    }
    triangleFanPos[NUM_SEGMENTS+1] = triangleFanPos[1];
    Fanposition[NUM_SEGMENTS+1]=triangleFanPos[NUM_SEGMENTS+1];
    textCoords[0] = Vertex2DMake(0.5f, 0.5f);
    for (int i = 0; i < NUM_SEGMENTS; i++) {
        GLfloat theta = ( self.rotation * M_PI / 180 ) + ( deltaAngle * i );
        textCoords[i+1] = Vertex2DMake(0.5+cosf(theta)*0.5, 
                                       0.5+sinf(theta)*0.5);

    }
    textCoords[NUM_SEGMENTS+1] = textCoords[1];

   R++;
    printf("Recalcutation ended\n");
}

now, texture goes with ball but i want to show that texture as rotating also What should I have to update here? Guide me.....

2

2 Answers

0
votes

There is nothing wrong with the code it just that you need to tell box2d to make a simulation step every frame update :

b2world->Step(dt, 10, 10);

b2World is the box2d world object used when creating box2d bodies. Call this step method in an update method scheduled somewhere in your main game scene/layer. dt is the time delta from the last update call that has passed until the current update call.

This method calculates the new position/angle of all box2d objects taking into account all the forces that act upon these bodies. You can read more about it here

And of course something in your physics world must make your ball rotate or otherwise there is no reason for it to rotate.

But nevertheless to make the ball's texture rotate with the ball you take the ball body's angle and apply it on the ball's node object :

self.rotation = -1 * CC_RADIANS_TO_DEGREES(innerCircleBody->GetAngle());

(self is the ball's node instance.)

There is a great site with a nice tutorial about box2d and cocos2d which I recommend you to look through. It will help with understanding about this all updating stuff very easily.

0
votes

This code is most common for making textures move and rotate with bodies

-(void)tick:(ccTime)dt{

int32 velocityIterations = 8;
int32 positionIterations = 1;
uint substeps = 4;
float32 subdt = dt / substeps;

for (uint i = 0; i < substeps; i++) {


    world->Step(subdt, velocityIterations, positionIterations);

for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
    if (((CCSprite*)b->GetUserData()) != nil) {

        CCSprite *myActor = (CCSprite*)b->GetUserData();
        myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
        myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
 }
}

Hope it helps