-
Notifications
You must be signed in to change notification settings - Fork 4
/
intersections_connect2d.gd
366 lines (325 loc) · 11.9 KB
/
intersections_connect2d.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
tool
extends Node2D
# class member variables go here, for example:
# need to be class-level because draw()
var loc_src_exit = Vector2(0,0)
var loc_dest_exit = Vector2(0,0)
var loc_src_extended = Vector2(0,0)
var loc_dest_extended = Vector2(0,0)
func _ready():
pass
func connect_intersections(one, two):
print("Connecting intersections " + str(one) + " and " + str(two))
# catch indices out of range
if one > get_child_count() -1 or two > get_child_count() -1:
print("Wrong indices given")
return false
if not "point_one" in get_child(one) or not "point_one" in get_child(two):
print("Targets are not intersections?")
return false
var src_exit = get_src_exit(get_child(one), get_child(two))
if not src_exit:
return false
loc_src_exit = to_local(get_child(one).to_global(src_exit))
var dest_exit = get_dest_exit(get_child(one), get_child(two))
if not dest_exit:
return false
loc_dest_exit = to_local(get_child(two).to_global(dest_exit))
extend_lines(one,two)
func extend_lines(one,two):
#B-A: A->B
var src_line = loc_src_exit-get_child(one).get_position()
var extend = 3
# note: src_line*extend > helper line's vector must be true, otherwise the turns won't work
loc_src_extended = src_line*extend + get_child(one).get_position()
var dest_line = loc_dest_exit-get_child(two).get_position()
loc_dest_extended = dest_line*extend + get_child(two).get_position()
func _draw():
draw_line(loc_src_exit, loc_dest_exit, Color(0,1,0, 0.5))
# test
#draw_line(get_child(0).get_position(), loc_src_exit, Color(0,0,1))
draw_circle(loc_src_extended, 1.0, Color(0,0,1))
draw_circle(loc_dest_extended, 1.0, Color(0,1,0))
draw_line(loc_src_exit, loc_src_extended, Color(0,0,1))
draw_line(loc_src_extended, loc_dest_extended, Color(0,0,1))
draw_line(loc_dest_extended, loc_dest_exit, Color(0,1,1))
#var arr = [loc_src_exit, loc_src_extended, loc_dest_extended, loc_dest_exit]
#draw_polyline(arr, Color(0,0,1))
# assume standard rotation for now
func get_src_exit(src, dest):
var src_exits = src.open_exits
if src_exits.size() < 1:
print("Error, no exits left")
return
# those are global positions
# print("Positions: " + str(src.get_position()) + " " + str(dest.get_position()))
# print("Transform:" + str(src.get_transform()))
var rel_pos = src.get_transform().xform_inv(dest.get_position())
print("Src exits for relative pos: " + str(rel_pos) + " angle " + str(atan2(rel_pos.y, rel_pos.x)))
# exits NEED to be listed CW (right = 2, bottom = 1 , top = 3) if we sort by y
# listed CCW if we sort by angle?
#quadrant 1 (we exclude top exit to avoid crossing over)
if rel_pos.x > 0 and rel_pos.y > 0:
print("Quadrant 1... angle: " + str(atan2(rel_pos.y, rel_pos.x)))
if atan2(rel_pos.y, rel_pos.x) > 1:
if src_exits.has(src.point_one):
src_exits.remove(src_exits.find(src.point_one))
return src.point_one
elif src_exits.has(src.point_two):
src_exits.remove(src_exits.find(src.point_two))
return src.point_two
else:
print("No exits found!")
else:
if src_exits.has(src.point_two):
src_exits.remove(src_exits.find(src.point_two))
return src.point_two
elif src_exits.has(src.point_one):
src_exits.remove(src_exits.find(src.point_one))
return src.point_one
else:
print("No exits found")
# quadrant 2
# same
elif rel_pos.x < 0 and rel_pos.y > 0:
print("Quadrant 2")
# if angle is small, consider only bottom and right
if atan2(rel_pos.y, rel_pos.x) < 2:
if src_exits.has(src.point_one):
src_exits.remove(src_exits.find(src.point_one))
return src.point_one
elif src_exits.has(src.point_two):
src_exits.remove(src_exits.find(src.point_two))
return src.point_two
else:
print("No exits found")
else:
if src_exits.has(src.point_three):
src_exits.remove(src_exits.find(src.point_three))
return src.point_three
elif src_exits.has(src.point_one):
src_exits.remove(src_exits.find(src.point_one))
return src.point_one
else:
print("No exits found")
# quadrant 3 (exclude bottom exit)
elif rel_pos.x > 0 and rel_pos.y < 0:
print("Quadrant 3")
if src_exits.has(src.point_two):
src_exits.remove(src_exits.find(src.point_two))
return src.point_two
elif src_exits.has(src.point_three):
src_exits.remove(src_exits.find(src.point_three))
return src.point_three
else:
print("No exits found")
# quadrant 4
elif rel_pos.x < 0 and rel_pos.y < 0:
print("Quadrant 4")
if src_exits.has(src.point_three):
src_exits.remove(src_exits.find(src.point_three))
return src.point_three
elif src_exits.has(src.point_one):
src_exits.remove(src_exits.find(src.point_one))
return src.point_one
else:
print("No exit found")
# naive method, sometimes leads to crossing over the intersection itself
# var dists = []
# var closest = []
# for e in src_exits:
# var dist = e.distance_to(rel_pos)
# print("Distance: exit: " + str(e) + " " + str(rel_pos) + " dist: " + str(dist))
# closest.append([dist, e])
# dists.append(dist)
#
# dists.sort()
#
# for t in closest:
# if t[0] == dists[0]:
# print("Closest exit is: " + str(t[1]))
# src_exits.remove(src_exits.find(t[1]))
# return t[1]
# if abs(dest.get_position().x - src.get_position().x) > abs(dest.get_position().y - src.get_position().y):
# if dest.get_position().y > src.get_position().y:
# if src_exits.has(src.point_two):
# print("[src] " + src.get_name() + " " + dest.get_name() + " X rule")
# src_exits.remove(src_exits.find(src.point_two))
# return src.point_two
# else:
# if src_exits.has(src.point_three):
# print("[src] " + src.get_name() + " " + dest.get_name() + " X rule alt")
# src_exits.remove(src_exits.find(src.point_three))
# return src.point_three
# else:
# if src_exits.has(src.point_three):
# #if dest.get_position().x < src.get_position().x and src_exits.has("two"):
# print("[src] " + str(src.get_name()) + " " + str(dest.get_name()) + " X rule inv" )
#
# src_exits.remove(src_exits.find(src.point_three))
# #src_exits.remove(src_exits.find("two"))
#
# return src.point_three
#
# else:
# if src_exits.has(src.point_one):
# print("[src] " + str(src.get_name()) + " " + str(dest.get_name()) + " X rule inv alt")
# src_exits.remove(src_exits.find(src.point_one))
#
# return src.point_one
#
# elif dest.get_position().y > src.get_position().y and src_exits.has(src.point_one):
# print("[src] " + str(src.get_name()) + " " + str(dest.get_name()) + " Y rule")
#
# src_exits.remove(src_exits.find(src.point_one))
# #src_exits.remove(src_exits.find("one"))
#
# return src.point_one
#
# elif src_exits.has(src.point_three):
#src_exits.has("three"):
# else
# print("[src] " + str(src.get_name()) + " " + str(dest.get_name()) + " Y rule 2")
#
# src_exits.remove(src_exits.find(src.point_three))
# #src_exits.remove(src_exits.find("three"))
# return src.point_three
# assume standard rotation for now
func get_dest_exit(src, dest): #, dest_exits):
var dest_exits = dest.open_exits
#print("X abs: " + str(abs(dest.get_position().x - src.get_position().x)))
#print("Y abs: " + str(abs(dest.get_position().y - src.get_position().y)))
if dest_exits.size() < 1:
print("Error, no exits left")
return
# those are global positions
# print("Positions: " + str(src.get_position()) + " " + str(dest.get_position()))
# print("Transform:" + str(src.get_transform()))
var rel_pos = dest.get_transform().xform_inv(src.get_position())
print("Dest exits for relative pos: " + str(rel_pos) + " angle " + str(atan2(rel_pos.y, rel_pos.x)))
# exits NEED to be listed CW (right = 2, bottom = 1, top = 3)
# listed CCW if we sort by angle?
# quadrant 4, exclude right exit to avoid crossing over
if rel_pos.x < 0 and rel_pos.y < 0:
print("Dest quadrant 4... " + str(atan2(rel_pos.y, rel_pos.x)))
if atan2(rel_pos.y, rel_pos.x) > -2:
print("Case 1")
if dest_exits.has(dest.point_three):
dest_exits.remove(dest_exits.find(dest.point_three))
return dest.point_three
elif dest_exits.has(dest.point_two):
dest_exits.remove(dest_exits.find(dest.point_two))
return dest.point_two
else:
print("Case 2")
if dest_exits.has(dest.point_three):
dest_exits.remove(dest_exits.find(dest.point_three))
return dest.point_three
elif dest_exits.has(dest.point_one):
dest_exits.remove(dest_exits.find(dest.point_one))
return dest.point_one
# quadrant 3, same
elif rel_pos.x > 0 and rel_pos.y < 0:
print("Dest quadrant 3")
if dest_exits.has(dest.point_three):
dest_exits.remove(dest_exits.find(dest.point_three))
return dest.point_three
elif dest_exits.has(dest.point_two):
dest_exits.remove(dest_exits.find(dest.point_two))
return dest.point_two
# quadrant 2
elif rel_pos.x < 0 and rel_pos.y > 0:
print("Dest quadrant 2")
if dest_exits.has(dest.point_three):
dest_exits.remove(dest_exits.find(dest.point_three))
return dest.point_three
elif dest_exits.has(dest.point_one):
dest_exits.remove(dest_exits.find(dest.point_one))
return dest.point_one
# quadrant 1, same
elif rel_pos.x > 0 and rel_pos.y > 0:
if dest_exits.has(dest.point_two):
dest_exits.remove(dest_exits.find(dest.point_two))
return dest.point_two
elif dest_exits.has(dest.point_three):
dest_exits.remove(dest_exits.find(dest.point_three))
return dest.point_three
# naive method sometimes leads to crossing over the intersection itself
# var dists = []
# var closest = []
# for e in dest_exits:
# var dist = e.distance_to(rel_pos)
# print("Distance: exit: " + str(e) + " " + str(rel_pos) + " dist: " + str(dist))
# closest.append([dist, e])
# dists.append(dist)
#
# dists.sort()
#
# for t in closest:
# if t[0] == dists[0]:
# print("Closest exit is: " + str(t[1]))
# dest_exits.remove(dest_exits.find(t[1]))
# return t[1]
# if abs(dest.get_position().x - src.get_position().x) > abs(dest.get_position().y - src.get_position().y):
# if dest.get_position().y > src.get_position().y:
# if dest_exits.has(dest.point_three):
# print("[dest] " + src.get_name() + " " + dest.get_name() + " X rule a)")
# dest_exits.remove(dest_exits.find(dest.point_three))
#
# return dest.point_three
# else:
# if dest_exits.has(dest.point_one):
# print("[dest] " + src.get_name() + " " + dest.get_name() + " X rule b)")
# dest_exits.remove(dest_exits.find(dest.point_one))
# return dest.point_one
# else:
# print("[dest] " + src.get_name() + " " + dest.get_name() + " X rule b) alt")
# dest_exits.remove(dest_exits.find(dest.point_three))
# return dest.point_three
#
#
# if dest_exits.has(dest.point_three):
# #if dest.get_position().x < src.get_position().x and dest_exits.has("one"):
# print("[dest] " + str(src.get_name()) + " " + str(dest.get_name()) + " X rule")
#
# dest_exits.remove(dest_exits.find(dest.point_three))
# #dest_exits.remove(dest_exits.find("three"))
#
# return dest.point_three
#
# else:
# print("[dest] " + src.get_name() + " " + dest.get_name() + " replacement for X rule")
#
# dest_exits.remove(dest_exits.find(dest.point_one))
# #dest_exits.remove(dest_exits.find("one"))
#
# return dest.point_one
#dest_exits.remove(dest_exits.find("one"))
#return dest.point_one
# elif dest.get_position().y > src.get_position().y:
# if dest_exits.has(dest.point_three):
# #if dest_exits.has("three"):
# print("[dest] " + str(src.get_name()) + " " + str(dest.get_name()) + " Y rule")
#
# dest_exits.remove(dest_exits.find(dest.point_three))
# #dest_exits.remove(dest_exits.find("three"))
#
# return dest.point_three
#
# else:
# print("[dest] " + src.get_name() + " " + dest.get_name() + " replacement for Y rule")
#
# dest_exits.remove(dest_exits.find(dest.point_two))
# #dest_exits.remove(dest_exits.find("two"))
#
# return dest.point_two
#
# elif dest_exits.has(dest.point_one):
# #elif dest_exits.has("one"):
# #else:
# print("[dest] " + str(src.get_name()) + " " + str(dest.get_name()) + " Y rule 2")
#
# dest_exits.remove(dest_exits.find(dest.point_one))
# #dest_exits.remove(dest_exits.find("one"))
#
# return dest.point_one