Skip to content

Commit

Permalink
Add Arm Inertia (#6050)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbith authored and nerzhul committed Jun 26, 2017
1 parent 936d67d commit 1d1d922
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
4 changes: 4 additions & 0 deletions builtin/settingtypes.txt
Expand Up @@ -469,6 +469,10 @@ enable_waving_plants (Waving plants) bool false

[***Advanced]

# Arm inertia, gives a more realistic movement of
# the arm when the camera moves.
arm_inertia (Arm inertia) bool true

# If FPS would go higher than this, limit it by sleeping
# to not waste CPU power for no benefit.
fps_max (Maximum FPS) int 60
Expand Down
5 changes: 5 additions & 0 deletions minetest.conf.example
Expand Up @@ -534,6 +534,11 @@

#### Advanced

# Arm inertia, gives a more realistic movement of
# the arm when the camera moves.
# type: bool
# arm_inertia = true

# If FPS would go higher than this, limit it by sleeping
# to not waste CPU power for no benefit.
# type: int
Expand Down
83 changes: 80 additions & 3 deletions src/camera.cpp
Expand Up @@ -75,6 +75,7 @@ Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
m_cache_view_bobbing_amount = g_settings->getFloat("view_bobbing_amount");
m_cache_fov = g_settings->getFloat("fov");
m_cache_zoom_fov = g_settings->getFloat("zoom_fov");
m_arm_inertia = g_settings->getBool("arm_inertia");
m_nametags.clear();
}

Expand Down Expand Up @@ -193,6 +194,82 @@ void Camera::step(f32 dtime)
}
}

void Camera::add_arm_inertia(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_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;

if (m_cam_vel.X > m_cam_vel_old.X)
m_cam_vel_old.X = m_cam_vel.X;

if (m_last_cam_pos.X > player_yaw)
// right
m_wieldmesh_offset.X -= (0.1f + frametime) * m_cam_vel.X;
else
// left
m_wieldmesh_offset.X += (0.1f + frametime) * m_cam_vel.X;

if (m_last_cam_pos.X != player_yaw)
m_last_cam_pos.X = player_yaw;

m_wieldmesh_offset.X = rangelim(m_wieldmesh_offset.X, 48.0f, 62.0f);
}

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

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

if (m_last_cam_pos.Y > m_camera_direction.Y)
// down
m_wieldmesh_offset.Y += (0.1f + frametime) * m_cam_vel.Y;
else
// up
m_wieldmesh_offset.Y -= (0.1f + frametime) * m_cam_vel.Y;

if (m_last_cam_pos.Y != m_camera_direction.Y)
m_last_cam_pos.Y = m_camera_direction.Y;

m_wieldmesh_offset.Y = rangelim(m_wieldmesh_offset.Y, -45.0f, -30.0f);
}
} else {
/*
the arm now gets back to its default position when the camera stops.
*/

if (floor(m_wieldmesh_offset.X) == 55.0f) {
m_cam_vel_old.X = 0.0f;
m_wieldmesh_offset.X = 55.0f;
}

if (m_wieldmesh_offset.X > 55.0f)
m_wieldmesh_offset.X -= (0.05f + frametime) * m_cam_vel_old.X;
if (m_wieldmesh_offset.X < 55.0f)
m_wieldmesh_offset.X += (0.05f + frametime) * m_cam_vel_old.X;

if (floor(m_wieldmesh_offset.Y) == -35.0f) {
m_cam_vel_old.Y = 0.0f;
m_wieldmesh_offset.Y = -35.0f;
}

if (m_wieldmesh_offset.Y > -35.0f)
m_wieldmesh_offset.Y -= (0.05f + frametime) * m_cam_vel_old.Y;
if (m_wieldmesh_offset.Y < -35.0f)
m_wieldmesh_offset.Y += (0.05f + frametime) * m_cam_vel_old.Y;
}
}

void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime,
f32 tool_reload_ratio, ClientEnvironment &c_env)
{
Expand Down Expand Up @@ -380,12 +457,12 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime,
m_cameranode->setAspectRatio(m_aspect);
m_cameranode->setFOV(m_fov_y);

float wieldmesh_offset_Y = -35 + player->getPitch() * 0.05;
wieldmesh_offset_Y = rangelim(wieldmesh_offset_Y, -52, -32);
if (m_arm_inertia)
add_arm_inertia(player->getYaw(), frametime);

// Position the wielded item
//v3f wield_position = v3f(45, -35, 65);
v3f wield_position = v3f(55, wieldmesh_offset_Y, 65);
v3f wield_position = v3f(m_wieldmesh_offset.X, m_wieldmesh_offset.Y, 65);
//v3f wield_rotation = v3f(-100, 120, -100);
v3f wield_rotation = v3f(-100, 120, -100);
wield_position.Y += fabs(m_wield_change_timer)*320 - 40;
Expand Down
9 changes: 9 additions & 0 deletions src/camera.h
Expand Up @@ -166,6 +166,8 @@ class Camera

void drawNametags();

inline void add_arm_inertia(f32 player_yaw, f32 frametime);

private:
// Nodes
scene::ISceneNode *m_playernode = nullptr;
Expand All @@ -188,6 +190,12 @@ class Camera
// Camera offset
v3s16 m_camera_offset;

v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
v2f m_timer;
v2f m_cam_vel;
v2f m_cam_vel_old;
v2f m_last_cam_pos;

// Field of view and aspect ratio stuff
f32 m_aspect = 1.0f;
f32 m_fov_x = 1.0f;
Expand Down Expand Up @@ -221,6 +229,7 @@ class Camera
f32 m_cache_view_bobbing_amount;
f32 m_cache_fov;
f32 m_cache_zoom_fov;
bool m_arm_inertia;

std::list<Nametag *> m_nametags;
};
Expand Down
1 change: 1 addition & 0 deletions src/defaultsettings.cpp
Expand Up @@ -171,6 +171,7 @@ void set_default_settings(Settings *settings)
settings->setDefault("ambient_occlusion_gamma", "2.2");
settings->setDefault("enable_shaders", "true");
settings->setDefault("enable_particles", "true");
settings->setDefault("arm_inertia", "true");

settings->setDefault("enable_minimap", "true");
settings->setDefault("minimap_shape_round", "true");
Expand Down

0 comments on commit 1d1d922

Please sign in to comment.