math - 弧度 atan2 显示相反的值,然后是它们需要的值
问题描述
double shootx = vx + dx / t0;
double shooty = vy + dy / t0;
double radians = atan2((double)-shooty, shootx);
deg_to_aim = (int)((radians * 360) / (2 * 3.141592653589793238462));
myprintf("(A) radians = %f deg to aim = %d\n", radians, deg_to_aim);
radians 3.14 = 180 should be 0
radians 0 = 0 should be 180
radians -1.584827 = -90 should be 90
radians -1579912 = 90 should be -90
如何使值在所有方面都正确显示。目前,如果我围绕该点旋转,它将显示一个向外的运动,就像在实际点后面一样,当它应该始终指向该点时。它也从不179
打到.-179
180
完整代码如下所示
/* Relative player position */
float const dx = (MyShip.XCoordinate + 18) - (Enemy.XCoordinate + 18);
float const dy = (MyShip.YCoordinate + 18) - (Enemy.YCoordinate + 18);
/* Relative player velocity */
float const vx = MyShip.XSpeed - Enemy.XSpeed;
float const vy = MyShip.YSpeed - Enemy.YSpeed;
float const a = vx * vx + vy * vy - bulletSpeed * bulletSpeed;
float const b = 2.f * (vx * dx + vy * dy);
float const c = dx * dx + dy * dy;
float const discriminant = b * b - 4.f * a * c;
int deg_to_aim = 0;
if (discriminant >= 0) {
float t0 = (float)(-b + sqrt(discriminant)) / (2 * a);
float t1 = (float)(-b - sqrt(discriminant)) / (2 * a);
if (t0 < 0.f || (t1 < t0 && t1 >= 0.f))
t0 = t1;
if (t0 >= 0.f)
{
// Aim at
double shootx = vx + dx / t0;
double shooty = vy + dy / t0;
double radians = atan2((double)-shooty, shootx);
deg_to_aim = (int)((radians * 360) / (2 * 3.141592653589793238462));
myprintf("(A) radians = %f deg to aim = %d\n", radians, deg_to_aim);
}
}
else {
myprintf("Error found!!!!!!! no solution\n");
}
解决方案
修复它只是翻转 MyShip Enemy 值。
而不是MyShip - Enemy
你做Enemy - MyShip
推荐阅读
- reactjs - npm包中antd的使用
- azure-blob-storage - Azure Blob:如果目标容器中的虚拟目录路径不完全匹配,则 StartCopyAsync 失败
- python - 如何优雅地在python中串行(或区分)函数参数或串行函数调用结果?
- c - 当目标值等于程序中的 res 值时,如何终止程序?
- android - 如何从 UmmalquraCalendar 单击按钮获取下一个和上一个日期?
- javascript - 云消息传递在 react-native-firebase v6.xx 中如何工作?
- laravel - 作曲家版本要求
- python - 包装python字典或设置到新行
- sql - 如何将表 ID 从串行更改为身份?
- boost - 在 Mac 上安装 Apache Pulsar C++ 客户端