Fixing rope again

Hopefully final time

Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
This commit is contained in:
Ethan Wellenreiter 2025-01-04 23:04:19 -05:00
parent ec30b8e972
commit 0132088cae

View File

@ -52,8 +52,7 @@ func shift_pop_rope_seg() -> void:
rope_segments[-2].update_sprite_heading(player.global_position)
rope_segments[-2].update_ray_heading(player.global_position)
pop_rope_seg()
#print("shift_pop_exited")
func clear_rope() -> void:
@ -63,21 +62,36 @@ func clear_rope() -> void:
func angle_valid_release_rope(a1:float, a2:float, cw:bool) -> bool:
var result : bool
#a1 = fmod(a1+PI, 2*PI)
#a2 = fmod(a2+PI, 2*PI)
a1 = fmod(a1+PI, 2*PI) # 0 to 2PI
a2 = fmod(a2+PI, 2*PI)
#print(abs(a1-a2))
var diff : float = PI - abs(abs(a1 - a2) - PI)
#print("angle diff: ", PI - abs(abs(a1 - a2) - PI))
var angle_margin : float = 0.2
if cw:
result = fmod(a2-angle_margin, 2*PI) < a1 and diff < angle_margin
#print()
else:
result = fmod(a2+angle_margin, 2*PI) > a1 and diff < angle_margin
#var angle_margin : float = 0.09
var within_angle : bool = diff < 0.2
# https://math.stackexchange.com/questions/110080/shortest-way-to-achieve-target-angle#:~:text=If%20a%3Eb%2C%20and%20a,b%3E180%2C%20turn%20counterclockwise.
var temp = [a2 - a1, a2 - a1 + 2*PI, a2 - a1 - 2*PI]
#var alpha = a2 - a1 # T - C
#var beta = a2 - a1 + 2*PI
#var gamma = a2 - a1 - 2*PI
var smallest = temp[0]
for i in temp:
if abs(i) < abs(smallest):
smallest = i
result = smallest > 0 # if it's positive
if not cw:
result = not result
result = result and within_angle
#print(a1, " ", a2, " ", cw, " ", result)
return result
func check_new_coll():
#print("enter")
var cur_rope = rope_segments[-1]
@ -87,14 +101,13 @@ func check_new_coll():
#print(player.rope_length, " ", dist)
if player.rope_length - dist > DIST_MARGIN:
#if (rope_segments.size() <= 1):
#print("adding rope_seg ", player.rope_length, " " ,dist, " ", player.rope_length - dist)
shift_add_rope_seg(col_point)
rope_segments[-2].connection_angle = rope_segments[-2].conn_point.angle_to_point(player.global_position)
var tangent : Vector2 = col_point - player.global_position
tangent = Vector2(-tangent.y, tangent.x)
rope_segments[-2].clockwise = player.velocity.dot(tangent) < 0
tangent = Vector2(tangent.y, -tangent.x)
rope_segments[-2].clockwise = player.velocity.dot(tangent) > 0
#print(tangent, " ", player.velocity, " ", rope_segments[-2].clockwise)
#cur_rope.update_sprite_heading(col_point)
player.rope_length = dist
player.connection_point = col_point
@ -105,14 +118,8 @@ func check_new_coll():
while counter >= 0:
#print("looping")
if rope_segments[counter].rope_ray.is_colliding(): # safety check
#var temp_col_point : Vector2 = rope_segments[counter].rope_ray.get_collision_point()
#var temp_dist : float = player.global_position.distance_to(temp_col_point)
#print(rope_segments[counter].conn_point.angle_to_point(player.global_position), " ", rope_segments[counter].connection_angle, " ", angle_valid_release_rope(rope_segments[counter].conn_point.angle_to_point(player.global_position), rope_segments[counter].connection_angle, rope_segments[counter].clockwise))
if rope_segments[counter].conn_point.distance_to(rope_segments[counter].rope_ray.get_collision_point()) < DIST_MARGIN and \
angle_valid_release_rope(rope_segments[counter].conn_point.angle_to_point(player.global_position), rope_segments[counter].connection_angle, rope_segments[counter].clockwise): # if the collision of the other ray is longer (so it can see the earlier connection point)
#print("removing ",counter, " ", rope_segments[counter].conn_point.distance_to(rope_segments[counter].rope_ray.get_collision_point()))
player.connection_point = rope_segments[counter].conn_point
player.rope_length = player.global_position.distance_to(player.connection_point)
shift_pop_rope_seg()