-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrack.py
More file actions
169 lines (155 loc) · 5.39 KB
/
track.py
File metadata and controls
169 lines (155 loc) · 5.39 KB
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
import math
from util import Vector2
class Track:
def __init__(self, trackPoints=None):
self.points = trackPoints
self.pointsChunks = {}
self.chunkSize = 20
self.trackWidth = 8
self.track_loop = None
if self.points is None:
# self.points = self.generateSquareTrack()
self.points = self.generateSTrack()
# self.points = self.generateOTrack()
# self.points = self.generateHalfOTrack()
self.generateChunks()
def generateChunks(self):
self.pointsChunks = {}
for p in self.points:
cx, cy = self.get_chunk_for(p[0])
if (cx, cy) not in self.pointsChunks:
self.pointsChunks[(cx, cy)] = []
self.pointsChunks[(cx, cy)].append(p)
def get_chunk_for(self, v: Vector2):
return v.x // self.chunkSize, v.y // self.chunkSize
def get_next_point_after(self, pointIndex, indexDelta):
pointIndex += indexDelta
if pointIndex >= len(self.points):
if self.track_loop:
pointIndex -= len(self.points)
else:
pointIndex = len(self.points) - 1
return self.points[pointIndex]
def get_nearest(self, position: Vector2):
cx, cy = self.get_chunk_for(position)
dsqr = float('inf')
nearest = self.points[0]
for x in range(-1, 2):
for y in range(-1, 2):
if (cx + x, cy + y) in self.pointsChunks:
for p in self.pointsChunks[(cx + x, cy + y)]:
_t = (position - p[0]).dist_sqr()
if _t < dsqr:
dsqr = _t
nearest = p
return nearest
def generateSquareTrack(self):
self.track_loop = True
n = 100
width = 200
height = 100
points = []
for i in range(n):
t = i / n
x = -1
y = -1
if t < 1/6:
x = 0
y = (t / (1/6)) * height
elif t < 3/6:
x = ((t - (1/6)) / (2/6)) * width
y = height
elif t < 4/6:
x = width
y = (1 - (t - (3/6)) / (1/6)) * height
elif t < 1:
x = (1 - (t - (4/6)) / (2/6)) * width
y = 0
points.append([Vector2(x, y), i])
return self.smoothTrack(points, 60)
def generateSTrack(self):
self.track_loop = False
n = 100
width = 200
height = 100
points = []
for i in range(n):
t = i / n
x = -1
y = -1
if t < 0.25:
x = 0
y = (t / 0.25) * height
elif t < 0.75:
x = ((t - 0.25) / 0.5) * width
y = height
elif t < 1:
x = width
y = (1 - (t - 0.75) / 0.25) * height
points.append([Vector2(x, y), i])
return self.smoothTrack(points, 60)
def generateOTrack(self):
self.track_loop = True
n = 100
width = 100
height = 100
points = []
for i in range(n):
t = i / n
x = width + self.trackWidth / 2
y = 0
x -= math.cos(t * 2 * math.pi) * width
y -= math.sin(t * 2 * math.pi) * height
points.append([Vector2(x, y), i])
return self.smoothTrack(points, 1)
def generateHalfOTrack(self):
self.track_loop = True
n = 100
width = 100
height = 100
points = []
for i in range(n):
t = i / n
x = width - self.trackWidth
y = 0
if t < 0.5:
x -= math.cos(t * 2 * math.pi) * width
y -= math.sin(t * 2 * math.pi) * height
elif t < 1:
x = (1 - (t - 0.5) / 0.5) * 2 * width - self.trackWidth
y = 0
points.append([Vector2(x, y), i])
return self.smoothTrack(points, 60)
def smoothTrack(self, points, toleranceDiv: int = 40):
_maxdelta = 0
for i in range(len(points)):
if i == 0 or i == len(points) - 1:
continue
delta = (points[i][0] - points[i - 1][0]) - (points[i + 1][0] - points[i][0])
_maxdelta = max(delta.dist_sqr(), _maxdelta)
tolerance = _maxdelta / toleranceDiv
smoothed = False
skipAhead = False
while not smoothed:
smoothed = True
for i in range(len(points)):
if skipAhead:
skipAhead = False
continue
prevI = i - 1
nextI = i + 1
if prevI < 0:
if not self.track_loop:
continue
prevI = -1
if nextI >= len(points):
if not self.track_loop:
continue
nextI = 0
delta = (points[i][0] - points[prevI][0]) - (points[nextI][0] - points[i][0])
if delta.dist_sqr() > tolerance * tolerance:
points[i][0].x = (points[prevI][0].x + points[nextI][0].x) / 2
points[i][0].y = (points[prevI][0].y + points[nextI][0].y) / 2
skipAhead = True
smoothed = False
return points