iphone - 导致奇怪路径的角度

标签 iphone ios math cocos2d-iphone

我正在使用 cocos2d 制作游戏,我正在尝试从当前敌人位置在一定长度的敌人角度上创建射击路径。

这是我用来旋转和射击枪的代码。

gun runAction: [CCSequence actions: [CCDelayTime actionWithDuration:2.0],
                 [CCCallBlockN actionWithBlock:^(CCNode *node)
                  { [self shootEnemyLaserFromPositionAtAngle:gun];}],
                 [CCRotateTo actionWithDuration:0.5 angle:250],
                 [CCCallBlockN actionWithBlock:^(CCNode *node)
                  { [self shootEnemyLaserFromPositionAtAngle:gun];}],
                 [CCRotateTo actionWithDuration:0.5 angle:230],
                 [CCCallBlockN actionWithBlock:^(CCNode *node)

我正在使用这个等式来计算矢量(这段代码在 shootEnemyLaserFromPositionAtAngle 中):

CGPoint endPoint = CGPointMake(gun.position.x + (800 * cos(gun.rotation)), gun.position.y + (800 * sin(gun.rotation)));
NSLog(@"end Point x: %f, %f", endPoint.x, endPoint.y);

shipLaser.position = gun.position;

CGPoint shootVector = ccpNormalize(ccpSub(endPoint, shipLaser.position));
CGPoint shootTarget = ccpMult(shootVector, _winSize.width*2);

[shipLaser runAction:[CCSequence actions: [CCMoveBy actionWithDuration:4.0 position:shootTarget],
                      [CCCallFuncN actionWithTarget:self selector:@selector(invisNode:)], nil]];

我的问题是它最终会朝奇怪的方向发射。我的端点值是这样打印出来的。我还在下面打印出了枪的旋转角度:

angle: -90.000000
end Point x: 21.541107, -499.593536
angle: -110.000000
end Point x: -419.216644, 250.997940
angle: -130.000000
end Point x: 86.166939, 959.688538

我注意到角度突然变负了。我尝试更改为正角度(将 360 度添加到负角度),但这导致了相同的终点。

任何人都可以帮助或给我一些尝试的东西吗??

谢谢!

最佳答案

cos()sin() 函数都从弧度输入返回一个值。您想使用 CC_RADIANS_TO_DEGREES 来修复它。

关于iphone - 导致奇怪路径的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12681581/

相关文章:

iphone - 应用程序图标与 MonoTouch 中的 57 和 114 字段图标不匹配!

ios - UICollectionViewCell 背景图像超出单元格

iOS 遍历对象的属性并添加操作

ios - appDelegate 实践

iOS 检索远程通知负载

Java - 如何检查点是否在圆的切片内

iphone - NSPredicate 获取 NSSet CoreData 中的特定数据?

iphone - 如何在 UIView 和 EAGLView 之间进行混合?

python - 根据费马小定理解释代码以检查素数

ios - 精确的 3D 缩放缩放