代码之家  ›  专栏  ›  技术社区  ›  SSpoke

radian atan2显示的值正好相反

  •  -2
  • SSpoke  · 技术社区  · 6 年前
    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");
            }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   SSpoke    6 年前

    MyShip - Enemy
    Enemy - MyShip