【发布时间】: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