-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zipline.gd
147 lines (120 loc) · 3.53 KB
/
Zipline.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#@Authors - David
#@Description - fancy catenary math for simulating rope physics
@tool
class_name Zipline
extends Node2D
const SEGMENT_SIZE = 6
@export var endpoint = Vector2(2, 0):
set(new_endpoint):
endpoint = new_endpoint
rebuild()
@export var template: PackedScene:
set(new_template):
template = new_template
rebuild()
@export var drop = 0.1:
set(new_drop):
drop = new_drop
rebuild()
@export var start_head: PackedScene:
set(new_start_head):
start_head = new_start_head
rebuild()
@export var end_head: PackedScene:
set(new_end_head):
end_head = new_end_head
rebuild()
@export var level_camera: Camera2D
var components = []
var a = 0.0
var p = 0.0
var q = 0.0
#var is_ready = false
func _ready():
#is_ready = true
rebuild()
func attach_body(body):
#print("Body attached!")
body.attach_to_zip(self)
if level_camera:
level_camera.player_object = self
level_camera.camera_offset = Vector2(0, 0)
level_camera.end_level()
#get_tree().call_deferred("reload_current_scene")
func rebuild():
if template == null:
return
#print(">>>>>>>>>>", endpoint, template, drop)
#if not is_ready:
# return
for component in components:
component.queue_free()
components = []
_bake_catenary()
var ratio = endpoint.x / (floor(endpoint.x / SEGMENT_SIZE) * SEGMENT_SIZE)
while len(components) < round(endpoint.x) / SEGMENT_SIZE:
var component = template.instantiate()
component.name = "bridge_component_" + str(len(components))
var distance = ratio * len(components) * SEGMENT_SIZE
var height = -1 * calculate_catenary(distance)
var delta = -1 * calculate_catenary(distance + ratio) - height
#print(distance, ", ", height, ", ", ratio)
component.position = Vector2(
distance,
height)
var shift = Vector2(ratio, delta)
component.scale = Vector2(shift.length(), 1.0)
component.rotation = shift.angle()
component.skew = -shift.angle()
components.append(component)
add_child(component)
component.body_entered.connect(attach_body)
if start_head != null:
var start = start_head.instantiate()
start.name = "bridge_start"
start.position = Vector2(-1, 0)
components.append(start)
add_child(start)
if end_head != null:
var end = end_head.instantiate()
end.name = "bridge_end"
end.position = endpoint + Vector3(1, 0, 0)
end.scale = Vector3(-1, 1, 1)
components.append(end)
add_child(end)
func calculate_catenary(distance):
#print(a)
return a * cosh((distance-p)/a) + q
func get_snap_height(x):
return position.y - calculate_catenary(x - position.x)
const DERIVATIVE_STEP = 0.001
func get_snap_slope(x):
var center = x - position.x
var rise = calculate_catenary(center + DERIVATIVE_STEP) - calculate_catenary(center - DERIVATIVE_STEP)
var run = DERIVATIVE_STEP + DERIVATIVE_STEP
return rise / run
func get_snap_slope_vector(x):
return Vector2(1.0, get_snap_slope(x)).normalized()
func _bake_catenary():
var h = endpoint.x
var v = endpoint.y
var l = Vector2(endpoint.x, endpoint.y).length() + drop
a = (l * l * l) # calculate a really big upper boud to bisect with.
var Precision = 0.00001;
#var IntervalStep = 0.01
var a_prev = 0.0#a - IntervalStep
var a_next = a
var loop_count = 0
#print("baking... ", loop_count, ", ", a)
while loop_count <= 0 or a_next - a_prev > Precision:
loop_count += 1
a = (a_prev + a_next) / 2.0;
if (sqrt(pow(l, 2) - pow(v, 2)) < 2 * a * sinh(h/(2*a))):
a_prev = a
else:
a_next = a
#print("baking... ", loop_count, ", ", a)
if loop_count > 1000:
return
p = (h - a * log((l+v)/(l-v))) / 2
q = (v - l / tanh(h/(2*a))) / 2