Skip to content

Commit e71e949

Browse files
authored
"change atan2_safe to atan2" followup (#7531)
Follow-up to #6255. Partially restore use of `atan2_safe` from 87ea04f: keep special case while delegating most cases to atan2. Fixes #7530.
1 parent b65a92f commit e71e949

5 files changed

Lines changed: 29 additions & 12 deletions

File tree

code/hud/hudtarget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3135,7 +3135,7 @@ void HudGaugeReticleTriangle::renderTriangle(vec3d *hostile_pos, int aspect_flag
31353135
unsize(&hostile_vertex.screen.xyw.x, &hostile_vertex.screen.xyw.y);
31363136
}
31373137

3138-
float ang = atan2(-(hostile_vertex.screen.xyw.y - tablePosY), hostile_vertex.screen.xyw.x - tablePosX);
3138+
float ang = atan2_safe(-(hostile_vertex.screen.xyw.y - tablePosY), hostile_vertex.screen.xyw.x - tablePosX);
31393139
float sin_ang=sinf(ang);
31403140
float cos_ang=cosf(ang);
31413141

code/math/vecmat.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ 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 handles the special case of x == 0 and y == 0.
70+
//
71+
float atan2_safe(float y, float x)
72+
{
73+
// special case
74+
if ( x == 0.0f && y == 0.0f )
75+
return 0.0f;
76+
77+
return atan2(y, x);
78+
}
79+
6680
// ---------------------------------------------------------------------
6781
// vm_vec_component()
6882
//
@@ -1109,7 +1123,7 @@ angles *vm_extract_angles_matrix(angles *a, const matrix *m)
11091123
{
11101124
float sinh,cosh,cosp;
11111125

1112-
a->h = atan2(m->vec.fvec.xyz.x,m->vec.fvec.xyz.z);
1126+
a->h = atan2_safe(m->vec.fvec.xyz.x,m->vec.fvec.xyz.z);
11131127

11141128
sinh = sinf(a->h); cosh = cosf(a->h);
11151129

@@ -1124,7 +1138,7 @@ angles *vm_extract_angles_matrix(angles *a, const matrix *m)
11241138

11251139
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) ) );
11261140

1127-
a->p = atan2(-m->vec.fvec.xyz.y, fvec_xz_distance);
1141+
a->p = atan2_safe(-m->vec.fvec.xyz.y, fvec_xz_distance);
11281142

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

@@ -1136,7 +1150,7 @@ angles *vm_extract_angles_matrix(angles *a, const matrix *m)
11361150
sinb = m->vec.rvec.xyz.y/cosp;
11371151
cosb = m->vec.uvec.xyz.y/cosp;
11381152

1139-
a->b = atan2(sinb,cosb);
1153+
a->b = atan2_safe(sinb,cosb);
11401154
}
11411155

11421156

@@ -1182,7 +1196,7 @@ static angles *vm_extract_angles_vector_normalized(angles *a, const vec3d *v)
11821196

11831197
a->p = asinf_safe(-v->xyz.y);
11841198

1185-
a->h = atan2(v->xyz.z,v->xyz.x);
1199+
a->h = atan2_safe(v->xyz.z,v->xyz.x);
11861200

11871201
return a;
11881202
}
@@ -1853,10 +1867,10 @@ float vm_closest_angle_to_matrix(const matrix* mat, const vec3d* rot_axis, float
18531867
//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:
18541868
const float yz_recip = (!std::numeric_limits<float>::is_iec559 && y * z < 0.001f) ? FLT_MAX : 1.0f / (y * z);
18551869

1856-
solutions = { 2.0f * atan2f(-sr_neg * (y * y + sr) * yz_recip, -2.0f * sr_neg),
1857-
2.0f * atan2f(sr_neg * (y * y + sr) * yz_recip, 2.0f * sr_neg),
1858-
2.0f * atan2f(-sr_pos * (y * y - sr) * yz_recip, -2.0f * sr_pos),
1859-
2.0f * atan2f(sr_pos * (y * y - sr) * yz_recip, 2.0f * sr_pos) };
1870+
solutions = { 2.0f * atan2_safe(-sr_neg * (y * y + sr) * yz_recip, -2.0f * sr_neg),
1871+
2.0f * atan2_safe(sr_neg * (y * y + sr) * yz_recip, 2.0f * sr_neg),
1872+
2.0f * atan2_safe(-sr_pos * (y * y - sr) * yz_recip, -2.0f * sr_pos),
1873+
2.0f * atan2_safe(sr_pos * (y * y - sr) * yz_recip, 2.0f * sr_pos) };
18601874
}
18611875
float value = -2.0f;
18621876
float correct = 0;

code/math/vecmat.h

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

512+
// Wrapper around atan2() that returns 0 for the degenerate (0, 0) input.
513+
float atan2_safe(float y, float x);
514+
512515
// Translates from world coordinates to body coordinates
513516
vec3d* vm_rotate_vec_to_body(vec3d *body_vec, const vec3d *world_vec, const matrix *orient);
514517

code/render/3ddraw.cpp

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

895895
} else {
896-
a = atan2(taily - heady, tailx - headx);
896+
a = atan2_safe(taily - heady, tailx - headx);
897897

898898
w = len_2d;
899899

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 (std::holds_alternative<asymmetric_fov>(Proj_fov)) {
9696
const auto& afov = std::get<asymmetric_fov>(Proj_fov);
97-
float angle = atan2(z, x) + (afov.left + afov.right);
97+
float angle = atan2_safe(z, x) + (afov.left + afov.right);
9898
x = angle == PI_2 ? 0.0f : z / tanf(angle);
9999

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

0 commit comments

Comments
 (0)