File size: 3,610 Bytes
23dce22 |
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 |
import simulate as sm
from simulate.assets.action_mapping import ActionMapping
import time
CAMERA_HEIGHT = 40
CAMERA_WIDTH = 64
scene = sm.Scene(engine="unity")
# scene = sm.Scene()
scene += sm.LightSun()
scene += sm.Box(
name="table_surface",
position=[1, 0, 1],
bounds=[0, 7, 0, 1, 0, 11],
material=sm.Material.GREEN,
with_collider=True,
with_rigid_body=True,
physics_component=sm.RigidBodyComponent(
use_gravity=True,
kinematic=True,
collision_detection="continuous"
),
)
scene += sm.Box(
name="border_left",
position=[0, 0, 0],
bounds=[0, 1, 0, 2, 0, 13],
material=sm.Material.GREEN,
with_collider=True,
with_rigid_body=True,
physics_component=sm.RigidBodyComponent(
use_gravity=True,
kinematic=True,
collision_detection="continuous"
),
)
scene += sm.Box(
name="border_right",
position=[7, 0, 0],
bounds=[0, 1, 0, 2, 0, 13],
material=sm.Material.GREEN,
with_collider=True,
with_rigid_body=True,
physics_component=sm.RigidBodyComponent(
use_gravity=True,
mass=1,
kinematic=True,
collision_detection="continuous"
),
)
scene += sm.Box(
name="border_top",
position=[0, 0, 0],
bounds=[0, 8, 0, 2, 0, 1],
material=sm.Material.GREEN,
with_collider=True,
with_rigid_body=True,
physics_component=sm.RigidBodyComponent(
use_gravity=True,
kinematic=True,
collision_detection="continuous"
),
)
scene += sm.Box(
name="border_bottom",
position=[0, 0, 12],
bounds=[0, 8, 0, 2, 0, 1],
material=sm.Material.GREEN,
with_collider=True,
with_rigid_body=True,
physics_component=sm.RigidBodyComponent(
use_gravity=True,
kinematic=True,
collision_detection="continuous"
),
)
cue_ball = sm.Sphere(
name="cue_ball",
position=[4, 1.2, 9],
radius=0.2,
material=sm.Material.WHITE,
with_collider=True,
with_rigid_body=True,
physics_component=sm.RigidBodyComponent(
use_gravity=True,
constraints=["freeze_position_y"],
mass=1,
collision_detection="continuous"
),
)
cue_ball.is_actor = True
mapping = [
ActionMapping("add_force", axis=[0, 0, -1], is_impulse=True),
]
cue_ball.actuator = sm.Actuator(
mapping=mapping,
high=[10],
low=[-10],
shape=(1,)
)
cue_ball += sm.StateSensor(target_entity=cue_ball, properties=["position"])
scene += cue_ball
target_ball = sm.Sphere(
name="target_ball",
position=[4, 1.2, 6],
radius=0.2,
material=sm.Material.RED,
with_collider=True,
with_rigid_body=True,
physics_component=sm.RigidBodyComponent(
use_gravity=True,
constraints=["freeze_position_y"],
mass=0.5,
collision_detection="continuous"
),
)
scene += target_ball
scene += sm.Camera(name="camera", position=[0,0,0], rotation=[35, 0, 0])
scene.show()
# # uncomment here
env = sm.RLEnv(scene)
env.reset()
for i in range(500):
action = scene.action_space.sample()
print("###############################")
obs, reward, done, info = env.step(action)
print("###############################")
print(action)
print(f"Ball position {obs.get('StateSensor')}")
print(f"step {i}, reward {reward[0]}, observation")
time.sleep(0.1)
env.close()
# input("Press enter to continue...")
|