如何在box2d中旋转垒球的纹理

时间:2013-08-09 11:24:30

标签: cocos2d-iphone box2d-iphone

我已经通过以下链接制作了垒球身体:http://www.uchidacoonga.com/2012/04/soft-body-physics-with-box2d-and-cocos2d-part-44/

现在我想旋转球的纹理以显示球旋转。

如何旋转它的纹理?

我推荐此链接:http://www.cocos2d-iphone.org/forums/topic/soft-body-chipmunk-physics/page/2/

但这是花栗鼠。我想在box2d中旋转纹理。 任何想法或建议都表示赞赏。

我试过这个

- (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);

}

&安培;在我的交换方法中,我重新计算triangleFanPos&amp; 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");
}

现在,纹理随球而去,但我想将纹理显示为旋转 我应该在这里更新什么? 引导我.....

2 个答案:

答案 0 :(得分:0)

代码没有任何问题,只需要告诉box2d在每次帧更新时进行模拟步骤:

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

b2World是创建box2d实体时使用的box2d世界对象。在主游戏场景/图层中某处安排的更新方法中调用此步骤方法。 dt是从上次更新调用到当前更新调用的时间增量。

此方法计算所有box2d对象的新位置/角度,同时考虑作用于这些物体的所有力。您可以阅读更多相关信息here

当然,物理世界中的某些东西必须让你的球旋转,否则就没有理由让它旋转。

但是为了让球的纹理随球旋转,你可以将球体的角度加到球的节点上:

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

self是球的节点实例。)

有一个great site有关于box2d和cocos2d的精彩教程,我建议你仔细阅读。它将有助于非常容易地理解这些所有更新的东西。

答案 1 :(得分:0)

此代码最常用于使纹理随着物体移动和旋转

-(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());
 }
}

希望有所帮助