1

我通过以下链接制作了垒球体: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 和 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");
}

现在,纹理与球一起出现,但我想将纹理显示为旋转也我应该在这里更新什么?引导我.....

4

2 回答 2

0

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

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

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

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

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

但是,为了使球的纹理随着球旋转,您需要获取球体的角度并将其应用于球的节点对象:

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

self是球的节点实例。)

有一个很棒的网站,里面有关于 box2d 和 cocos2d 的很好的教程,我建议你去看看。这将有助于非常轻松地了解所有更新内容。

于 2013-08-09T13:14:10.040 回答
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());
 }
}

希望能帮助到你

于 2013-12-28T04:53:59.730 回答