【问题标题】:Cocos2d rotation on touches moved problemCocos2d 旋转触摸移动问题
【发布时间】:2011-06-30 18:10:29
【问题描述】:

我有一个像精灵一样的长矛。它的旋转由 touchesMoved 方法决定。每当用户滑动手指时,它就会指向该触摸。这是我的方法:

- (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 度。它相反。所以当我从手指向下移动到顶部时,它顺时针旋转(它应该跟随手指的方向并逆时针旋转)。从 45 到 90,它工作正常(反向点击移动),但前提是我在屏幕的上对角线开始触摸。

我做错了什么? 谢谢

【问题讨论】:

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

标签: objective-c ios cocoa-touch cocos2d-iphone


【解决方案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());
        }   
    }
}

【讨论】:

  • 感谢您的详细回复。我没有使用 box2d 或花栗鼠 atm。就是纯cocos2d,我想要的就是用手指来引导长矛的旋转。
  • 答案还在里面。
【解决方案2】:

找出问题所在。我需要将触摸得到的 CGPoint 更改为 GL 点,如下所示:

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

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

我傻了。之前应该考虑过这个。

【讨论】:

    【解决方案3】:
    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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      相关资源
      最近更新 更多