【问题标题】:Update coordinates of sprite in game更新游戏中精灵的坐标
【发布时间】:2011-12-12 18:56:37
【问题描述】:

这确实很麻烦,但我有两个精灵,它们都是 17 像素长的手臂。它们都在 ccp(0.5f,0.0f) 处有锚点;我想要的是,当 arm1 旋转时,arm2 的 CGPoint 等于 arm1 锚点的另一端。比如,在 45 度角,CGPoint 就是 ccp (arm1.position.y, arm1.position.x + 17);

所以我在 ccTime 函数中更新了 arm1 的旋转,它调用了另一种方法来计算角度旋转。基本上发生的事情是...... arm2 在正确的圆形区域内快速旋转,这意味着某些事情是正确的,但旋转速度非常快。

-(void) callEveryFrame:(ccTime)dt{
    //if the user is holding down on the screen, arm1 rotates.
    if(held == true){
        _theArm2.position = [self fixAngle];
        timer = timer + .0166; //this gets reset after touchesEnded.
        _theArm1.rotation = _theArm1.rotation - (timer * 10);
}
-(CGPoint)fixAngle{
    CGFloat theAngle = _theArm1.rotation;
//I'm not to sure how the degrees works in cocos2d, so I added 90 to the angle of rotation's original position, and it works until the rotation variable changes.
    CGFloat thyAngle = theAngle + 90; 

    CGFloat theMathx = (17*cosf(thyAngle)); //17 is the length of the arm
    CGFloat theMathy = (17*sinf(thyAngle));
    theMathx = theMathx + 100;//these 2 updates just change the position, because arm1's 
    theMathy = theMathy + 55; //CGpoint is located at ccp(100,57)

    return CGPointMake(theMathx, theMathy);
}

对不起,如果代码是......不好。我对编程比较陌生,但是除了愚蠢的 arm2 喜欢快速旋转一圈之外,一切都可以正常工作。

我会爱任何在我/他们的余生中解决我问题的人。

【问题讨论】:

  • 我认为旋转是弧度,而不是度数。我从那里开始。

标签: objective-c xcode math cocos2d-iphone geometry


【解决方案1】:

编辑:

根据关于此线程的讨论,看起来您使用的度数应该使用弧度,但不是我想的。试试这个:

CGFloat theMathx = (17*cosf(CC_DEGREES_TO_RADIANS(thyAngle))); //17 is the length of the arm
CGFloat theMathy = (17*sinf(CC_DEGREES_TO_RADIANS(thyAngle)));

尝试使用它来增加 90 度的旋转。 Cocos2D 使用弧度而不是度数:

【讨论】:

  • 错误。 Cocos 使用度数。 cocos2d-iphone.org/api-ref/latest-stable/…
  • 抱歉,你是对的。 _theArm1 和 _theArm2 可能不是 CCNode。它们可能是以弧度旋转的物体?这是一个延伸,但可以解释这种行为。
  • 是的。问题在于从度数中获取 cosf 和 sinf
  • 位置正确,但旋转错误。 cosf 和 sinf 仅用于设置位置,除非我错过了什么
  • 位置实际上是一个圆上的位置。它在代码中通过 radius*vector(cos, sin) 计算。因为传递给 cos、sin 的值不正确,所以旋转变得非常快。比预期快 180/pi 倍
猜你喜欢
  • 1970-01-01
  • 2016-09-05
  • 2016-11-29
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多