Trying to slope rotate (futile style)

This commit is contained in:
Jakob Feldmann 2022-11-01 15:25:26 +01:00
parent 0c5c2112b4
commit 7c423e7fc9
3 changed files with 88 additions and 43 deletions

View File

@ -354,36 +354,85 @@ func execute_airstrafe(
air_strafe_charges -= 1 air_strafe_charges -= 1
return linear_velocity return linear_velocity
var snapped = false
func execute_movement() -> void: func execute_movement() -> void:
if(GlobalState.is_dead): if(GlobalState.is_dead):
return return
var colliding_ray_number = 0 var snap = Vector2.DOWN * 128
var target_angle = 0 var center_floor_rot = 0
var rotation_point: Vector2 var floor_rot = 0
if(is_on_floor()): # get rotation of floor, compare collided floor with floor under center
for raycast in $SlopeRaycasts.get_children(): if is_on_floor():
if(raycast.is_colliding()): # TODO: Problem when correctly rotating?
colliding_ray_number += 1 center_floor_rot = $SlopeRaycast.get_collision_normal().angle() + PI/2
var collision_normal = raycast.get_collision_normal() floor_rot = get_floor_normal().angle() + PI/2
target_angle += Vector2.UP.angle_to(collision_normal) if(abs(center_floor_rot) > PI/4+0.1):
target_angle = rotation + target_angle/max(colliding_ray_number,1) center_floor_rot = floor_rot
rotation_point = sign(target_angle) * Vector2($BlobbyBody.shape.extents.x + $BlobbyBody.to_local(position).x, 0) # snap when on slopes
print(rotation_point) if(abs(floor_rot) > 0.1 || abs(center_floor_rot) > 0.1):
if(target_angle == 0 || colliding_ray_number < 2): velocity = move_and_slide_with_snap(velocity.rotated(floor_rot),
rotation = 0 snap, FLOOR_NORMAL, true)
velocity = move_and_slide(velocity, FLOOR_NORMAL,true, 4, 0.785398,false) snapped = true
# normal slide on flat floor
else: else:
velocity = move_and_slide(velocity, FLOOR_NORMAL,true, 4, 0.785398,false) if snapped == true:
var translation = rotation_point.rotated(target_angle) - rotation_point print("unsnapped")
# if(target_angle == 0): velocity = move_and_slide(velocity.rotated(floor_rot),FLOOR_NORMAL)
# position.x += translation.xddddad snapped = false
# position.y += translation.y # rotate related to floor slope
rotation = target_angle var rot = calculate_slope_rotation(center_floor_rot)
# Convert velocity back to local space.
velocity = velocity.rotated(-floor_rot)
func calculate_slope_rotation(center_floor_rot: float) -> float:
var angle = 0
# var distance_to_slope_left = abs($SlopeRaycastLeft.global_position.distance_to($SlopeRaycastLeft.get_collision_point()))
# var distance_to_slope_right = abs($SlopeRaycastRight.global_position.distance_to($SlopeRaycastRight.get_collision_point()))
# var slope_angle_left = rad2deg($SlopeRaycastLeft.get_collision_normal().rotated(PI/2).angle())
# var slope_angle_right = rad2deg($SlopeRaycastRight.get_collision_normal().rotated(PI/2).angle())
# if(abs(slope_angle_left - slope_angle_right) < 5):
# print("on slope")
# elif(abs(slope_angle_left) > 0 && distance_to_slope_left < distance_to_slope_right ||
# abs(slope_angle_right) > 0 && distance_to_slope_right < distance_to_slope_left):
# print("upturn")
# var length_vector: Vector2 = $SlopeRaycastRight.get_collision_point() - $SlopeRaycastLeft.get_collision_point()
# angle = length_vector.angle()
# print(rad2deg(angle))
# else:
# print("downturn")
# var length_vector: Vector2 = $SlopeRaycastRight.get_collision_point() - $SlopeRaycastLeft.get_collision_point()
# angle = length_vector.angle()
# # angle = center_floor_rot * min(1, 1.3*overhang(distance_to_slope_left, distance_to_slope_right)/$BlobbyBody.shape.extents.x)
if(player_state_machine.facing == 1):
var length_vector: Vector2 = $SlopeRaycastRight.get_collision_point() - $SlopeRaycastLeft.get_collision_point()
angle = length_vector.angle()
print(rad2deg(angle))
return angle
# When Blobby is going over an edge,
# This function returns how many pixels he has gone over that edge, measured from his center
func overhang(distance_to_slope_left, distance_to_slope_right) -> float:
var distance_to_slope = abs($SlopeRaycast.global_position.distance_to($SlopeRaycast.get_collision_point()))
if(!(abs(distance_to_slope_left) > 1 || abs(distance_to_slope_right) > 1)):
distance_to_slope = max(1,distance_to_slope)
var hitbox_to_slope_vector = (global_position - $SlopeRaycast.get_collision_point()).normalized()
var angle = min(PI/2,abs(hitbox_to_slope_vector.angle_to($SlopeRaycast.get_collision_normal().rotated(PI/2))))
var overhang = distance_to_slope * tan(angle)
overhang = $BlobbyBody.shape.extents.x if abs(overhang) > $BlobbyBody.shape.extents.x else overhang
# if(overhang < 0.6):
# print("dist to slope:")
# print(distance_to_slope)
# print("hitbox to slope vec:")
# print(hitbox_to_slope_vector)
# print("angle:")
# print(rad2deg(angle))
# print("tan(angle):")
# print(tan(angle))
# print("overhang:")
# print(overhang)
return abs(overhang)
#TODO Limit movement
func die() -> void: func die() -> void:
z_index = 1 z_index = 1
GlobalState.is_dead = true GlobalState.is_dead = true

View File

@ -1778,7 +1778,7 @@ tracks/4/keys = {
"times": PoolRealArray( 0 ), "times": PoolRealArray( 0 ),
"transitions": PoolRealArray( 1 ), "transitions": PoolRealArray( 1 ),
"update": 0, "update": 0,
"values": [ Vector2( -0.0287741, -8.97473 ) ] "values": [ Vector2( -1, -9 ) ]
} }
tracks/5/type = "value" tracks/5/type = "value"
tracks/5/path = NodePath("../WallRaycasts/RightWallRaycast/Right_Wallcast1:position") tracks/5/path = NodePath("../WallRaycasts/RightWallRaycast/Right_Wallcast1:position")
@ -4474,7 +4474,7 @@ root_motion_track = NodePath(".")
parameters/playback = SubResource( 48 ) parameters/playback = SubResource( 48 )
parameters/ducking/blend_position = 1.0 parameters/ducking/blend_position = 1.0
parameters/falling/blend_position = 1.0 parameters/falling/blend_position = 1.0
parameters/idling/blend_position = 1.0 parameters/idling/blend_position = 1.35307
parameters/jumpToFall/blend_position = 1.0 parameters/jumpToFall/blend_position = 1.0
parameters/jumping/blend_position = 1.0 parameters/jumping/blend_position = 1.0
parameters/runToJump/blend_position = 1.0 parameters/runToJump/blend_position = 1.0
@ -4598,19 +4598,21 @@ exclude_parent = false
cast_to = Vector2( 1.5, 0 ) cast_to = Vector2( 1.5, 0 )
collision_mask = 56 collision_mask = 56
[node name="SlopeRaycasts" type="Node2D" parent="."] [node name="SlopeRaycastLeft" type="RayCast2D" parent="."]
position = Vector2( 0, -1 ) position = Vector2( -9, 0 )
[node name="SlopeRaycast" type="RayCast2D" parent="SlopeRaycasts"]
position = Vector2( 14, 0 )
enabled = true enabled = true
cast_to = Vector2( 0, 19 ) cast_to = Vector2( 0, 32 )
collision_mask = 56 collision_mask = 56
[node name="SlopeRaycast2" type="RayCast2D" parent="SlopeRaycasts"] [node name="SlopeRaycast" type="RayCast2D" parent="."]
position = Vector2( -12, 0 )
enabled = true enabled = true
cast_to = Vector2( 0, 19 ) cast_to = Vector2( 0, 16 )
collision_mask = 56
[node name="SlopeRaycastRight" type="RayCast2D" parent="."]
position = Vector2( 10, 0 )
enabled = true
cast_to = Vector2( 0, 32 )
collision_mask = 56 collision_mask = 56
[connection signal="area_entered" from="BlobbySkin" to="." method="_on_BlobbySkin_area_entered"] [connection signal="area_entered" from="BlobbySkin" to="." method="_on_BlobbySkin_area_entered"]

File diff suppressed because one or more lines are too long