Skip to content

Commit

Permalink
Improved scrolling in KinematicArm
Browse files Browse the repository at this point in the history
  • Loading branch information
manglemix committed Nov 18, 2020
1 parent d690f40 commit 6008f7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 8 additions & 5 deletions TrokaraScripts/kinematic_arm.gd
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# A GTA style arm for cameras
# Will only shorten the arm if the end is intersecting an obstacle
# So the arm can intersect small obstacles without shortening
class_name KinematicArm
extends Spatial

Expand Down Expand Up @@ -69,7 +72,7 @@ func _ready():


func _physics_process(delta):
# warning-ignore:return_value_discarded
current_length = lerp(current_length, target_length, weight * delta)
var collision_info := _move_kinematic_body()

if is_instance_valid(collision_info):
Expand All @@ -79,21 +82,21 @@ func _physics_process(delta):
else:
var relative_origin := global_transform.origin - kinematic_body.global_transform.origin
var arm_vector := - global_transform.basis.z * current_length
var perpendicular_vector_length := arm_vector.dot(collision_info.normal)
var perpendicular_arm_length := arm_vector.dot(collision_info.normal)
var perpendicular_distance := collision_info.normal.dot(relative_origin)
var ratio := abs(perpendicular_distance / perpendicular_vector_length)
var ratio := abs(perpendicular_distance / perpendicular_arm_length)
var correction_vector := arm_vector.slide(collision_info.normal) * ratio + relative_origin.slide(collision_info.normal)

if correction_vector.dot(arm_vector) > 0:
_reset_kinematic_body()

else:
# warning-ignore:return_value_discarded
kinematic_body.move_and_collide(correction_vector)
current_length = arm_vector.length() * ratio
current_length *= ratio
_update_children()

else:
current_length = lerp(current_length, target_length, weight * delta)
_update_children()


Expand Down
4 changes: 3 additions & 1 deletion TrokaraScripts/scrollable_kinematic_arm.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ extends KinematicArm


export var scroll_step := 0.5 # how much the arm moves with each scroll
export var max_length := 10.0
export var max_length := 10.0 # the maximum length of the arm


func _input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_WHEEL_UP:
print(target_length)
target_length = clamp(current_length - scroll_step, min_length, max_length)
print(target_length)

elif event.button_index == BUTTON_WHEEL_DOWN:
target_length = clamp(current_length + scroll_step, min_length, max_length)

0 comments on commit 6008f7f

Please sign in to comment.