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ets.action_mapping import ActionMapping
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
CAMERA_HEIGHT = 40
|
| 6 |
+
CAMERA_WIDTH = 64
|
| 7 |
+
|
| 8 |
+
scene = sm.Scene(engine="unity")
|
| 9 |
+
# scene = sm.Scene()
|
| 10 |
+
scene += sm.LightSun()
|
| 11 |
+
scene += sm.Box(
|
| 12 |
+
name="table_surface",
|
| 13 |
+
position=[1, 0, 1],
|
| 14 |
+
bounds=[0, 7, 0, 1, 0, 11],
|
| 15 |
+
material=sm.Material.GREEN,
|
| 16 |
+
with_collider=True,
|
| 17 |
+
with_rigid_body=True,
|
| 18 |
+
physics_component=sm.RigidBodyComponent(
|
| 19 |
+
use_gravity=True,
|
| 20 |
+
kinematic=True,
|
| 21 |
+
collision_detection="continuous"
|
| 22 |
+
),
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
scene += sm.Box(
|
| 26 |
+
name="border_left",
|
| 27 |
+
position=[0, 0, 0],
|
| 28 |
+
bounds=[0, 1, 0, 2, 0, 13],
|
| 29 |
+
material=sm.Material.GREEN,
|
| 30 |
+
with_collider=True,
|
| 31 |
+
with_rigid_body=True,
|
| 32 |
+
physics_component=sm.RigidBodyComponent(
|
| 33 |
+
use_gravity=True,
|
| 34 |
+
kinematic=True,
|
| 35 |
+
collision_detection="continuous"
|
| 36 |
+
),
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
scene += sm.Box(
|
| 40 |
+
name="border_right",
|
| 41 |
+
position=[7, 0, 0],
|
| 42 |
+
bounds=[0, 1, 0, 2, 0, 13],
|
| 43 |
+
material=sm.Material.GREEN,
|
| 44 |
+
with_collider=True,
|
| 45 |
+
with_rigid_body=True,
|
| 46 |
+
physics_component=sm.RigidBodyComponent(
|
| 47 |
+
use_gravity=True,
|
| 48 |
+
mass=1,
|
| 49 |
+
kinematic=True,
|
| 50 |
+
collision_detection="continuous"
|
| 51 |
+
),
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
scene += sm.Box(
|
| 55 |
+
name="border_top",
|
| 56 |
+
position=[0, 0, 0],
|
| 57 |
+
bounds=[0, 8, 0, 2, 0, 1],
|
| 58 |
+
material=sm.Material.GREEN,
|
| 59 |
+
with_collider=True,
|
| 60 |
+
with_rigid_body=True,
|
| 61 |
+
physics_component=sm.RigidBodyComponent(
|
| 62 |
+
use_gravity=True,
|
| 63 |
+
kinematic=True,
|
| 64 |
+
collision_detection="continuous"
|
| 65 |
+
),
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
scene += sm.Box(
|
| 69 |
+
name="border_bottom",
|
| 70 |
+
position=[0, 0, 12],
|
| 71 |
+
bounds=[0, 8, 0, 2, 0, 1],
|
| 72 |
+
material=sm.Material.GREEN,
|
| 73 |
+
with_collider=True,
|
| 74 |
+
with_rigid_body=True,
|
| 75 |
+
physics_component=sm.RigidBodyComponent(
|
| 76 |
+
use_gravity=True,
|
| 77 |
+
kinematic=True,
|
| 78 |
+
collision_detection="continuous"
|
| 79 |
+
),
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
cue_ball = sm.Sphere(
|
| 83 |
+
name="cue_ball",
|
| 84 |
+
position=[4, 1.2, 9],
|
| 85 |
+
radius=0.2,
|
| 86 |
+
material=sm.Material.WHITE,
|
| 87 |
+
with_collider=True,
|
| 88 |
+
with_rigid_body=True,
|
| 89 |
+
physics_component=sm.RigidBodyComponent(
|
| 90 |
+
use_gravity=True,
|
| 91 |
+
constraints=["freeze_position_y"],
|
| 92 |
+
mass=1,
|
| 93 |
+
collision_detection="continuous"
|
| 94 |
+
),
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
cue_ball.is_actor = True
|
| 99 |
+
|
| 100 |
+
mapping = [
|
| 101 |
+
ActionMapping("add_force", axis=[0, 0, -1], is_impulse=True),
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
cue_ball.actuator = sm.Actuator(
|
| 105 |
+
mapping=mapping,
|
| 106 |
+
high=[10],
|
| 107 |
+
low=[-10],
|
| 108 |
+
shape=(1,)
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
cue_ball += sm.StateSensor(target_entity=cue_ball, properties=["position"])
|
| 112 |
+
|
| 113 |
+
scene += cue_ball
|
| 114 |
+
|
| 115 |
+
target_ball = sm.Sphere(
|
| 116 |
+
name="target_ball",
|
| 117 |
+
position=[4, 1.2, 6],
|
| 118 |
+
radius=0.2,
|
| 119 |
+
material=sm.Material.RED,
|
| 120 |
+
with_collider=True,
|
| 121 |
+
with_rigid_body=True,
|
| 122 |
+
physics_component=sm.RigidBodyComponent(
|
| 123 |
+
use_gravity=True,
|
| 124 |
+
constraints=["freeze_position_y"],
|
| 125 |
+
mass=0.5,
|
| 126 |
+
collision_detection="continuous"
|
| 127 |
+
),
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
scene += target_ball
|
| 131 |
+
|
| 132 |
+
scene += sm.Camera(name="camera", position=[0,0,0], rotation=[35, 0, 0])
|
| 133 |
+
|
| 134 |
+
scene.show()
|
| 135 |
+
|
| 136 |
+
# # uncomment here
|
| 137 |
+
env = sm.RLEnv(scene)
|
| 138 |
+
env.reset()
|
| 139 |
+
|
| 140 |
+
for i in range(500):
|
| 141 |
+
action = scene.action_space.sample()
|
| 142 |
+
|
| 143 |
+
print("###############################")
|
| 144 |
+
obs, reward, done, info = env.step(action)
|
| 145 |
+
print("###############################")
|
| 146 |
+
print(action)
|
| 147 |
+
print(f"Ball position {obs.get('StateSensor')}")
|
| 148 |
+
print(f"step {i}, reward {reward[0]}, observation")
|
| 149 |
+
time.sleep(0.1)
|
| 150 |
+
|
| 151 |
+
env.close()
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
# input("Press enter to continue...")
|