[go: up one dir, main page]

Menu

[f338fb]: / scripts / vamos_track.py  Maximize  Restore  History

Download this file

391 lines (351 with data), 11.4 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
## \file Common track-generation code.
import random
import math
import sys
header = '''<?xml version="1.0"?>
<track name="Random">
<!-- Sky Box -->
<sky>
<sides>textures/sky_sides.png</sides>
<top>textures/sky_top.png</top>
<bottom>textures/sky_bottom.png</bottom>
<smooth/>
</sky>
<material name="track" type="asphalt">
<friction>1.0</friction>
<restitution>0.1</restitution>
<rolling>1.0</rolling>
<drag>0.0</drag>
<bump-amplitude>0.01</bump-amplitude>
<bump-wavelength>100.0</bump-wavelength>
<texture>
<file>textures/track2.png</file>
<length>200.0</length>
<smooth/>
<mipmap/>
</texture>
</material>
<material name="grass" type="grass">
<friction>0.7</friction>
<restitution>0.1</restitution>
<rolling>5.0</rolling>
<drag>20.0</drag>
<bump-amplitude>0.03</bump-amplitude>
<bump-wavelength>2.0</bump-wavelength>
<texture>
<file>textures/grass.png</file>
<width>10.0</width>
<length>12.0</length>
<smooth/>
<mipmap/>
</texture>
</material>
<material name="gravel" type="gravel">
<friction>0.8</friction>
<restitution>0.0</restitution>
<rolling>40.0</rolling>
<drag>200.0</drag>
<bump-amplitude>0.05</bump-amplitude>
<bump-wavelength>2.0</bump-wavelength>
<texture>
<file>textures/gravel3.png</file>
<width>10.0</width>
<length>10.0</length>
<smooth/>
<mipmap/>
</texture>
</material>
<material name="tires" type="rubber">
<friction>1.0</friction>
<restitution>0.8</restitution>
<rolling>1.0</rolling>
<drag>5.0</drag>
<bump-amplitude>0.0</bump-amplitude>
<bump-wavelength>1.0</bump-wavelength>
<texture>
<file>textures/red-tires.png</file>
<width>0.33</width>
<length>3.0</length>
<smooth/>
<mipmap/>
</texture>
</material>
<material name="rail" type="metal">
<friction>1.0</friction>
<restitution>0.1</restitution>
<rolling>1.0</rolling>
<drag>0.0</drag>
<bump-amplitude>0.0</bump-amplitude>
<bump-wavelength>1.0</bump-wavelength>
<texture>
<file>textures/rail.png</file>
<width>0.34</width>
<length>10.0</length>
<smooth/>
<mipmap/>
</texture>
</material>
<material name="wall" type="concrete">
<friction>1.0</friction>
<restitution>0.1</restitution>
<rolling>1.0</rolling>
<drag>0.0</drag>
<bump-amplitude>0.0</bump-amplitude>
<bump-wavelength>1.0</bump-wavelength>
<texture>
<file>textures/wall.png</file>
<width>1.0</width>
<length>10.0</length>
<smooth/>
<mipmap/>
</texture>
</material>
<material name="kerb" type="concrete">
<friction>1.0</friction>
<restitution>0.1</restitution>
<rolling>1.0</rolling>
<drag>0.0</drag>
<bump-amplitude>0.06</bump-amplitude>
<bump-wavelength>2.0</bump-wavelength>
<texture>
<file>textures/red-kerb.png</file>
<width>2.0</width>
<length>4.0</length>
<smooth/>
<mipmap/>
</texture>
</material>
<!-- Segment Properties -->
<segment name="straight">
[ wall grass kerb track kerb grass rail ]
</segment>
<segment name="straight pit">
[ wall track kerb track kerb grass rail ]
</segment>
<segment name="left turn">
[ wall grass kerb track kerb gravel tires ]
</segment>
<segment name="left turn pit">
[ wall track kerb track kerb gravel tires ]
</segment>
<segment name="right turn">
[ tires gravel kerb track kerb grass rail ]
</segment>
<!-- The track -->
'''
first_segment_extras = ''' <left-width>[ 0.0, 25.0 ]</left-width>
<right-width>[ 0.0, 25.0 ]</right-width>
<left-road-width>[ 0.0, 8.0 ]</left-road-width>
<right-road-width>[ 0.0, 8.0 ]</right-road-width>
<left-wall-height>1.0</left-wall-height>
<right-wall-height>1.0</right-wall-height>
<right-kerb>
<start>
<distance>0.0</distance>
<transition>
<length>4.0</length>
<width>2.1</width>
</transition>
</start>
<end>
<distance>100.0</distance>
<transition>
<length>4.0</length>
<width>2.1</width>
</transition>
</end>
<profile>[ 1.0, 0.1 ][ 2.0, 0.1 ][ 2.1, 0.0 ]</profile>
</right-kerb>
<left-kerb>
<start>
<distance>0.0</distance>
<transition>
<length>4.0</length>
<width>2.1</width>
</transition>
</start>
<end>
<distance>100.0</distance>
<transition>
<distance>10.0</distance>
<length>4.0</length>
<width>2.1</width>
</transition>
</end>
<profile>[ 1.0, 0.1 ][ 2.0, 0.1 ][ 2.1, 0.0 ]</profile>
</left-kerb>'''
def angle_to_radius (angle, length):
radius = length / (math.pi * (angle / 180.0))
return radius
if radius > 1e-6:
return max (radius, limit)
elif radius < -1e-6:
return min (radius, -limit)
return 0.0;
def random_in_range (limits):
return random.uniform (limits [0], limits [1])
class road_segment:
'''A straight or curved segment of track'''
# Turns with a radius smaller than this get braking markers, gravel runoff,
# and tire barriers.
turn_radius_threshold = 200.0
turn_angle_threshold = 1
def __init__ (self, length, radius):
'''Create a segment with the given properties'''
self.length = length
self.radius = radius
angle = 0
if radius != 0:
angle = abs (length/radius)
self.name = 'straight'
if (abs (self.radius) < self.turn_radius_threshold
and angle > self.turn_angle_threshold):
if self.radius > 0.0:
self.name = 'left turn'
elif self.radius < 0.0:
self.name = 'right turn'
self.elevation = []
self.braking_marker_side = None
self.camera_position = None
self.camera_range = None
def add_elevation (self, distance, elevation):
self.elevation.append ([distance, elevation])
def add_camera (self, position, camera_range):
self.camera_position = position
self.camera_range = camera_range
# Put the camera on the outside of the turn for greatest visibility of
# the adjoining straights.
if self.radius > 0.0:
self.camera_position[1] *= -1.0
def add_braking_markers (self, side):
self.braking_marker_side = side
def print_segment (self, first, turn = None):
'''Print the XML for this segment'''
if turn != None:
print (' <!-- Turn %d -->' % turn)
print (''' <road segment="%s">
<resolution>2</resolution>
<length>%.1f</length>
<radius>%.1f</radius>''' % (self.name, self.length, self.radius))
if first:
print (first_segment_extras)
for e in self.elevation:
print (' <elevation>[ %.1f, %.1f ]</elevation>' % (e[0], e[1]))
if self.braking_marker_side != None:
for distance in [ 50, 100, 150 ]:
if distance > self.length:
break;
print (''' <braking-marker>
<file>textures/%d.png</file>
<distance>%d.0</distance>
<size>[ 1.4, 0.7 ]</size>
<offset>[ 2.0, 0.0 ]</offset>
<side>%s</side>
</braking-marker>''' % (distance, distance, self.braking_marker_side))
if self.camera_position != None:
print (''' <camera>
<position>[ %.1f, %.1f, %.1f ]</position>
<range>%.1f</range>
</camera>''' % (self.camera_position[0],
self.camera_position[1],
self.camera_position[2],
self.camera_range))
print (' </road>\n')
class vamos_track ():
elevation_distance_range = [30.0, 400.0]
elevation_change_range = [-1.0, 1.0]
elevation_vertical_factor = 0.1
first_segment_length = 200.0
def __init__ (self, close):
self.close = close
# Accumulated data
self.elevation = []
self.segments = []
self.length = 0
self.elevation;
self.e_start = 0 # The indices of the elevation array
self.e_end = 0 # to be used for a given segment.
self.camera_distance = 0.0
def construct (self):
self.build_track ()
self.build_elevation ()
self.add_braking_markers ()
def add_braking_markers (self):
'''Go through the segments in reverse to find straights that precede
curves.'''
last_type = ''
for s in reversed (self.segments):
if s.name == 'straight':
if last_type == 'left turn':
s.add_braking_markers ('right')
elif last_type == 'right turn':
s.add_braking_markers ('left')
last_type = s.name
def build_elevation (self):
'''Construct an array of (distance, elevation) points for the entire
track'''
# Keep the starting area relatively flat.
distance = self.first_segment_length
height = 0.0
elevation = [[distance/2, height], [distance, height]]
while distance <= self.length:
delta_d = random_in_range (self.elevation_distance_range)
distance += delta_d
height += (random_in_range (self.elevation_change_range)
* self.elevation_vertical_factor
* delta_d)
elevation.append ([distance, height])
# For circuits, scale the elevation so it's back to zero at the start
# line.
if self.close:
e_last = elevation[-2]
for e in elevation[:-1]:
e[1] -= e_last[1] * e[0]/e_last[0]
elevation[-1][1] = elevation[0][1]
distance = 0.0
i = 0
for s in self.segments:
end_distance = distance + s.length
while elevation [i][0] < end_distance:
s.add_elevation (elevation [i][0] - distance, elevation [i][1])
i += 1
distance = end_distance
# Avoid extrapolating the end of an open track.
if not self.close:
last = self.segments [-1]
last.add_elevation (last.length, elevation [-1][1])
def add_segment (self, length, radius):
s = road_segment (length, radius)
self.camera_distance += length/2.0
# On each turn, add a camera halfway through. Switch to that camera
# halfway between this camera and the last camera.
if s.radius != 0.0:
s.add_camera ([length/2.0, 20.0, 5.0], self.camera_distance/2.0)
self.camera_distance = 0.0
self.camera_distance += length/2.0
self.segments.append (s)
self.e_start = self.e_end
self.length += length
def write_track (self, output_file):
out = open (output_file, 'w')
sys.stdout = out
print (header)
first = True
turn = 0
for s in self.segments:
if s.name != 'straight':
turn += 1
s.print_segment (first, turn)
else:
s.print_segment (first)
first = False
if self.close:
print (' <circuit/>')
print (' <timing-line>%d</timing-line>' % 10)
print (' <timing-line>%d</timing-line>' % (self.length/3))
print (' <timing-line>%d</timing-line>' % (2*self.length/3))
else:
for s in range (1000, int (self.length), 1000):
print (' <timing-line>%d</timing-line>' % s)
print ('</track>')
out.close ()