♻️ enhance readability, lerp both first and third person rotations, and set initial camera rotation to improve accuracy in relative motion derivation

This commit is contained in:
anyreso 2024-12-10 16:56:19 -05:00
parent 8f94ae2a0f
commit fae82c45f8
3 changed files with 56 additions and 39 deletions

View file

@ -17,7 +17,7 @@ class_name PlayerInputController extends Node
@export var jetting:bool = false
@export var skiing:bool = false
@export var direction:Vector2
@export var camera_rotation:Vector2
@export var camera_rotation:Vector3
signal jump
signal primary(pressed: bool)

View file

@ -133,8 +133,8 @@ func _ready() -> void:
func(_source: Node, target: Node, amount: int) -> void:
target.health.damage(amount, 0))
input.camera_rotation.x = rotation.x
input.camera_rotation.y = rotation.y
# sets initial camera rotation to match global rotation of player
input.camera_rotation = global_rotation
if _is_pawn():
pawn_player = self
@ -160,32 +160,29 @@ func _ready() -> void:
third_person.show()
%Inventory.hide()
hud.hide()
input.camera_rotation = pivot.rotation
func _process(_delta:float) -> void:
if not _is_pawn():
if not is_alive():
return
if _is_pawn():
# handle pivot rotation
pivot.rotation.x = lerp_angle(pivot.rotation.x, input.camera_rotation.x, .5)
pivot.rotation.y = lerp_angle(pivot.rotation.y, input.camera_rotation.y, .5)
# handle throw force display
if hud.throw_progress.is_visible_in_tree():
var time_elapsed: float = throw_duration_max - throw_timer.time_left
hud.throw_progress.set_value(time_elapsed / throw_duration_max * 100)
else:
if Game.type is Multiplayer and Game.type.mode == Multiplayer.Mode.FREE_FOR_ALL:
iff.fill = Color.RED
elif is_instance_valid(pawn_player) and team_id:
iff.fill = Color.GREEN if team_id == pawn_player.team_id else Color.RED
_update_third_person_animations()
if not is_alive():
return
# compute target rotation from input.camera_rotation
var target_euler := Vector3(input.camera_rotation.x, input.camera_rotation.y, 0.0)
# smoothly interpolate rotation of pivot node towards target rotation
pivot.global_transform.basis = pivot.global_transform.basis.slerp(Basis.from_euler(target_euler), .6)
if not _is_pawn():
# compute target rotation from input.camera_rotation
var tp_target_euler := Vector3(.0, input.camera_rotation.y + PI, 0.0)
# smoothly interpolate rotation of third person node towards target rotation
tp_mesh.global_transform.basis = tp_mesh.global_transform.basis.slerp(Basis.from_euler(tp_target_euler), .6)
else:
if hud.throw_progress.is_visible_in_tree():
var time_elapsed: float = throw_duration_max - throw_timer.time_left
hud.throw_progress.set_value(time_elapsed / throw_duration_max * 100)
# handle third person mesh rotation
tp_mesh.rotation.y = lerp_angle(tp_mesh.rotation.y, input.camera_rotation.y + PI, .6)
func _physics_process(delta:float) -> void:
_update_jetpack_energy(delta)
@ -271,34 +268,33 @@ func _integrate_forces(_state:PhysicsDirectBodyState3D) -> void:
# skip if player is dead
if not is_alive():
return
# compute direction in local space
var _direction:Vector3 = (transform.basis * Vector3(
input.direction.x, 0, input.direction.y)).normalized()
# adjust direction based on pivot rotation
_direction = _direction.rotated(Vector3.UP, pivot.rotation.y)
var world_direction := Vector3(input.direction.x, 0.0, input.direction.y)
var view_direction := camera.global_transform.basis * world_direction
if is_on_floor():
if not _direction.is_zero_approx() and not input.skiing:
# retrieve collision normal
var normal:Vector3 = walkable_surface_sensor.get_collision_normal(0)
# retrieve collision normal
var normal:Vector3 = walkable_surface_sensor.get_collision_normal(0)
if not view_direction.is_zero_approx() and not input.skiing:
# calculate the angle between the ground normal and the up vector
var slope_angle:float = rad_to_deg(acos(normal.dot(Vector3.UP)))
# check if the slope angle exceeds the maximum slope angle
if slope_angle <= max_floor_angle:
# adjust direction based on the floor normal to align with the slope
_direction = _direction.slide(normal)
view_direction = view_direction.slide(normal)
linear_velocity = lerp(linear_velocity, _direction * ground_speed, .1)
linear_velocity = lerp(linear_velocity, view_direction * ground_speed, .1)
if _jumping:
var v:float = sqrt(2. * g * jump_height)
apply_central_impulse(Vector3(0., mass * v, 0.))
_jumping = false
_handle_aerial_control(_direction)
_handle_jetpack(_direction)
_handle_aerial_control(view_direction)
_handle_jetpack(view_direction)
_handle_ski()
func _update_third_person_animations() -> void:

View file

@ -283,7 +283,7 @@ fov = 90.0
[node name="Inventory" type="Node3D" parent="Pivot"]
unique_name_in_owner = true
transform = Transform3D(-1, 0, 8.74228e-08, 0, 1, 0, -8.74228e-08, 0, -1, 0.15, -0.2, -0.45)
transform = Transform3D(-1, 0, 1.50996e-07, 0, 1, 0, -1.50996e-07, 0, -1, 0.15, -0.2, -0.45)
script = ExtResource("6_du54c")
[node name="SpaceGun" parent="Pivot/Inventory" instance=ExtResource("9_achlo")]
@ -320,7 +320,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
visible = false
[node name="Mesh" parent="ThirdPerson" node_paths=PackedStringArray("spine_ik_target_attachment") instance=ExtResource("8_eiy7q")]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0)
transform = Transform3D(-1, 0, -1.50996e-07, 0, 1, 0, 1.50996e-07, 0, -1, 0, 0, 0)
spine_ik_target_attachment = NodePath("../../Pivot/SpineIKTarget")
[node name="Skeleton3D" parent="ThirdPerson/Mesh/Node" index="0"]
@ -400,20 +400,32 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2.6068e-14, -1.19209e-07, 9.5
[node name="sides" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/SpaceGun/Mesh/Armature/Skeleton3D/sides" index="0"]
layers = 2
[node name="BarrelsInner" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/ChainGun/Armature/Skeleton3D" index="0"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.692504, 1.30946)
[node name="BarrelsInner" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/ChainGun/Armature/Skeleton3D/BarrelsInner" index="0"]
layers = 2
[node name="BarrelsOuter" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/ChainGun/Armature/Skeleton3D" index="1"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.692504, 1.30946)
[node name="BarrelsOuter" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/ChainGun/Armature/Skeleton3D/BarrelsOuter" index="0"]
layers = 2
[node name="Base" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/ChainGun/Armature/Skeleton3D" index="2"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.692504, 1.30946)
[node name="Base" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/ChainGun/Armature/Skeleton3D/Base" index="0"]
layers = 2
[node name="Grip" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/ChainGun/Armature/Skeleton3D" index="3"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.692504, 1.30946)
[node name="Grip" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/ChainGun/Armature/Skeleton3D/Grip" index="0"]
layers = 2
[node name="Barrels" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/ChainGun" index="2"]
transform = Transform3D(-1, -7.45058e-09, 8.98726e-08, 8.42847e-08, 0, 1, 0, 1, 0, 1.19209e-07, 4.76837e-07, -0.55315)
transform = Transform3D(-1, 0, 8.42847e-08, 8.3819e-08, 0, 1, 0, 1, -3.72529e-09, -1.19209e-07, -4.76837e-07, -0.55315)
[node name="Skeleton3D" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/GrenadeLauncher/Armature" index="0"]
bones/0/rotation = Quaternion(0, 0.707107, 0.707107, 0)
@ -421,12 +433,21 @@ bones/1/rotation = Quaternion(0, 0.707107, 0.707107, 0)
bones/2/rotation = Quaternion(0, 0.707107, 0.707107, 0)
bones/3/rotation = Quaternion(0, 0.707107, 0.707107, 0)
[node name="barrel" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/GrenadeLauncher/Armature/Skeleton3D" index="0"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.19209e-07, 0)
[node name="barrel" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/GrenadeLauncher/Armature/Skeleton3D/barrel" index="0"]
layers = 2
[node name="grip" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/GrenadeLauncher/Armature/Skeleton3D" index="1"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.082471, -0.0653242)
[node name="grip" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/GrenadeLauncher/Armature/Skeleton3D/grip" index="0"]
layers = 2
[node name="main" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/GrenadeLauncher/Armature/Skeleton3D" index="2"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.19209e-07, 0)
[node name="main" parent="ThirdPerson/Mesh/Node/Skeleton3D/HandAttachment/GrenadeLauncher/Armature/Skeleton3D/main" index="0"]
layers = 2