Cocos2d旋转触动问题

问题描述:

我有一个像精灵一样的矛。它的旋转取决于touchesMoved方法。每当用户滑动他的手指时,它指向那个触摸。这是我的方法:Cocos2d旋转触动问题

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

} 

这种有点,但只能从0到45度。而它却相反。因此,当我从底部移动到顶部时,它会顺时针旋转(它应该跟随fnger的方向并逆时针旋转)。从45到90,它工作正常(点击移动计数器),但只有当我开始触摸屏幕的上部对角线。

我在做什么错了? 感谢

+0

如果您不从上对角线开始,45到90会发生什么?你是什​​么意思的上对角线? – lins314159

#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

感谢您的详细回复。我没有使用box2d或花栗鼠atm。只需要纯粹的cocos2d,我所需要的就是引导矛旋转的手指。 – sumderungHAY

+0

答案依然存在。 –

想通了什么事。我需要改变CGPoint我从触摸钻进GL点是这样的:

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

位置= [[CCDirector sharedDirector] convertToGL:位置]。

傻我。之前应该有这个想法。

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];