aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoic Blot <loic.blot@unix-experience.fr>2017-06-26 23:19:47 +0200
committerLoic Blot <loic.blot@unix-experience.fr>2017-06-26 23:22:42 +0200
commit53a6b5439eac140bbbe76441a4d6a02590fadb2f (patch)
tree2b593d5f7787dfba85e53f8a68512fe3d67bc983
parentb3a36f7378ea0f299cfa36c81de42e00adb7292d (diff)
downloadminetest-53a6b5439eac140bbbe76441a4d6a02590fadb2f.tar.gz
minetest-53a6b5439eac140bbbe76441a4d6a02590fadb2f.tar.bz2
minetest-53a6b5439eac140bbbe76441a4d6a02590fadb2f.zip
Fix undefined behaviour in arm movement when dividing by zero
-rw-r--r--src/camera.cpp17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/camera.cpp b/src/camera.cpp
index 83239fe7c..855bca3ad 100644
--- a/src/camera.cpp
+++ b/src/camera.cpp
@@ -197,8 +197,15 @@ 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) {
/*
@@ -206,8 +213,7 @@ void Camera::addArmInertia(f32 player_yaw, f32 frametime)
*/
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;
@@ -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;