【问题标题】:Radian Angle Check in Box2d Objective-CBox2d Objective-C中的弧度角检查
【发布时间】:2013-04-12 15:02:42
【问题描述】:

我正在使用 Box2D 在 Objective C 中创建一个汽车游戏。我想根据 CCSprite(转向)旋转来旋转车轮。

为此,我成功地根据触摸设置 CCSprite 转向的角度。但是当我试图获得 Steer 旋转的值时,尽管 Steer 旋转完美,但它并没有得到完美的结果。但弧度角值有时并不完美。 所以我无法按照转向旋转来旋转车轮

我的代码是设置转向角度如下

在 Touch Begin 上,设置起始角度

-(void)getAngleSteer:(UITouch *) touch
{
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];
    float adjacent = steering.position.x - location.x;
    float opposite = steering.position.y - location.y;

    self.startAngleForSteer = atan2f(adjacent, opposite);
}

触摸移动、移动转向和设置汽车角度

-(void)rotateSteer:(UITouch *) touch
{
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];

    float adjacent = steering.position.x - location.x;
    float opposite = steering.position.y - location.y;

    float angle = atan2f(adjacent, opposite);
    angle = angle - self.startAngleForSteer;
    steering.rotation = CC_RADIANS_TO_DEGREES(angle);

    //Main issue is in below line
    NSLog(@"%f %f", angle, CC_RADIANS_TO_DEGREES(angle));       
    if(angle > M_PI/3 || angle < -M_PI/3) return;
    steering_angle = -angle;//This is for Box2D Car Angle
}

这是原木,当移动转向时,逆时针方向

-0.127680 -7.315529
-0.212759 -12.190166
-0.329367 -18.871363
5.807306 332.734131    // Why is 5.80 cuming just after -0.32 it should be decrease.
5.721369 327.810303
5.665644 324.617462

【问题讨论】:

  • atan2 返回 -pi/2 到 pi/2 en.wikipedia.org/wiki/Atan2 范围内的值
  • 我搜索了很多,也关注了您的评论。但我没有取得任何成功。如果你找到了,请帮忙。

标签: objective-c box2d angle radians


【解决方案1】:

终于找到答案了。感谢 Box2D 论坛 (http://box2d.org/forum/viewtopic.php?f=5&t=4726)

对于 Objective C,我们必须像下面的函数那样标准化角度。

-(float) normalizeAngle:(float) angle
{
    CGFloat result = (float)((int) angle % (int) (2*M_PI));

    return (result) >= 0 ? (angle < M_PI) ? angle : angle - (2*M_PI) : (angle >= -M_PI) ? angle : angle + (2*M_PI);
}

在条件检查 M_PI/3 之前调用它

steering.rotation = CC_RADIANS_TO_DEGREES(angle);
angle = [self normalizeAngle:angle];//This is where to call.
if(angle > M_PI/3 || angle < -M_PI/3) return;
steering_angle = -angle;

【讨论】:

    猜你喜欢
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多