From 6605221398b50c99db8a0dbb1f4ec9a57e4ed58b Mon Sep 17 00:00:00 2001 From: Jakob Feldmann Date: Tue, 31 May 2022 23:09:43 +0200 Subject: [PATCH] Fixed velocity problem for landing on moving floors The problem was that blobby got the velocity of the velocity_jump_boost added to him whilst jumping on a moving platform without moving --- src/Actors/Blobby/Blobby.gd | 47 ++++++++++++++++++--------- src/Actors/Blobby/Blobby.tscn | 5 +-- src/Actors/Player.gd | 2 +- src/Actors/PlayerStateMachine.gd | 21 ++++-------- src/Contraptions/Platform/Spring.gd | 3 ++ src/Contraptions/Platform/Spring.tscn | 4 +-- src/Levels/Plattforms Level.tscn | 27 ++++++--------- 7 files changed, 56 insertions(+), 53 deletions(-) diff --git a/src/Actors/Blobby/Blobby.gd b/src/Actors/Blobby/Blobby.gd index 8a17bd9..4fe6627 100644 --- a/src/Actors/Blobby/Blobby.gd +++ b/src/Actors/Blobby/Blobby.gd @@ -27,8 +27,7 @@ func _on_BlobbySkin_area_entered(area: Area2D) -> void: # When the Enemy collision BODY enters the enemy collision area -> die func _on_BlobbySkin_body_entered(body: Node) -> void: if body.name == "EnemyBody": - #die() - return + die() func _on_JumpBufferTimer_timeout() -> void: @@ -100,7 +99,10 @@ func calculate_grounded_velocity( elif !reverse_move: out_vel.x = max_velocity[state] * direction.x # Jumping when grounded or jump is buffered - if Input.is_action_just_pressed("jump") || jump_buffer_filled: + if ( + Input.is_action_just_pressed("jump") + || (jump_buffer_filled && is_on_floor()) + ): return calculate_jump_velocity(velocity, delta, direction) elif player_state_machine.coyote_hanging: @@ -124,7 +126,7 @@ func is_reversing_horizontal_movement(direction: Vector2) -> bool: # Returns if the character is touching a wall with its whole body # Being able to touch a vertical surface over this length also makes it a qualified "wall" # Also sets wall_touch_direction -# TODO Walljumping is a bit to radical behaving +# TODO Walljumping sometimes catapults the player func is_touching_wall_completely() -> bool: for left_raycast in left_wall_raycasts.get_children(): wall_touch_direction = -1 @@ -138,6 +140,7 @@ func is_touching_wall_completely() -> bool: # TODO Player gets stuck to a wall easily +# TODO Boosters should be able to fire while wallsliding # Attached to wall state is in the PlayerStateMachine func is_correct_walljump_input(direction: Vector2) -> bool: return ( @@ -167,16 +170,12 @@ func calculate_jump_velocity( ) -> Vector2: var state = self.get_node("PlayerStateMachine").state var walljumping = is_correct_walljump_input(direction) + var additive_jump_force = velocity_jump_boost_ratio * abs(velocity.x) * mass + + if state != "jump": + if state == "idle": + additive_jump_force = 0 - if ( - Input.is_action_just_pressed("jump") && state != "jump" - || jump_buffer_filled && state != "jump" - ): - var additive_jump_force = ( - velocity_jump_boost_ratio - * abs(velocity.x) - * mass - ) linear_velocity.y = PhysicsFunc.two_step_euler( linear_velocity.y, (acceleration_force[state].y + additive_jump_force) * -1, @@ -199,6 +198,7 @@ func calculate_jump_velocity( linear_velocity.y += _gravity * delta # TODO Dis shizzle buggy + # TODO is boosting part of jump? if is_equal_approx(velocity.x, 0): linear_velocity.x += inair_velocity * direction.x @@ -247,15 +247,22 @@ func calculate_wallslide_velocity( ) -> Vector2: # Walljump mechanics if is_correct_walljump_input(direction): - linear_velocity.y = PhysicsFunc.two_step_euler( + linear_velocity.x = PhysicsFunc.two_step_euler( + linear_velocity.x, + acceleration_force["walljump"].x * direction.x, + mass, + delta + ) + linear_velocity.y += PhysicsFunc.two_step_euler( linear_velocity.y, acceleration_force["walljump"].y * -1, mass, delta ) - linear_velocity.x += PhysicsFunc.two_step_euler( + elif is_correct_airstrafe_input(): + linear_velocity.x = PhysicsFunc.two_step_euler( linear_velocity.x, - acceleration_force["walljump"].x * direction.x, + acceleration_force["air_strafe"].x * direction.x, mass, delta ) @@ -281,3 +288,11 @@ func execute_movement() -> void: func die() -> void: queue_free() PlayerData.deaths += 1 + + +# This problem stems from trying to decelerate a walk +# that was caused by the moving environment and not by input +# It is particularly usefull for moving floor physics +func _on_Blobby_got_grounded() -> void: + velocity -= get_floor_velocity() + air_strafe_charges = 1 diff --git a/src/Actors/Blobby/Blobby.tscn b/src/Actors/Blobby/Blobby.tscn index 05770f9..2a2a345 100644 --- a/src/Actors/Blobby/Blobby.tscn +++ b/src/Actors/Blobby/Blobby.tscn @@ -50,11 +50,11 @@ polygon = PoolVector2Array( -6.7, -3.311, -2.676, -7.3, 3.939, -7.3, 8, -1.863, script = ExtResource( 3 ) [node name="JumpBufferTimer" type="Timer" parent="PlayerStateMachine"] -wait_time = 0.034 +wait_time = 0.067 one_shot = true [node name="CoyoteTimer" type="Timer" parent="PlayerStateMachine"] -wait_time = 0.034 +wait_time = 0.067 one_shot = true [node name="StateLabel" type="Label" parent="."] @@ -101,4 +101,5 @@ collision_mask = 9 [connection signal="area_entered" from="BlobbySkin" to="." method="_on_BlobbySkin_area_entered"] [connection signal="body_entered" from="BlobbySkin" to="." method="_on_BlobbySkin_body_entered"] +[connection signal="got_grounded" from="PlayerStateMachine" to="." method="_on_Blobby_got_grounded"] [connection signal="timeout" from="PlayerStateMachine/JumpBufferTimer" to="." method="_on_JumpBufferTimer_timeout"] diff --git a/src/Actors/Player.gd b/src/Actors/Player.gd index 3f8f9b3..a8308af 100644 --- a/src/Actors/Player.gd +++ b/src/Actors/Player.gd @@ -12,7 +12,7 @@ var normal_floor_friction := 0.5 var max_velocity := { "walk": 120, "run": 160, "fall": 400, "walljump": 150, "idle": 120 } -var velocity_jump_boost_ratio := 0.1967 +var velocity_jump_boost_ratio := 12 # This is added to the acceleration force initially var init_acceleration_force := { "idle_walk": 4181, "idle_run": 5765, "walk_run": 1000 diff --git a/src/Actors/PlayerStateMachine.gd b/src/Actors/PlayerStateMachine.gd index b596cd8..95d4756 100644 --- a/src/Actors/PlayerStateMachine.gd +++ b/src/Actors/PlayerStateMachine.gd @@ -1,5 +1,7 @@ extends StateMachine +signal got_grounded + onready var coyoteTimer = $CoyoteTimer export var coyote_hanging = false export var init_boost = false @@ -143,8 +145,7 @@ func _get_transition(_delta): else: # TODO How does this apply to enviornment induced movement? - # TODO Velocity on moving platforms goes to 0 and to platform speed when jumping - # Can get from platform by jumping often while platform has same direction + # TODO Can get from platform by jumping often while platform has same direction new_state = states.idle coyote_hanging = false if new_state != self.state: @@ -154,23 +155,13 @@ func _get_transition(_delta): func _enter_state(new_state, old_state): - if old_state == "idle" && new_state == "walk" || new_state == "run": + if old_state == "idle" && (new_state == "walk" || new_state == "run"): init_boost = true init_boost_type = old_state + "_" + new_state - if old_state == "walk" && new_state == "run": - init_boost = true - init_boost_type = old_state + "_" + new_state - if ( - ["fall", "jump", "wallslide"].has(old_state) - && ["idle", "walk", "run"].has(new_state) - ): - parent.air_strafe_charges = 1 - # if old_state == "run" && new_state == "walk": - # init_boost = false # TODO This may be hard to keep track of if many states are added - # if (old_state != "run" || "walk" || "idle") && parent.is_on_floor(): - # emit_signal("got_grounded") + if !["run", "walk", "idle"].has(old_state) && parent.is_on_floor(): + emit_signal("got_grounded") # if ( # (old_state == "run" || "walk" || "idle" || "wallslide") # && ! parent.is_on_floor() diff --git a/src/Contraptions/Platform/Spring.gd b/src/Contraptions/Platform/Spring.gd index e622adf..7c67620 100644 --- a/src/Contraptions/Platform/Spring.gd +++ b/src/Contraptions/Platform/Spring.gd @@ -17,6 +17,9 @@ func _ready() -> void: start_y = self.position.y +# TODO extensively playtest to find problems + + # Called every frame. 'delta' is the elapsed time since the previous frame. func _physics_process(delta: float) -> void: if !_body_contact(): diff --git a/src/Contraptions/Platform/Spring.tscn b/src/Contraptions/Platform/Spring.tscn index cb4ef85..1075f77 100644 --- a/src/Contraptions/Platform/Spring.tscn +++ b/src/Contraptions/Platform/Spring.tscn @@ -10,7 +10,7 @@ extents = Vector2( 11.6455, 1.51714 ) extents = Vector2( 11.918, 1.57982 ) [sub_resource type="RectangleShape2D" id=3] -extents = Vector2( 11.7595, 0.501062 ) +extents = Vector2( 11.7595, 0.755349 ) [node name="Spring" type="Node2D"] script = ExtResource( 1 ) @@ -41,7 +41,7 @@ collision_layer = 32 collision_mask = 41 [node name="CollisionShape2D" type="CollisionShape2D" parent="EnteringVelocityDetector"] -position = Vector2( 0.0106821, 1.00246 ) +position = Vector2( 0.0106821, 0.748173 ) shape = SubResource( 3 ) [connection signal="area_exited" from="SpringSkin" to="." method="_on_SpringSkin_area_exited"] diff --git a/src/Levels/Plattforms Level.tscn b/src/Levels/Plattforms Level.tscn index b418b64..8c5ae42 100644 --- a/src/Levels/Plattforms Level.tscn +++ b/src/Levels/Plattforms Level.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=9 format=2] +[gd_scene load_steps=10 format=2] [ext_resource path="res://src/Actors/Blobby/Blobby.tscn" type="PackedScene" id=1] [ext_resource path="res://assets/environment/blocks/Basic stone block.png" type="Texture" id=2] [ext_resource path="res://src/Contraptions/Platform/Spring.tscn" type="PackedScene" id=3] [ext_resource path="res://src/Environment/Background.tscn" type="PackedScene" id=4] +[ext_resource path="res://src/Contraptions/Platform/Track.tscn" type="PackedScene" id=5] [sub_resource type="NavigationPolygon" id=1] vertices = PoolVector2Array( 16, 16, 0, 16, 0, 0, 16, 0 ) @@ -59,21 +60,9 @@ collision_mask = 2147483648 format = 1 tile_data = PoolIntArray( 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0, 11, 0, 0, 12, 0, 0, 13, 0, 0, 14, 0, 0, 15, 0, 0, 16, 0, 0, 17, 0, 0, 18, 0, 0, 19, 0, 0, 20, 0, 0, 21, 0, 0, 22, 0, 0, 23, 0, 0, 24, 0, 0, 25, 0, 0, 26, 0, 0, 27, 0, 0, 28, 0, 0, 29, 0, 0, 30, 0, 0, 31, 0, 0, 32, 0, 0, 33, 0, 0, 34, 0, 0, 35, 0, 0, 36, 0, 0, 37, 0, 0, 38, 0, 0, 39, 0, 0, 40, 0, 0, 41, 0, 0, 42, 0, 0, 43, 0, 0, 44, 0, 0, 45, 0, 0, 46, 0, 0, 47, 0, 0, 48, 0, 0, 49, 0, 0, 50, 0, 0, 51, 0, 0, 52, 0, 0, 53, 0, 0, 54, 0, 0, 55, 0, 0, 56, 0, 0, 57, 0, 0, 58, 0, 0, 59, 0, 0, 60, 0, 0, 65536, 0, 0, 65596, 0, 0, 131072, 0, 0, 131132, 0, 0, 196608, 0, 0, 196668, 0, 0, 262144, 0, 0, 262204, 0, 0, 327680, 0, 0, 327740, 0, 0, 393216, 0, 0, 393276, 0, 0, 458752, 0, 0, 458812, 0, 0, 524288, 0, 0, 524348, 0, 0, 589824, 0, 0, 589884, 0, 0, 655360, 0, 0, 655420, 0, 0, 720896, 0, 0, 720956, 0, 0, 786432, 0, 0, 786492, 0, 0, 851968, 0, 0, 852028, 0, 0, 917504, 0, 0, 917564, 0, 0, 983040, 0, 0, 983100, 0, 0, 1048576, 0, 0, 1048636, 0, 0, 1114112, 0, 0, 1114172, 0, 0, 1179648, 0, 0, 1179708, 0, 0, 1245184, 0, 0, 1245244, 0, 0, 1310720, 0, 0, 1310780, 0, 0, 1376256, 0, 0, 1376316, 0, 0, 1441792, 0, 0, 1441852, 0, 0, 1507328, 0, 0, 1507388, 0, 0, 1572864, 0, 0, 1572924, 0, 0, 1638400, 0, 0, 1638460, 0, 0, 1703936, 0, 0, 1703996, 0, 0, 1769472, 0, 0, 1769532, 0, 0, 1835008, 0, 0, 1835068, 0, 0, 1900544, 0, 0, 1900604, 0, 0, 1966080, 0, 0, 1966140, 0, 0, 2031616, 0, 0, 2031676, 0, 0, 2097152, 0, 0, 2097212, 0, 0, 2162688, 0, 0, 2162748, 0, 0, 2228224, 0, 0, 2228284, 0, 0, 2293760, 0, 0, 2293820, 0, 0, 2359296, 0, 0, 2359356, 0, 0, 2424832, 0, 0, 2424892, 0, 0, 2490368, 0, 0, 2490428, 0, 0, 2555904, 0, 0, 2555905, 0, 0, 2555906, 0, 0, 2555907, 0, 0, 2555908, 0, 0, 2555909, 0, 0, 2555910, 0, 0, 2555911, 0, 0, 2555912, 0, 0, 2555913, 0, 0, 2555914, 0, 0, 2555915, 0, 0, 2555916, 0, 0, 2555917, 0, 0, 2555918, 0, 0, 2555919, 0, 0, 2555920, 0, 0, 2555921, 0, 0, 2555922, 0, 0, 2555923, 0, 0, 2555924, 0, 0, 2555925, 0, 0, 2555926, 0, 0, 2555927, 0, 0, 2555928, 0, 0, 2555929, 0, 0, 2555930, 0, 0, 2555931, 0, 0, 2555932, 0, 0, 2555933, 0, 0, 2555934, 0, 0, 2555935, 0, 0, 2555936, 0, 0, 2555937, 0, 0, 2555938, 0, 0, 2555939, 0, 0, 2555940, 0, 0, 2555941, 0, 0, 2555942, 0, 0, 2555943, 0, 0, 2555944, 0, 0, 2555945, 0, 0, 2555946, 0, 0, 2555947, 0, 0, 2555948, 0, 0, 2555949, 0, 0, 2555950, 0, 0, 2555951, 0, 0, 2555952, 0, 0, 2555953, 0, 0, 2555954, 0, 0, 2555955, 0, 0, 2555956, 0, 0, 2555957, 0, 0, 2555958, 0, 0, 2555959, 0, 0, 2555960, 0, 0, 2555961, 0, 0, 2555962, 0, 0, 2555963, 0, 0, 2555964, 0, 0 ) -[node name="Spring" parent="." instance=ExtResource( 3 )] -position = Vector2( 206.918, 601.665 ) - [node name="Blobby" parent="." instance=ExtResource( 1 )] position = Vector2( 50.7867, 604.063 ) -[node name="Spring2" parent="." instance=ExtResource( 3 )] -position = Vector2( 247.223, 601.801 ) - -[node name="Spring3" parent="." instance=ExtResource( 3 )] -position = Vector2( 288.235, 601.801 ) - -[node name="Spring" parent="Spring3" instance=ExtResource( 3 )] -position = Vector2( 206.918, 601.665 ) - [node name="Spring4" parent="." instance=ExtResource( 3 )] position = Vector2( 331.785, 601.665 ) scale = Vector2( 1.88002, 1 ) @@ -81,9 +70,13 @@ scale = Vector2( 1.88002, 1 ) [node name="Spring" parent="Spring4" instance=ExtResource( 3 )] position = Vector2( 206.918, 601.665 ) -[editable path="Spring"] -[editable path="Spring2"] -[editable path="Spring3"] -[editable path="Spring3/Spring"] +[node name="Track" parent="." instance=ExtResource( 5 )] +position = Vector2( 422.501, 601.665 ) +scale = Vector2( 2.83999, 0.56 ) + +[node name="KinematicBody2D" parent="Track" index="0"] +position = Vector2( 8, 0 ) + [editable path="Spring4"] [editable path="Spring4/Spring"] +[editable path="Track"]