-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.py
617 lines (515 loc) · 24 KB
/
env.py
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import numpy as np
import Box2D
from Box2D.b2 import (edgeShape, circleShape, fixtureDef, polygonShape, revoluteJointDef, distanceJointDef,
contactListener)
import gym
from gym import spaces
from gym.utils import seeding
import pyglet
"""
The objective of this environment is to land a rocket on a ship. 此环境的目标是将火箭降落在一艘船上。
STATE VARIABLES 状态变量
The state consists of the following variables: 状态包括以下变量:
- x position x 位置
- y position y 位置
- angle 角度
- first leg ground contact indicator 第一条腿接触地面的指示器
- second leg ground contact indicator 第二条腿接触地面的指示器
- throttle 节流器
- engine gimbal 引擎万向节
If VEL_STATE is set to true, the velocities are included: 如果 VEL_STATE 设置为 true,则速度变量也包括在内:
- x velocity x 轴速度
- y velocity y 轴速度
- angular velocity 角速度
all state variables are roughly in the range [-1, 1] 所有状态变量大致在 [-1, 1] 范围内。
CONTROL INPUTS 控制输入
Discrete control inputs are: 离散控制输入包括:
- gimbal left 万向节左转
- gimbal right 万向节右转
- throttle up 提高节流
- throttle down 降低节流
- use first control thruster 使用第一控制喷射器
- use second control thruster 使用第二控制喷射器
- no action 无操作
Continuous control inputs are: 连续控制输入包括:
- gimbal (left/right) 万向节(左/右)
- throttle (up/down) 节流(增加/减少)
- control thruster (left/right) 控制喷射器(左/右)
"""
CONTINUOUS = True
VEL_STATE = True # Add velocity info to state
FPS = 60
SCALE_S = 0.35 # Temporal Scaling, lower is faster - adjust forces appropriately
INITIAL_RANDOM = 0.4 # Random scaling of initial velocity, higher is more difficult
START_HEIGHT = 1000.0
START_SPEED = 80.0
# ROCKET
MIN_THROTTLE = 0.4
GIMBAL_THRESHOLD = 0.4
MAIN_ENGINE_POWER = 1600 * SCALE_S
SIDE_ENGINE_POWER = 100 / FPS * SCALE_S
ROCKET_WIDTH = 3.66 * SCALE_S
ROCKET_HEIGHT = ROCKET_WIDTH / 3.7 * 47.9
ENGINE_HEIGHT = ROCKET_WIDTH * 0.5
ENGINE_WIDTH = ENGINE_HEIGHT * 0.7
THRUSTER_HEIGHT = ROCKET_HEIGHT * 0.86
# LEGS
LEG_LENGTH = ROCKET_WIDTH * 2.2
BASE_ANGLE = -0.27
SPRING_ANGLE = 0.27
LEG_AWAY = ROCKET_WIDTH / 2
# SHIP
SHIP_HEIGHT = ROCKET_WIDTH
SHIP_WIDTH = SHIP_HEIGHT * 40
# VIEWPORT
VIEWPORT_H = 720
VIEWPORT_W = 500
H = 1.1 * START_HEIGHT * SCALE_S
W = float(VIEWPORT_W) / VIEWPORT_H * H
# SMOKE FOR VISUALS
MAX_SMOKE_LIFETIME = 2 * FPS
MEAN = np.array([-0.034, -0.15, -0.016, 0.0024, 0.0024, 0.137,
- 0.02, -0.01, -0.8, 0.002])
VAR = np.sqrt(np.array([0.08, 0.33, 0.0073, 0.0023, 0.0023, 0.8,
0.085, 0.0088, 0.063, 0.076]))
class ContactDetector(contactListener):
def __init__(self, env):
contactListener.__init__(self)
self.env = env
def BeginContact(self, contact):
if self.env.water in [contact.fixtureA.body, contact.fixtureB.body] \
or self.env.lander in [contact.fixtureA.body, contact.fixtureB.body] \
or self.env.containers[0] in [contact.fixtureA.body, contact.fixtureB.body] \
or self.env.containers[1] in [contact.fixtureA.body, contact.fixtureB.body]:
self.env.game_over = True
self.env.game_over_result = "Contact Detector"
else:
for i in range(2):
if self.env.legs[i] in [contact.fixtureA.body, contact.fixtureB.body]:
self.env.legs[i].ground_contact = True
def EndContact(self, contact):
for i in range(2):
if self.env.legs[i] in [contact.fixtureA.body, contact.fixtureB.body]:
self.env.legs[i].ground_contact = False
class DrawText:
def __init__(self, label:pyglet.text.Label):
self.label=label
def set_text(self,text):
self.label.text=text
self.label.draw()
def render(self):
self.label.draw()
class RocketLander(gym.Env):
metadata = {
'render.modes': ['human', 'rgb_array'],
'video.frames_per_second': FPS
}
def __init__(self):
self._seed()
self.viewer = None
self.episode_number = 0
self.world = Box2D.b2World()
self.water = None
self.lander = None
self.engine = None
self.ship = None
self.legs = []
high = np.array([1, 1, 1, 1, 1, 1, 1, np.inf, np.inf, np.inf], dtype=np.float32)
low = -high
if not VEL_STATE:
high = high[0:7]
low = low[0:7]
self.observation_space = spaces.Box(low, high, dtype=np.float32)
if CONTINUOUS:
self.action_space = spaces.Box(-1.0, +1.0, (3,), dtype=np.float32)
else:
self.action_space = spaces.Discrete(7)
self.reset()
def _seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def _destroy(self):
if not self.water:
return
self.world.contactListener = None
self.world.DestroyBody(self.water)
self.water = None
self.world.DestroyBody(self.lander)
self.lander = None
self.world.DestroyBody(self.ship)
self.ship = None
self.world.DestroyBody(self.legs[0])
self.world.DestroyBody(self.legs[1])
self.legs = []
self.world.DestroyBody(self.containers[0])
self.world.DestroyBody(self.containers[1])
self.containers = []
def reset(self):
self._destroy()
self.world.contactListener_keepref = ContactDetector(self)
self.world.contactListener = self.world.contactListener_keepref
self.game_over = False
self.prev_shaping = None
self.throttle = 0
self.gimbal = 0.0
self.landed_ticks = 0
self.stepnumber = 0
self.smoke = []
self.powerUse = 0
self.distance_min = 10.0
self.game_over_result = ""
# self.terrainheigth = self.np_random.uniform(H / 20, H / 10)
self.terrainheigth = H / 20
self.shipheight = self.terrainheigth + SHIP_HEIGHT
# ship_pos = self.np_random.uniform(0, SHIP_WIDTH / SCALE) + SHIP_WIDTH / SCALE
ship_pos = W / 2
self.helipad_x1 = ship_pos - SHIP_WIDTH / 2
self.helipad_x2 = self.helipad_x1 + SHIP_WIDTH
self.helipad_y = self.terrainheigth + SHIP_HEIGHT
self.water = self.world.CreateStaticBody(
fixtures=fixtureDef(
shape=polygonShape(vertices=((0, 0), (W, 0), (W, self.terrainheigth), (0, self.terrainheigth))),
friction=0.1,
restitution=0.0)
)
self.water.color1 = rgb(70, 96, 176)
self.ship = self.world.CreateStaticBody(
fixtures=fixtureDef(
shape=polygonShape(
vertices=((self.helipad_x1, self.terrainheigth),
(self.helipad_x2, self.terrainheigth),
(self.helipad_x2, self.terrainheigth + SHIP_HEIGHT),
(self.helipad_x1, self.terrainheigth + SHIP_HEIGHT))),
friction=0.5,
restitution=0.0)
)
self.containers = []
for side in [-1, 1]:
self.containers.append(self.world.CreateStaticBody(
fixtures=fixtureDef(
shape=polygonShape(
vertices=((ship_pos + side * 0.95 * SHIP_WIDTH / 2, self.helipad_y),
(ship_pos + side * 0.95 * SHIP_WIDTH / 2, self.helipad_y + SHIP_HEIGHT),
(ship_pos + side * 0.95 * SHIP_WIDTH / 2 - side * SHIP_HEIGHT,
self.helipad_y + SHIP_HEIGHT),
(ship_pos + side * 0.95 * SHIP_WIDTH / 2 - side * SHIP_HEIGHT, self.helipad_y)
)),
friction=0.2,
restitution=0.0)
))
self.containers[-1].color1 = rgb(206, 206, 2)
self.ship.color1 = (0.2, 0.2, 0.2)
initial_x = W / 2 + W * np.random.uniform(-0.3, 0.3)
initial_y = H * 0.95
self.lander = self.world.CreateDynamicBody(
position=(initial_x, initial_y),
angle=0.0,
fixtures=fixtureDef(
shape=polygonShape(vertices=((-ROCKET_WIDTH / 2, 0),
(+ROCKET_WIDTH / 2, 0),
(ROCKET_WIDTH / 2, +ROCKET_HEIGHT),
(-ROCKET_WIDTH / 2, +ROCKET_HEIGHT))),
density=1.0,
friction=0.5,
categoryBits=0x0010,
maskBits=0x001,
restitution=0.0)
)
self.lander.color1 = rgb(230, 230, 230)
for i in [-1, +1]:
leg = self.world.CreateDynamicBody(
position=(initial_x - i * LEG_AWAY, initial_y + ROCKET_WIDTH * 0.2),
angle=(i * BASE_ANGLE),
fixtures=fixtureDef(
shape=polygonShape(
vertices=((0, 0), (0, LEG_LENGTH / 25), (i * LEG_LENGTH, 0), (i * LEG_LENGTH, -LEG_LENGTH / 20),
(i * LEG_LENGTH / 3, -LEG_LENGTH / 7))),
density=1,
restitution=0.0,
friction=0.2,
categoryBits=0x0020,
maskBits=0x001)
)
leg.ground_contact = False
leg.color1 = (0.25, 0.25, 0.25)
rjd = revoluteJointDef(
bodyA=self.lander,
bodyB=leg,
localAnchorA=(i * LEG_AWAY, ROCKET_WIDTH * 0.2),
localAnchorB=(0, 0),
enableLimit=True,
maxMotorTorque=2500.0,
motorSpeed=-0.05 * i,
enableMotor=True
)
djd = distanceJointDef(bodyA=self.lander,
bodyB=leg,
anchorA=(i * LEG_AWAY, ROCKET_HEIGHT / 8),
anchorB=leg.fixtures[0].body.transform * (i * LEG_LENGTH, 0),
collideConnected=False,
frequencyHz=0.01,
dampingRatio=0.9
)
if i == 1:
rjd.lowerAngle = -SPRING_ANGLE
rjd.upperAngle = 0
else:
rjd.lowerAngle = 0
rjd.upperAngle = + SPRING_ANGLE
leg.joint = self.world.CreateJoint(rjd)
leg.joint2 = self.world.CreateJoint(djd)
self.legs.append(leg)
self.lander.linearVelocity = (
-self.np_random.uniform(0, INITIAL_RANDOM) * START_SPEED * (initial_x - W / 2) / W,
-START_SPEED)
self.lander.angularVelocity = (1 + INITIAL_RANDOM) * np.random.uniform(-1, 1)
self.drawlist = self.legs + [self.water] + [self.ship] + self.containers + [self.lander]
if CONTINUOUS:
return self.step([0, 0, 0])[0]
else:
return self.step(6)[0]
def step(self, action):
# 控制喷射器
self.force_dir = 0
if CONTINUOUS:
np.clip(action, -1, 1)
self.gimbal += action[0] * 0.15 / FPS
self.throttle += action[1] * 0.5 / FPS
if action[2] > 0.5:
self.force_dir = 1
elif action[2] < -0.5:
self.force_dir = -1
else:
if action == 0:
self.gimbal += 0.01
elif action == 1:
self.gimbal -= 0.01
elif action == 2:
self.throttle += 0.01
elif action == 3:
self.throttle -= 0.01
elif action == 4: # left
self.force_dir = -1
elif action == 5: # right
self.force_dir = 1
self.gimbal = np.clip(self.gimbal, -GIMBAL_THRESHOLD, GIMBAL_THRESHOLD)
self.throttle = np.clip(self.throttle, 0.0, 1.0)
self.power = 0 if self.throttle == 0.0 else MIN_THROTTLE + self.throttle * (1 - MIN_THROTTLE)
self.powerUse += self.power
# main engine force
force_pos = (self.lander.position[0], self.lander.position[1])
force = (-np.sin(self.lander.angle + self.gimbal) * MAIN_ENGINE_POWER * self.power,
np.cos(self.lander.angle + self.gimbal) * MAIN_ENGINE_POWER * self.power)
self.lander.ApplyForce(force=force, point=force_pos, wake=False)
# control thruster force
force_pos_c = self.lander.position + THRUSTER_HEIGHT * np.array(
(np.sin(self.lander.angle), np.cos(self.lander.angle)))
force_c = (-self.force_dir * np.cos(self.lander.angle) * SIDE_ENGINE_POWER,
self.force_dir * np.sin(self.lander.angle) * SIDE_ENGINE_POWER)
self.lander.ApplyLinearImpulse(impulse=force_c, point=force_pos_c, wake=False)
self.world.Step(1.0 / FPS, 60, 60)
pos = self.lander.position
vel_l = np.array(self.lander.linearVelocity) / START_SPEED
vel_a = self.lander.angularVelocity
x_distance = (pos.x - W / 2) / W
y_distance = (pos.y - self.shipheight) / (H - self.shipheight)
angle = (self.lander.angle / np.pi) % 2
if angle > 1:
angle -= 2
state = [
2 * x_distance,
2 * (y_distance - 0.5),
angle,
1.0 if self.legs[0].ground_contact else 0.0,
1.0 if self.legs[1].ground_contact else 0.0,
2 * (self.throttle - 0.5),
(self.gimbal / GIMBAL_THRESHOLD)
]
if VEL_STATE:
state.extend([vel_l[0],
vel_l[1],
vel_a])
# REWARD -------------------------------------------------------------------------------------------------------
# state variables for reward
distance = np.linalg.norm((3 * x_distance, y_distance)) # weight x position more
speed = np.linalg.norm(vel_l)
groundcontact = self.legs[0].ground_contact or self.legs[1].ground_contact
brokenleg = (self.legs[0].joint.angle < 0 or self.legs[1].joint.angle > -0) and groundcontact
outside = abs(pos.x - W / 2) > W / 2 or pos.y > H
fuelcost = 0.1 * (0 * self.power + abs(self.force_dir)) / FPS
landed = self.legs[0].ground_contact and self.legs[1].ground_contact and speed < 0.1
done = False
reward = -fuelcost
if outside:
self.game_over = True
self.game_over_result = "outside"
if brokenleg:
self.game_over = True
self.game_over_result = "brokenleg"
# 限制
if self.stepnumber > 3000:
self.game_over = True
self.game_over_result = "stepnumber"
if (self.distance_min + 0.01) < y_distance:
self.game_over = True
self.game_over_result = "distance_min: " + ("%.6f" % (y_distance - self.distance_min))
# print("distance", self.stepnumber, y_distance - self.distance_min)
if self.distance_min > y_distance:
self.distance_min = y_distance
shaping = 0
if self.game_over:
done = True
else:
# reward shaping
shaping = -0.5 * (distance + speed + abs(angle) ** 2)
shaping += 0.1 * (self.legs[0].ground_contact + self.legs[1].ground_contact)
if self.prev_shaping is not None:
reward += shaping - self.prev_shaping
self.prev_shaping = shaping
if landed:
self.landed_ticks += 1
else:
self.landed_ticks = 0
if self.landed_ticks == FPS:
reward = 1.0
done = True
self.game_over_result = "Success"
if done:
reward += max(-1, 0 - 2 * (speed + distance + abs(angle) + abs(vel_a)))
elif not groundcontact:
reward -= 0.25 / FPS
reward = np.clip(reward, -1, 1)
# REWARD -------------------------------------------------------------------------------------------------------
self.stepnumber += 1
state = (state - MEAN[:len(state)]) / VAR[:len(state)]
self.reward = reward
self.speed = speed
self.angle = angle
self.distance = distance
if self.viewer:
print("step", "%.6f" % reward, "fuelcost:%.6f" % fuelcost, 'FPS: %.6f' % (0.25 / FPS), "shaping: %.6f" % shaping, "legs: %d/%d" % (self.legs[0].ground_contact, self.legs[1].ground_contact))
if done:
print("over", self.stepnumber, "%.6f" % reward, self.game_over_result)
print("")
return np.array(state), reward, done, {"speed": speed, "distance": y_distance, "angle": angle, "vel_a": vel_a, "power": self.powerUse}
def render(self, mode='human', close=False):
if close:
if self.viewer is not None:
self.viewer.close()
self.viewer = None
return
from gym.envs.classic_control import rendering
if self.viewer is None:
self.viewer = rendering.Viewer(VIEWPORT_W, VIEWPORT_H)
self.viewer.set_bounds(0, W, 0, H)
sky = rendering.FilledPolygon(((0, 0), (0, H), (W, H), (W, 0)))
self.sky_color = rgb(126, 150, 233)
sky.set_color(*self.sky_color)
self.sky_color_half_transparent = np.array((np.array(self.sky_color) + rgb(255, 255, 255))) / 2
self.viewer.add_geom(sky)
self.rockettrans = rendering.Transform()
engine = rendering.FilledPolygon(((0, 0),
(ENGINE_WIDTH / 2, -ENGINE_HEIGHT),
(-ENGINE_WIDTH / 2, -ENGINE_HEIGHT)))
self.enginetrans = rendering.Transform()
engine.add_attr(self.enginetrans)
engine.add_attr(self.rockettrans)
engine.set_color(.4, .4, .4)
self.viewer.add_geom(engine)
self.fire = rendering.FilledPolygon(((ENGINE_WIDTH * 0.4, 0), (-ENGINE_WIDTH * 0.4, 0),
(-ENGINE_WIDTH * 1.2, -ENGINE_HEIGHT * 5),
(0, -ENGINE_HEIGHT * 8), (ENGINE_WIDTH * 1.2, -ENGINE_HEIGHT * 5)))
self.fire.set_color(*rgb(255, 230, 107))
self.firescale = rendering.Transform(scale=(1, 1))
self.firetrans = rendering.Transform(translation=(0, -ENGINE_HEIGHT))
self.fire.add_attr(self.firescale)
self.fire.add_attr(self.firetrans)
self.fire.add_attr(self.enginetrans)
self.fire.add_attr(self.rockettrans)
smoke = rendering.FilledPolygon(((ROCKET_WIDTH / 2, THRUSTER_HEIGHT * 1),
(ROCKET_WIDTH * 3, THRUSTER_HEIGHT * 1.03),
(ROCKET_WIDTH * 4, THRUSTER_HEIGHT * 1),
(ROCKET_WIDTH * 3, THRUSTER_HEIGHT * 0.97)))
smoke.set_color(*self.sky_color_half_transparent)
self.smokescale = rendering.Transform(scale=(1, 1))
smoke.add_attr(self.smokescale)
smoke.add_attr(self.rockettrans)
self.viewer.add_geom(smoke)
self.gridfins = []
for i in (-1, 1):
finpoly = (
(i * ROCKET_WIDTH * 1.1, THRUSTER_HEIGHT * 1.01),
(i * ROCKET_WIDTH * 0.4, THRUSTER_HEIGHT * 1.01),
(i * ROCKET_WIDTH * 0.4, THRUSTER_HEIGHT * 0.99),
(i * ROCKET_WIDTH * 1.1, THRUSTER_HEIGHT * 0.99)
)
gridfin = rendering.FilledPolygon(finpoly)
gridfin.add_attr(self.rockettrans)
gridfin.set_color(0.25, 0.25, 0.25)
self.gridfins.append(gridfin)
self.label1 = DrawText(pyglet.text.Label( font_size=10, x=10, y=370, ))
self.viewer.add_geom(self.label1)
self.label2 = DrawText(pyglet.text.Label( font_size=10, x=10, y=370 - 16, ))
self.viewer.add_geom(self.label2)
self.label3 = DrawText(pyglet.text.Label( font_size=10, x=10, y=370 - 16*2, ))
self.viewer.add_geom(self.label3)
self.label4 = DrawText(pyglet.text.Label( font_size=10, x=10, y=370 - 16*3, ))
self.viewer.add_geom(self.label4)
self.label5 = DrawText(pyglet.text.Label( font_size=10, x=10, y=370 - 16*4, ))
self.viewer.add_geom(self.label5)
self.label6 = DrawText(pyglet.text.Label( font_size=10, x=10, y=370 - 16*5, ))
self.viewer.add_geom(self.label6)
self.label7 = DrawText(pyglet.text.Label( font_size=10, x=10, y=370 - 16*6, ))
self.viewer.add_geom(self.label7)
self.label8 = DrawText(pyglet.text.Label( font_size=10, x=10, y=370 - 16*7, color=(255, 0, 0, 255), ))
self.viewer.add_geom(self.label8)
if self.stepnumber % round(FPS / 10) == 0 and self.power > 0:
s = [MAX_SMOKE_LIFETIME * self.power, # total lifetime
0, # current lifetime
self.power * (1 + 0.2 * np.random.random()), # size
np.array(self.lander.position)
+ self.power * ROCKET_WIDTH * 10 * np.array((np.sin(self.lander.angle + self.gimbal),
-np.cos(self.lander.angle + self.gimbal)))
+ self.power * 5 * (np.random.random(2) - 0.5)] # position
self.smoke.append(s)
for s in self.smoke:
s[1] += 1
if s[1] > s[0]:
self.smoke.remove(s)
continue
t = rendering.Transform(translation=(s[3][0], s[3][1] + H * s[1] / 2000))
self.viewer.draw_circle(radius=0.05 * s[1] + s[2],
color=self.sky_color + (1 - (2 * s[1] / s[0] - 1) ** 2) / 3 * (
self.sky_color_half_transparent - self.sky_color)).add_attr(t)
self.viewer.add_onetime(self.fire)
for g in self.gridfins:
self.viewer.add_onetime(g)
for obj in self.drawlist:
for f in obj.fixtures:
trans = f.body.transform
path = [trans * v for v in f.shape.vertices]
self.viewer.draw_polygon(path, color=obj.color1)
for l in zip(self.legs, [-1, 1]):
path = [self.lander.fixtures[0].body.transform * (l[1] * ROCKET_WIDTH / 2, ROCKET_HEIGHT / 8),
l[0].fixtures[0].body.transform * (l[1] * LEG_LENGTH * 0.8, 0)]
self.viewer.draw_polyline(path, color=self.ship.color1, linewidth=1 if START_HEIGHT > 500 else 2)
self.viewer.draw_polyline(((self.helipad_x2, self.terrainheigth + SHIP_HEIGHT),
(self.helipad_x1, self.terrainheigth + SHIP_HEIGHT)),
color=rgb(206, 206, 2),
linewidth=1)
self.rockettrans.set_translation(*self.lander.position)
self.rockettrans.set_rotation(self.lander.angle)
self.enginetrans.set_rotation(self.gimbal)
self.firescale.set_scale(newx=1, newy=self.power * np.random.uniform(1, 1.3))
self.smokescale.set_scale(newx=self.force_dir, newy=1)
self.label1.set_text("step: %04i" % self.stepnumber)
self.label2.set_text("score: %.6f" % self.reward)
self.label3.set_text("power: %.6f" % self.power)
self.label4.set_text("angle: %.6f" % (self.angle * 3.1415))
self.label5.set_text("speed: %.6f" % self.speed)
self.label6.set_text("distance: %.6f" % self.distance)
self.label8.set_text(self.game_over_result)
# self.power
return self.viewer.render(return_rgb_array=mode == 'rgb_array')
def rgb(r, g, b):
return float(r) / 255, float(g) / 255, float(b) / 255