Skip to content

Commit 87ea04f

Browse files
committed
change atan2_safe to atan2
According to DahBlount: > Most atan2 implementations for a long time had problems when the inputs were close to machine epsilon or when x was less than or equal to 0 And according to z64555: > Modern versions of C++ should have an atan2() that works correctly when the inputs are at 0 or near epsilon of the floating point specification, meaning Volition's workaround shouldn't be needed anymore. So this removes `atan2_safe` and changes all instances to `atan2`. Consequently, the angle range in FRED now shows as [-180, 180] as expected. Fixes scp-fs2open#3730.
1 parent f5a268b commit 87ea04f

File tree

5 files changed

+12
-45
lines changed

5 files changed

+12
-45
lines changed

code/hud/hudtarget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3046,7 +3046,7 @@ void HudGaugeReticleTriangle::renderTriangle(vec3d *hostile_pos, int aspect_flag
30463046
// even if the screen position overflowed, it will still be pointing in the correct direction
30473047
unsize( &hostile_vertex.screen.xyw.x, &hostile_vertex.screen.xyw.y );
30483048

3049-
ang = atan2_safe(-(hostile_vertex.screen.xyw.y - tablePosY), hostile_vertex.screen.xyw.x - tablePosX);
3049+
ang = atan2(-(hostile_vertex.screen.xyw.y - tablePosY), hostile_vertex.screen.xyw.x - tablePosX);
30503050
sin_ang=sinf(ang);
30513051
cos_ang=cosf(ang);
30523052

code/math/vecmat.cpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,36 +63,6 @@ bool vm_matrix_equal(const matrix4 &self, const matrix4 &other)
6363
vm_vec_equal(self.vec.pos, other.vec.pos);
6464
}
6565

66-
// -----------------------------------------------------------
67-
// atan2_safe()
68-
//
69-
// Wrapper around atan2() that used atanf() to calculate angle. Safe
70-
// for optimized builds. Handles special cases when x == 0.
71-
//
72-
float atan2_safe(float y, float x)
73-
{
74-
float ang;
75-
76-
// special case, x == 0
77-
if ( x == 0.0f ) {
78-
if ( y == 0.0f )
79-
ang = 0.0f;
80-
else if ( y > 0.0f )
81-
ang = PI_2;
82-
else
83-
ang = -PI_2;
84-
85-
return ang;
86-
}
87-
88-
ang = atanf(y/x);
89-
if ( x < 0.0f ){
90-
ang += PI;
91-
}
92-
93-
return ang;
94-
}
95-
9666
// ---------------------------------------------------------------------
9767
// vm_vec_component()
9868
//
@@ -981,7 +951,7 @@ angles *vm_extract_angles_matrix(angles *a, const matrix *m)
981951
{
982952
float sinh,cosh,cosp;
983953

984-
a->h = atan2_safe(m->vec.fvec.xyz.x,m->vec.fvec.xyz.z);
954+
a->h = atan2(m->vec.fvec.xyz.x,m->vec.fvec.xyz.z);
985955

986956
sinh = sinf(a->h); cosh = cosf(a->h);
987957

@@ -996,7 +966,7 @@ angles *vm_extract_angles_matrix(angles *a, const matrix *m)
996966

997967
fvec_xz_distance = fl_sqrt( ( (m->vec.fvec.xyz.x)*(m->vec.fvec.xyz.x) ) + ( (m->vec.fvec.xyz.z)*(m->vec.fvec.xyz.z) ) );
998968

999-
a->p = atan2_safe(-m->vec.fvec.xyz.y, fvec_xz_distance);
969+
a->p = atan2(-m->vec.fvec.xyz.y, fvec_xz_distance);
1000970

1001971
if (cosp == 0.0f) //the cosine of pitch is zero. we're pitched straight up. say no bank
1002972

@@ -1008,7 +978,7 @@ angles *vm_extract_angles_matrix(angles *a, const matrix *m)
1008978
sinb = m->vec.rvec.xyz.y/cosp;
1009979
cosb = m->vec.uvec.xyz.y/cosp;
1010980

1011-
a->b = atan2_safe(sinb,cosb);
981+
a->b = atan2(sinb,cosb);
1012982
}
1013983

1014984

@@ -1054,7 +1024,7 @@ static angles *vm_extract_angles_vector_normalized(angles *a, const vec3d *v)
10541024

10551025
a->p = asinf_safe(-v->xyz.y);
10561026

1057-
a->h = atan2_safe(v->xyz.z,v->xyz.x);
1027+
a->h = atan2(v->xyz.z,v->xyz.x);
10581028

10591029
return a;
10601030
}
@@ -1723,10 +1693,10 @@ float vm_closest_angle_to_matrix(const matrix* mat, const vec3d* rot_axis, float
17231693
//If we support IEEE float handling, we don't need this, the div by 0 will be handled correctly with the INF. If not, do this:
17241694
const float yz_recip = (!std::numeric_limits<float>::is_iec559 && y * z < 0.001f) ? FLT_MAX : 1.0f / (y * z);
17251695

1726-
solutions = { 2 * atan2_safe(-sr_neg * (y * y + sr) * yz_recip, -2 * sr_neg),
1727-
2 * atan2_safe(sr_neg * (y * y + sr) * yz_recip, 2 * sr_neg),
1728-
2 * atan2_safe(-sr_pos * (y * y - sr) * yz_recip, -2 * sr_pos),
1729-
2 * atan2_safe(sr_pos * (y * y - sr) * yz_recip, 2 * sr_pos) };
1696+
solutions = { 2 * atan2(-sr_neg * (y * y + sr) * yz_recip, -2 * sr_neg),
1697+
2 * atan2(sr_neg * (y * y + sr) * yz_recip, 2 * sr_neg),
1698+
2 * atan2(-sr_pos * (y * y - sr) * yz_recip, -2 * sr_pos),
1699+
2 * atan2(sr_pos * (y * y - sr) * yz_recip, 2 * sr_pos) };
17301700
}
17311701
float value = -2.0f;
17321702
float correct = 0;

code/math/vecmat.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,6 @@ void vm_angular_move_forward_vec(const vec3d *goal_fvec, const matrix *orient, c
485485
// Find the bounding sphere for a set of points (center and radius are output parameters)
486486
void vm_find_bounding_sphere(const vec3d *pnts, int num_pnts, vec3d *center, float *radius);
487487

488-
// Version of atan2() that is safe for optimized builds
489-
float atan2_safe(float x, float y);
490-
491488
// Translates from world coordinates to body coordinates
492489
vec3d* vm_rotate_vec_to_body(vec3d *body_vec, const vec3d *world_vec, const matrix *orient);
493490

code/render/3ddraw.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ void g3_render_laser_2d(material *mat_params, vec3d *headp, float head_width, ve
887887
w = len_2d;
888888

889889
} else {
890-
a = atan2_safe(taily - heady, tailx - headx);
890+
a = atan2(taily - heady, tailx - headx);
891891

892892
w = len_2d;
893893

code/render/3dmath.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ ubyte g3_transfer_vertex(vertex *dest, const vec3d *src)
9494
static void g3_compensate_asymmetric_fov(float& x, float& y, float z) {
9595
if (mpark::holds_alternative<asymmetric_fov>(Proj_fov)) {
9696
const auto& afov = mpark::get<asymmetric_fov>(Proj_fov);
97-
float angle = atan2_safe(z, x) + (afov.left + afov.right);
97+
float angle = atan2(z, x) + (afov.left + afov.right);
9898
x = angle == PI_2 ? 0.0f : z / tanf(angle);
9999

100-
angle = atan2_safe(z, y) + (afov.up + afov.down);
100+
angle = atan2(z, y) + (afov.up + afov.down);
101101
y = angle == PI_2 ? 0.0f : z / tanf(angle);
102102
}
103103
}

0 commit comments

Comments
 (0)