2011-06-30 4 views
0

J'ai une lance comme sprite. La rotation est décidée par la méthode touchesMoved. chaque fois que l'utilisateur glisse son doigt, il pointe vers ce contact. C'est ma méthode:Cocos2d rotation sur les touches déplacé problème

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch* touch = [touches anyObject]; 
    CGPoint location = [touch locationInView: [touch view]]; 


    float angleRadians = atanf((float)location.y/(float)location.x); 
    float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); 

    spear.rotation = -1 * angleDegrees; 

} 

Ce genre fonctionne, mais seulement de 0 à 45 degrés. et ça va en face. Alors que je me déplace d'un doigt de bas en haut, il tourne dans le sens des aiguilles d'une montre (il devrait suivre la direction de fnger et tourner dans le sens inverse des aiguilles d'une montre). De 45 à 90, ça marche bien (se déplace contre le clic) mais seulement si je commence le toucher en diagonale supérieure de l'écran.

Qu'est-ce que je fais de mal? Remerciements

+0

Et que voulez-vous dire par diagonale supérieure? – lins314159

Répondre

1
#define PTM_RATIO 32 

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

    for(UITouch *touch in touches) { 

     CGPoint location = [touch locationInView: [touch view]]; 

     location = [[CCDirector sharedDirector] convertToGL: location]; 

     b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); 

     spriteBody->SetTransform(locationWorld,spriteBody->GetAngle()); 
    } 
} 

-(void) tick: (ccTime) dt 
{ 

    //It is recommended that a fixed time step is used with Box2D for stability 
    //of the simulation, however, we are using a variable time step here. 
    //You need to make an informed choice, the following URL is useful 
    //http://gafferongames.com/game-physics/fix-your-timestep/ 

    int32 velocityIterations = 8; 
    int32 positionIterations = 1; 

    // Instruct the world to perform a single step of simulation. It is 
    // generally best to keep the time step and iterations fixed. 
    m_world->Step(dt, velocityIterations, positionIterations); 

    // for (int i = 0; i < (int)birds.size(); i++) 
    //  birds[i]->render(); 

    //Iterate over the bodies in the physics world 
    for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext()) 
    { 
     if (b->GetUserData() != NULL) { 
      //Synchronize the AtlasSprites position and rotation with the corresponding body 
      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()); 
     } 
    } 
} 
+1

Merci pour la réponse détaillée. Je n'utilise pas box2d ou chipmunk atm. Juste cocos2d pur, tout ce que je veux, c'est le doigt pour guider la rotation de la lance. – sumderungHAY

+0

La réponse était toujours là. –

0

Compris ce qui n'allait pas. Je avais besoin de changer le CGPoint je suis arrivé des touches en point de GL comme ceci:

CGPoint location = [touch locationInView: [touch view]]; 

emplacement = [[CCDirector sharedDirector] convertToGL: location];

Silly moi. J'aurais dû y penser avant. Que se passe-t-il pour 45 à 90 si vous ne commencez pas dans la diagonale supérieure?

0
CGPoint touchLocation = [touch locationInView: [touch view]]; 
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 
touchLocation = [self convertToNodeSpace:touchLocation];  
GPoint diff = ccpSub(touchLocation, _player.position); 

//rotate to face the touch 
CGPoint diff = ccpSub(_player.position, touchLocation); 
float angleRadians = atanf((float)diff.y/(float)diff.x); 
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); 
float cocosAngle = -1 * angleDegrees; 

if(diff.x < 0) 
{ 
      cocosAngle += 180; 
} 

id actionRotateTo = [CCRotateTo actionWithDuration:0.1 angle:cocosAngle]; 
[_player runAction:actionRotateTo];