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
This commit is contained in:
Jakob Feldmann 2022-05-31 23:09:43 +02:00
parent b75d57499a
commit 6605221398
7 changed files with 56 additions and 53 deletions

View File

@ -27,8 +27,7 @@ func _on_BlobbySkin_area_entered(area: Area2D) -> void:
# When the Enemy collision BODY enters the enemy collision area -> die # When the Enemy collision BODY enters the enemy collision area -> die
func _on_BlobbySkin_body_entered(body: Node) -> void: func _on_BlobbySkin_body_entered(body: Node) -> void:
if body.name == "EnemyBody": if body.name == "EnemyBody":
#die() die()
return
func _on_JumpBufferTimer_timeout() -> void: func _on_JumpBufferTimer_timeout() -> void:
@ -100,7 +99,10 @@ func calculate_grounded_velocity(
elif !reverse_move: elif !reverse_move:
out_vel.x = max_velocity[state] * direction.x out_vel.x = max_velocity[state] * direction.x
# Jumping when grounded or jump is buffered # 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) return calculate_jump_velocity(velocity, delta, direction)
elif player_state_machine.coyote_hanging: 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 # 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" # Being able to touch a vertical surface over this length also makes it a qualified "wall"
# Also sets wall_touch_direction # 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: func is_touching_wall_completely() -> bool:
for left_raycast in left_wall_raycasts.get_children(): for left_raycast in left_wall_raycasts.get_children():
wall_touch_direction = -1 wall_touch_direction = -1
@ -138,6 +140,7 @@ func is_touching_wall_completely() -> bool:
# TODO Player gets stuck to a wall easily # 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 # Attached to wall state is in the PlayerStateMachine
func is_correct_walljump_input(direction: Vector2) -> bool: func is_correct_walljump_input(direction: Vector2) -> bool:
return ( return (
@ -167,16 +170,12 @@ func calculate_jump_velocity(
) -> Vector2: ) -> Vector2:
var state = self.get_node("PlayerStateMachine").state var state = self.get_node("PlayerStateMachine").state
var walljumping = is_correct_walljump_input(direction) 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 = PhysicsFunc.two_step_euler(
linear_velocity.y, linear_velocity.y,
(acceleration_force[state].y + additive_jump_force) * -1, (acceleration_force[state].y + additive_jump_force) * -1,
@ -199,6 +198,7 @@ func calculate_jump_velocity(
linear_velocity.y += _gravity * delta linear_velocity.y += _gravity * delta
# TODO Dis shizzle buggy # TODO Dis shizzle buggy
# TODO is boosting part of jump?
if is_equal_approx(velocity.x, 0): if is_equal_approx(velocity.x, 0):
linear_velocity.x += inair_velocity * direction.x linear_velocity.x += inair_velocity * direction.x
@ -247,15 +247,22 @@ func calculate_wallslide_velocity(
) -> Vector2: ) -> Vector2:
# Walljump mechanics # Walljump mechanics
if is_correct_walljump_input(direction): 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, linear_velocity.y,
acceleration_force["walljump"].y * -1, acceleration_force["walljump"].y * -1,
mass, mass,
delta delta
) )
linear_velocity.x += PhysicsFunc.two_step_euler( elif is_correct_airstrafe_input():
linear_velocity.x = PhysicsFunc.two_step_euler(
linear_velocity.x, linear_velocity.x,
acceleration_force["walljump"].x * direction.x, acceleration_force["air_strafe"].x * direction.x,
mass, mass,
delta delta
) )
@ -281,3 +288,11 @@ func execute_movement() -> void:
func die() -> void: func die() -> void:
queue_free() queue_free()
PlayerData.deaths += 1 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

View File

@ -50,11 +50,11 @@ polygon = PoolVector2Array( -6.7, -3.311, -2.676, -7.3, 3.939, -7.3, 8, -1.863,
script = ExtResource( 3 ) script = ExtResource( 3 )
[node name="JumpBufferTimer" type="Timer" parent="PlayerStateMachine"] [node name="JumpBufferTimer" type="Timer" parent="PlayerStateMachine"]
wait_time = 0.034 wait_time = 0.067
one_shot = true one_shot = true
[node name="CoyoteTimer" type="Timer" parent="PlayerStateMachine"] [node name="CoyoteTimer" type="Timer" parent="PlayerStateMachine"]
wait_time = 0.034 wait_time = 0.067
one_shot = true one_shot = true
[node name="StateLabel" type="Label" parent="."] [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="area_entered" from="BlobbySkin" to="." method="_on_BlobbySkin_area_entered"]
[connection signal="body_entered" from="BlobbySkin" to="." method="_on_BlobbySkin_body_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"] [connection signal="timeout" from="PlayerStateMachine/JumpBufferTimer" to="." method="_on_JumpBufferTimer_timeout"]

View File

@ -12,7 +12,7 @@ var normal_floor_friction := 0.5
var max_velocity := { var max_velocity := {
"walk": 120, "run": 160, "fall": 400, "walljump": 150, "idle": 120 "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 # This is added to the acceleration force initially
var init_acceleration_force := { var init_acceleration_force := {
"idle_walk": 4181, "idle_run": 5765, "walk_run": 1000 "idle_walk": 4181, "idle_run": 5765, "walk_run": 1000

View File

@ -1,5 +1,7 @@
extends StateMachine extends StateMachine
signal got_grounded
onready var coyoteTimer = $CoyoteTimer onready var coyoteTimer = $CoyoteTimer
export var coyote_hanging = false export var coyote_hanging = false
export var init_boost = false export var init_boost = false
@ -143,8 +145,7 @@ func _get_transition(_delta):
else: else:
# TODO How does this apply to enviornment induced movement? # TODO How does this apply to enviornment induced movement?
# TODO Velocity on moving platforms goes to 0 and to platform speed when jumping # TODO Can get from platform by jumping often while platform has same direction
# Can get from platform by jumping often while platform has same direction
new_state = states.idle new_state = states.idle
coyote_hanging = false coyote_hanging = false
if new_state != self.state: if new_state != self.state:
@ -154,23 +155,13 @@ func _get_transition(_delta):
func _enter_state(new_state, old_state): 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 = true
init_boost_type = old_state + "_" + new_state 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 # TODO This may be hard to keep track of if many states are added
# if (old_state != "run" || "walk" || "idle") && parent.is_on_floor(): if !["run", "walk", "idle"].has(old_state) && parent.is_on_floor():
# emit_signal("got_grounded") emit_signal("got_grounded")
# if ( # if (
# (old_state == "run" || "walk" || "idle" || "wallslide") # (old_state == "run" || "walk" || "idle" || "wallslide")
# && ! parent.is_on_floor() # && ! parent.is_on_floor()

View File

@ -17,6 +17,9 @@ func _ready() -> void:
start_y = self.position.y start_y = self.position.y
# TODO extensively playtest to find problems
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
if !_body_contact(): if !_body_contact():

View File

@ -10,7 +10,7 @@ extents = Vector2( 11.6455, 1.51714 )
extents = Vector2( 11.918, 1.57982 ) extents = Vector2( 11.918, 1.57982 )
[sub_resource type="RectangleShape2D" id=3] [sub_resource type="RectangleShape2D" id=3]
extents = Vector2( 11.7595, 0.501062 ) extents = Vector2( 11.7595, 0.755349 )
[node name="Spring" type="Node2D"] [node name="Spring" type="Node2D"]
script = ExtResource( 1 ) script = ExtResource( 1 )
@ -41,7 +41,7 @@ collision_layer = 32
collision_mask = 41 collision_mask = 41
[node name="CollisionShape2D" type="CollisionShape2D" parent="EnteringVelocityDetector"] [node name="CollisionShape2D" type="CollisionShape2D" parent="EnteringVelocityDetector"]
position = Vector2( 0.0106821, 1.00246 ) position = Vector2( 0.0106821, 0.748173 )
shape = SubResource( 3 ) shape = SubResource( 3 )
[connection signal="area_exited" from="SpringSkin" to="." method="_on_SpringSkin_area_exited"] [connection signal="area_exited" from="SpringSkin" to="." method="_on_SpringSkin_area_exited"]

View File

@ -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://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://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/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/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] [sub_resource type="NavigationPolygon" id=1]
vertices = PoolVector2Array( 16, 16, 0, 16, 0, 0, 16, 0 ) vertices = PoolVector2Array( 16, 16, 0, 16, 0, 0, 16, 0 )
@ -59,21 +60,9 @@ collision_mask = 2147483648
format = 1 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 ) 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 )] [node name="Blobby" parent="." instance=ExtResource( 1 )]
position = Vector2( 50.7867, 604.063 ) 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 )] [node name="Spring4" parent="." instance=ExtResource( 3 )]
position = Vector2( 331.785, 601.665 ) position = Vector2( 331.785, 601.665 )
scale = Vector2( 1.88002, 1 ) scale = Vector2( 1.88002, 1 )
@ -81,9 +70,13 @@ scale = Vector2( 1.88002, 1 )
[node name="Spring" parent="Spring4" instance=ExtResource( 3 )] [node name="Spring" parent="Spring4" instance=ExtResource( 3 )]
position = Vector2( 206.918, 601.665 ) position = Vector2( 206.918, 601.665 )
[editable path="Spring"] [node name="Track" parent="." instance=ExtResource( 5 )]
[editable path="Spring2"] position = Vector2( 422.501, 601.665 )
[editable path="Spring3"] scale = Vector2( 2.83999, 0.56 )
[editable path="Spring3/Spring"]
[node name="KinematicBody2D" parent="Track" index="0"]
position = Vector2( 8, 0 )
[editable path="Spring4"] [editable path="Spring4"]
[editable path="Spring4/Spring"] [editable path="Spring4/Spring"]
[editable path="Track"]