我有一个关于atan2()的简单问题,那就是如何从炮塔获得角度,以便它跟随宇宙飞船到达的地方。
我有一个从炮塔到SpaceShip的向量,但据我所知,atan2f给出了从斜边到0度线的角度。
如果我错了,请纠正我。

我希望那个角度突出显示为(蓝色),这样它就会跟随飞船的去向。
以下是我的代码:
-(void) upDateTurret:(CCTime)delta{
CGPoint playerToCannonVector = ccpSub(_playerSprite.position, _turretSprite.position);
float angle = atan2f(playerToCannonVector.y, playerToCannonVector.x);
_turretSprite.rotation = 90.0f - CC_RADIANS_TO_DEGREES(angle);
}这给了我正确的结果,但是如何得到呢?因为atan2f给出了从斜边到0度线的角度(红色角度)。
发布于 2015-09-08 13:14:45
这是我使用的:
// Calculates the angle from one point to another, in radians.
//
+ (float) angleFromPoint:(CGPoint)from toPoint:(CGPoint)to {
CGPoint pnormal = ccpSub(to, from);
float radians = atan2f(pnormal.x, pnormal.y);
return radians;
}它基本上与你自己的代码相匹配;唯一真正的区别是你从90.0度中减去了结果,这是我认为你遗漏的。请记住,旋转0度通常会将“北”指向屏幕的顶部(至少对我来说是这样)。
要将角度从north=0转换为east=0,我使用:
// Converts an angle in the world where 0 is north in a clockwise direction to a world
// where 0 is east in an anticlockwise direction.
//
+ (float) angleFromDegrees:(float)deg {
return fmodf((450.0f - deg), 360.0);
}https://stackoverflow.com/questions/32435248
复制相似问题