Skip to content

Commit

Permalink
Fix undefined behaviour in arm movement when dividing by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzhul committed Jun 26, 2017
1 parent b3a36f7 commit 53a6b54
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,23 @@ void Camera::step(f32 dtime)

void Camera::addArmInertia(f32 player_yaw, f32 frametime)
{
m_cam_vel.X = std::fabs((m_last_cam_pos.X - player_yaw) / m_timer.X) * 0.01f;
m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / m_timer.Y);
if (m_timer.X == 0.0f)
m_cam_vel.X = std::fabs((m_last_cam_pos.X - player_yaw)) * 0.01f;
else
m_cam_vel.X = std::fabs((m_last_cam_pos.X - player_yaw) / m_timer.X) * 0.01f;

if (m_timer.Y == 0.0f)
m_cam_vel.Y = std::fabs(m_last_cam_pos.Y - m_camera_direction.Y);
else
m_cam_vel.Y = std::fabs((m_last_cam_pos.Y - m_camera_direction.Y) / m_timer.Y);

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

if (m_cam_vel.X > 1.0f) {
m_timer.X = 0.0f;
m_timer.X += frametime;
m_timer.X = frametime;

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

if (m_cam_vel.Y > 1.0f) {
m_timer.Y = 0.0f;
m_timer.Y += frametime;
m_timer.Y = frametime;

if (m_cam_vel.Y > m_cam_vel_old.Y)
m_cam_vel_old.Y = m_cam_vel.Y;
Expand Down

1 comment on commit 53a6b54

@kilbith
Copy link
Contributor

@kilbith kilbith commented on 53a6b54 Jun 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Achtung! This commit caused a regression: #6060

Please sign in to comment.