diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-08-28 00:06:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-28 00:06:25 +0200 |
commit | 7e8a7dea2e76fe4fb49f336228fb8dc7739f83fa (patch) | |
tree | b12282fcda6ff2c0f2830c13128377b9eb7d9337 /editor | |
parent | 6ef7783abb4ea31e6c59b4c30a05c7249b361020 (diff) | |
parent | 28e1c88f6e6968c9fa8e91220d9d795e11b2da7b (diff) |
Merge pull request #10667 from Zylann/freelook_inertia
Added freelook inertia
Diffstat (limited to 'editor')
-rw-r--r-- | editor/editor_settings.cpp | 1 | ||||
-rw-r--r-- | editor/plugins/spatial_editor_plugin.cpp | 46 | ||||
-rw-r--r-- | editor/plugins/spatial_editor_plugin.h | 1 |
3 files changed, 30 insertions, 18 deletions
diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index c30ffd4b4e..b9a6414b08 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -674,6 +674,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { set("editors/3d/warped_mouse_panning", true); set("editors/3d/orbit_sensitivity", 0.4); + set("editors/3d/freelook_inertia", 3); set("editors/3d/freelook_base_speed", 1); diff --git a/editor/plugins/spatial_editor_plugin.cpp b/editor/plugins/spatial_editor_plugin.cpp index 01dd7a0031..d8f01c6b60 100644 --- a/editor/plugins/spatial_editor_plugin.cpp +++ b/editor/plugins/spatial_editor_plugin.cpp @@ -1608,8 +1608,10 @@ Point2i SpatialEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMous void SpatialEditorViewport::_update_freelook(real_t delta) { - if (!is_freelook_active()) + if (!is_freelook_active()) { + freelook_velocity = Vector3(); return; + } Vector3 forward = camera->get_transform().basis.xform(Vector3(0, 0, -1)); Vector3 right = camera->get_transform().basis.xform(Vector3(1, 0, 0)); @@ -1623,53 +1625,61 @@ void SpatialEditorViewport::_update_freelook(real_t delta) { int key_down = Object::cast_to<InputEventKey>(ED_GET_SHORTCUT("spatial_editor/freelook_down")->get_shortcut().ptr())->get_scancode(); int key_speed_modifier = Object::cast_to<InputEventKey>(ED_GET_SHORTCUT("spatial_editor/freelook_speed_modifier")->get_shortcut().ptr())->get_scancode(); - Vector3 velocity; + Vector3 direction; bool pressed = false; bool speed_modifier = false; const Input &input = *Input::get_singleton(); if (input.is_key_pressed(key_left)) { - velocity -= right; + direction -= right; pressed = true; } if (input.is_key_pressed(key_right)) { - velocity += right; + direction += right; pressed = true; } if (input.is_key_pressed(key_forward)) { - velocity += forward; + direction += forward; pressed = true; } if (input.is_key_pressed(key_backwards)) { - velocity -= forward; + direction -= forward; pressed = true; } if (input.is_key_pressed(key_up)) { - velocity += up; + direction += up; pressed = true; } if (input.is_key_pressed(key_down)) { - velocity -= up; + direction -= up; pressed = true; } if (input.is_key_pressed(key_speed_modifier)) { speed_modifier = true; } - if (pressed) { - const EditorSettings &s = *EditorSettings::get_singleton(); - const real_t base_speed = s.get("editors/3d/freelook_base_speed"); - const real_t modifier_speed_factor = s.get("editors/3d/freelook_modifier_speed_factor"); + const EditorSettings &s = *EditorSettings::get_singleton(); + real_t inertia = s.get("editors/3d/freelook_inertia"); + if (inertia < 0) + inertia = 0; - real_t speed = base_speed * cursor.distance; - if (speed_modifier) - speed *= modifier_speed_factor; + const real_t base_speed = s.get("editors/3d/freelook_base_speed"); + const real_t modifier_speed_factor = s.get("editors/3d/freelook_modifier_speed_factor"); - velocity.normalize(); + real_t speed = base_speed * cursor.distance; + if (speed_modifier) + speed *= modifier_speed_factor; - cursor.pos += velocity * (speed * delta); - } + Vector3 instant_velocity = direction * speed; + + // Higher inertia should increase "lag" (lerp with factor between 0 and 1) + // Inertia of zero should produce instant movement (lerp with factor of 1) + // Takes reference of 60fps for units, so that inertia of 1 gives approximate lerp factor of 0.5 + real_t factor = 1.0 / (1.0 + inertia * delta * 60.f); + freelook_velocity = freelook_velocity.linear_interpolate(instant_velocity, CLAMP(factor, 0, 1)); + + cursor.pos += freelook_velocity * delta; } void SpatialEditorViewport::set_message(String p_message, float p_time) { diff --git a/editor/plugins/spatial_editor_plugin.h b/editor/plugins/spatial_editor_plugin.h index a2bfd9ec4b..b024a8bd93 100644 --- a/editor/plugins/spatial_editor_plugin.h +++ b/editor/plugins/spatial_editor_plugin.h @@ -122,6 +122,7 @@ private: float gizmo_scale; bool freelook_active; + Vector3 freelook_velocity; PanelContainer *info; Label *info_label; |