Usable Spring Platform Implementation

The spring is a Node2D with it's self contained physics.
It manipulates the vertical velocities of parents of
areas that come in contact with it, based on their
current velocities and masses.
This commit is contained in:
Jakob Feldmann 2022-05-30 17:28:08 +02:00
parent 8d92d285d9
commit b75d57499a
5 changed files with 61 additions and 18 deletions

View File

@ -10,7 +10,6 @@ onready var init_boost_type = player_state_machine.init_boost_type
onready var camera_tween = $Camera2D/ShiftTween
onready var camera = $Camera2D
# TODO Midair Boost sometimes to far
# TODO Stuck to Wallslide on input
# TODO Too much speed reduction on landings
# TODO Too much speed through midair boosting
@ -28,7 +27,8 @@ 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()
#die()
return
func _on_JumpBufferTimer_timeout() -> void:
@ -178,7 +178,7 @@ func calculate_jump_velocity(
* mass
)
linear_velocity.y = PhysicsFunc.two_step_euler(
0,
linear_velocity.y,
(acceleration_force[state].y + additive_jump_force) * -1,
mass,
delta

View File

@ -3,7 +3,8 @@ const PhysicsFunc = preload("res://src/Utilities/Physic/PhysicsFunc.gd")
const PhysicsConst = preload("res://src/Utilities/Physic/PhysicsConst.gd")
var mass = 1
var spring_k = -1000
var coupled_mass = mass
var spring_k = -660
var start_y = 0
var y_velocity = 0
var friction = 0.91
@ -26,14 +27,15 @@ func _physics_process(delta: float) -> void:
shock_ready = false
var spring_force = spring_k * (self.position.y - self.start_y)
var total_mass = mass
if coupled_body != null:
total_mass = mass + coupled_body.mass
coupled_mass = mass + coupled_body.mass
else:
coupled_mass = mass
var weight_force = total_mass * PhysicsConst.gravity
var weight_force = coupled_mass * PhysicsConst.gravity
var result_force = weight_force + spring_force
y_velocity += PhysicsFunc.convert_force_to_velocity(
result_force, total_mass, delta
y_velocity = PhysicsFunc.two_step_euler(
y_velocity, result_force, coupled_mass, delta
)
y_velocity *= friction
@ -41,12 +43,11 @@ func _physics_process(delta: float) -> void:
self.position.y += y_velocity * delta
# TODO this works against the physics process
func _store_incoming_velocity():
var areas: Array = $EnteringVelocityDetector.get_overlapping_areas()
for i in range(0, areas.size()):
if areas[i].name == "BlobbySkin":
if areas[i].get_parent().velocity.y != 0:
if areas[i].get_parent().velocity.y > 0:
stored_incoming_velocity = areas[i].get_parent().velocity.y
@ -68,5 +69,23 @@ func _Kinematic_Body_on_Spring() -> void:
y_velocity += PhysicsFunc.complete_unelastic_shock(
a_velocity, b_velocity, a_mass, b_mass
)
coupled_body.velocity.y = y_velocity
if sign(coupled_body.velocity.y) > 0:
coupled_body.velocity.y = y_velocity
stored_incoming_velocity = 0
func _on_SpringSkin_area_exited(_area: Area2D) -> void:
var displacement = self.position.y - self.start_y
var potential_spring_energy = spring_k * 0.5 * pow(displacement, 2)
var mass_ratio = 1 - mass / coupled_mass
var transferred_kinetic_energy = potential_spring_energy * mass_ratio
var kinetic_energy_in_velocity = (
-sign(displacement)
* sqrt(
abs(
2 * transferred_kinetic_energy / max(coupled_mass - mass, 0.001)
)
)
)
if coupled_body != null:
coupled_body.velocity.y += kinetic_energy_in_velocity

View File

@ -4,13 +4,13 @@
[ext_resource path="res://assets/environment/blocks/Basic stone block.png" type="Texture" id=2]
[sub_resource type="RectangleShape2D" id=2]
extents = Vector2( 11.4526, 1.75208 )
extents = Vector2( 11.6455, 1.51714 )
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 11.9386, 1.57982 )
extents = Vector2( 11.918, 1.57982 )
[sub_resource type="RectangleShape2D" id=3]
extents = Vector2( 11.4, 0.878017 )
extents = Vector2( 11.7595, 0.501062 )
[node name="Spring" type="Node2D"]
script = ExtResource( 1 )
@ -24,7 +24,7 @@ collision_layer = 32
collision_mask = 3
[node name="CollisionShape2D" type="CollisionShape2D" parent="SpringSkin"]
position = Vector2( 0, -0.25779 )
position = Vector2( 0.0369902, -1.50376 )
shape = SubResource( 2 )
[node name="SpringBody" type="KinematicBody2D" parent="."]
@ -32,6 +32,7 @@ collision_layer = 32
collision_mask = 41
[node name="CollisionShape2D" type="CollisionShape2D" parent="SpringBody"]
position = Vector2( 0.00390673, 0 )
shape = SubResource( 1 )
[node name="EnteringVelocityDetector" type="Area2D" parent="."]
@ -40,5 +41,7 @@ collision_layer = 32
collision_mask = 41
[node name="CollisionShape2D" type="CollisionShape2D" parent="EnteringVelocityDetector"]
position = Vector2( 0, 0.629961 )
position = Vector2( 0.0106821, 1.00246 )
shape = SubResource( 3 )
[connection signal="area_exited" from="SpringSkin" to="." method="_on_SpringSkin_area_exited"]

View File

@ -65,4 +65,25 @@ 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 )
[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"]
[editable path="Spring4"]
[editable path="Spring4/Spring"]

View File

@ -1 +1 @@
const gravity: float = 700.0
const gravity: float = 660.0