Skip to content

Commit 53a6b54

Browse files
committedJun 26, 2017
Fix undefined behaviour in arm movement when dividing by zero
1 parent b3a36f7 commit 53a6b54

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed
 

Diff for: ‎src/camera.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,23 @@ void Camera::step(f32 dtime)
197197

198198
void Camera::addArmInertia(f32 player_yaw, f32 frametime)
199199
{
200-
m_cam_vel.X = std::fabs((m_last_cam_pos.X - player_yaw) / m_timer.X) * 0.01f;
201-
m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / m_timer.Y);
200+
if (m_timer.X == 0.0f)
201+
m_cam_vel.X = std::fabs((m_last_cam_pos.X - player_yaw)) * 0.01f;
202+
else
203+
m_cam_vel.X = std::fabs((m_last_cam_pos.X - player_yaw) / m_timer.X) * 0.01f;
204+
205+
if (m_timer.Y == 0.0f)
206+
m_cam_vel.Y = std::fabs(m_last_cam_pos.Y - m_camera_direction.Y);
207+
else
208+
m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / m_timer.Y);
202209

203210
if (m_cam_vel.X > 1.0f || m_cam_vel.Y > 1.0f) {
204211
/*
205212
the arm moves when the camera moves fast enough.
206213
*/
207214

208215
if (m_cam_vel.X > 1.0f) {
209-
m_timer.X = 0.0f;
210-
m_timer.X += frametime;
216+
m_timer.X = frametime;
211217

212218
if (m_cam_vel.X > m_cam_vel_old.X)
213219
m_cam_vel_old.X = m_cam_vel.X;
@@ -226,8 +232,7 @@ void Camera::addArmInertia(f32 player_yaw, f32 frametime)
226232
}
227233

228234
if (m_cam_vel.Y > 1.0f) {
229-
m_timer.Y = 0.0f;
230-
m_timer.Y += frametime;
235+
m_timer.Y = frametime;
231236

232237
if (m_cam_vel.Y > m_cam_vel_old.Y)
233238
m_cam_vel_old.Y = m_cam_vel.Y;

1 commit comments

Comments
 (1)

kilbith commented on Jun 27, 2017

@kilbith
Contributor

Achtung! This commit caused a regression: #6060

Please sign in to comment.