From a271baa8ee1b8d1b8e379bbe214ee7be80b17d92 Mon Sep 17 00:00:00 2001 From: AleaJactaEst Date: Sat, 12 Feb 2022 02:00:16 +0100 Subject: [PATCH] #3 - add small collisions handling --- player/player.gd | 48 ++++++++++++++++++++++++++++++---------------- player/player.tscn | 2 +- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/player/player.gd b/player/player.gd index 9535b21..1d156aa 100644 --- a/player/player.gd +++ b/player/player.gd @@ -10,11 +10,12 @@ const SPEED_WALK_STRAFE = 2.5 const SPEED_WALK_DOWN_STRAFE = 0.6 const SPEED_WALK_DOWN = 0.8 -const SPEED_RUN_UP = SPEED_WALK_UP * 2.0 -const SPEED_RUN_UP_STRAFE = SPEED_WALK_UP_STRAFE * 2.0 -const SPEED_RUN_STRAFE = SPEED_WALK_STRAFE * 2.0 -const SPEED_RUN_DOWN_STRAFE = SPEED_WALK_DOWN_STRAFE * 2.0 -const SPEED_RUN_DOWN = SPEED_WALK_DOWN * 2.0 +const DIFF_RUN_WALK = 2.0 +const SPEED_RUN_UP = SPEED_WALK_UP * DIFF_RUN_WALK +const SPEED_RUN_UP_STRAFE = SPEED_WALK_UP_STRAFE * DIFF_RUN_WALK +const SPEED_RUN_STRAFE = SPEED_WALK_STRAFE * DIFF_RUN_WALK +const SPEED_RUN_DOWN_STRAFE = SPEED_WALK_DOWN_STRAFE * DIFF_RUN_WALK +const SPEED_RUN_DOWN = SPEED_WALK_DOWN * DIFF_RUN_WALK const ZOOM_STEP_Y = 0.05 const ZOOM_STEP_Z = 0.1 @@ -24,12 +25,13 @@ const ZOOM_MAX_Z = 3.0 const SPEED_ROTATE = PI const PI_2 = PI / 2.0 const JUMP_FORCE = 4.5 -const STEP_FORCE = 30.0 +const STEP_FORCE = 50.0 const MUL_SPEED_FLY = 2.0 const MUL_SPEED_SWIM = 0.5 const FACTOR_WALK_WATER = 0.3 -const MUL_SPEED_STEP = 0.4 +const MUL_SPEED_STEP = 0.2 +const SPEED_COLLIDED = 0.0001 # Get the gravity from the project settings to be synced with RigidDynamicBody nodes. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") @@ -83,7 +85,9 @@ var state_player:StatePlayer = StatePlayer.WALK var level_water:float var heigh_underwater = 1.0 var stop_move:bool = false +var max_collided_speed = 0.0 @onready var camera_fps:Camera3D = $camera_root/Camera3D_FPS_WALK +@onready var height_step:Vector3 = $RayCastStep.get_position() - $RayCastGround.get_position() func switch_state(new_state): @@ -294,7 +298,6 @@ func _physics_process_walk(delta): var move_up:bool = false var move_down:bool = false var move_strafe: bool = false - #var step: bool = false var mulstep: float = 1.0 #print(get_position()) @@ -402,12 +405,6 @@ func _physics_process_walk(delta): switch_animation(anim_sitting_ground_idle) else: switch_animation(anim_idle) - if is_on_floor() and (input_x != 0.0 or input_y != 0.0) and $RayCastGround.is_colliding() and !$RayCastStep.is_colliding(): - motion_velocity.y = STEP_FORCE - move_and_slide() - motion_velocity.y = -STEP_FORCE - #step = true - mulstep = MUL_SPEED_STEP var direction = (transform.basis * Vector3(input_x, 0, input_y)).normalized() if direction: @@ -417,10 +414,27 @@ func _physics_process_walk(delta): motion_velocity.x = move_toward(motion_velocity.x, 0, speed * mulstep) motion_velocity.z = move_toward(motion_velocity.z, 0, speed * mulstep) - #print(">>> motion_velocity:", motion_velocity, " position:", get_position()) + var save_motion_velocity:Vector3 = motion_velocity + var collided = move_and_slide() - if collided: - print("+++++++++++: ", get_last_slide_collision().get_position(), " <> ", get_position()) + + if collided and is_on_floor() and (input_x != 0.0 or input_y != 0.0) and $RayCastGround.is_colliding() and !$RayCastStep.is_colliding(): + var delta_1:Vector3 = get_position_delta() + var areawalk = delta_1.x * delta_1.x + delta_1.z * delta_1.z + if (areawalk * 10.0 < max_collided_speed) or (is_run and (areawalk * 10.0 < max_collided_speed * DIFF_RUN_WALK )): + var jump_step:Vector3 = height_step + translate(jump_step) + motion_velocity = save_motion_velocity - delta_1 # (save_motion_velocity - delta_1) * MUL_SPEED_STEP + move_and_slide() + motion_velocity.x = 0.0 + motion_velocity.y = -STEP_FORCE + motion_velocity.z = 0.0 + collided = move_and_slide() + else: + var delta_1:Vector3 = get_position_delta() + var areawalk = (delta_1.x * delta_1.x + delta_1.z * delta_1.z) / speed + if !is_run and max_collided_speed < areawalk: + max_collided_speed = areawalk func _physics_process_walk_water(delta): diff --git a/player/player.tscn b/player/player.tscn index 34d09ff..e5a37ab 100644 --- a/player/player.tscn +++ b/player/player.tscn @@ -85,4 +85,4 @@ visible = false mesh = SubResource( "PlaneMesh_p36im" ) [node name="RayCastStep" type="RayCast3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 0, 1, 0, -0.3, 0, 0, 0.4, 0) +transform = Transform3D(1, 0, 0, 0, 0, 1, 0, -0.4, 0, 0, 0.4, 0)