We start building an Enviroment, based on cartpole-v0. We are trying to achieve a similar behaviour of our pole, so it doesn't rotate in the centerpoint, but at the Bottom. We are using set_rotation function from gym.classic_control.rendering. But there is no opportunity to set an anchorpoint.
We tried to translate the filledPolygon before rotation in different directions, but the anchorpoint remains in centerpoint.
import math
import gym
from gym import spaces, logger
from gym.utils import seeding
import numpy as np
from os import path
class THT_Env(gym.Env):
'''
I shorten the code to the render. The other parts of the code is working fine.
'''
def render(self, mode='human'):
screen_width = 600
screen_height = 600
lead_width = 6 #Lead diameter = 0.6mm
lead_length = 103 #Lead lenght ca. 10.3 mm
lead_spacing = 50 #Lead spacing 5 mm
body_height = 80 #Body height 8 mm
body_width = 65 #Body width 6.5 mm
pcb_thickness = 16 #1.6 mm
#pcb_hole_diameter = 9 #0.9mm
pcb_side = 270.5
pcb_middle = 41
if self.viewer is None:
from gym.envs.classic_control import rendering
self.viewer = rendering.Viewer(screen_width, screen_height)
# Initialize Body
fname = path.join(path.dirname(__file__), "assets/WYO_1nM.png")
body = rendering.Image(fname,body_width, body_height)
self.bodytrans = rendering.Transform()
body.add_attr(self.bodytrans)
self.viewer.add_geom(body)
# Initialize Lead 1
l, r, t, b = -lead_width/2, lead_width/2, lead_length/2, -lead_length/2
lead_1 = rendering.FilledPolygon([(l,b),(l,t),(r,t),(r,b)])
lead_1.set_color(.4, .4, .4)
self.lead_1_trans = rendering.Transform(translation=(lead_spacing/2, (-lead_length-body_height)/2))
lead_1.add_attr(self.lead_1_trans)
lead_1.add_attr(self.bodytrans)
self.viewer.add_geom(lead_1)
lead_2 = rendering.FilledPolygon([(l,b),(l,t),(r,t),(r,b)])
lead_2.set_color(.4, .4, .4)
#self.lead_2_trans = rendering.Transform(translation=(-lead_spacing/2, (-lead_length-body_height)/2))
self.lead_2_trans = rendering.Transform(translation=(0, (-lead_length-body_height)/2),rotation=np.pi/2)
lead_2.add_attr(self.lead_2_trans)
lead_2.add_attr(self.bodytrans)
self.viewer.add_geom(lead_2)
l, r, t, b = -pcb_side/2, pcb_side/2, pcb_thickness/2, -pcb_thickness/2
pcb_1 = rendering.FilledPolygon([(l,b),(l,t),(r,t),(r,b)])
pcb_1.set_color(.0, .42, .0)
self.pcb_1_trans = rendering.Transform(translation=(0+pcb_side/2, 110))
pcb_1.add_attr(self.pcb_1_trans)
self.viewer.add_geom(pcb_1)
pcb_2 = rendering.FilledPolygon([(l,b),(l,t),(r,t),(r,b)])
pcb_2.set_color(.0, .42, .0)
self.pcb_2_trans = rendering.Transform(translation=(screen_width-pcb_side/2, 110))
pcb_2.add_attr(self.pcb_2_trans)
self.viewer.add_geom(pcb_2)
l, r, t, b = -pcb_middle/2, pcb_middle/2, pcb_thickness/2, -pcb_thickness/2
self.pcb_mid = rendering.FilledPolygon([(l,b),(l,t),(r,t),(r,b)])
self.pcb_mid.set_color(.0, .42, .0)
self.pcb_mid_trans = rendering.Transform(translation=(screen_width/2, 110))
self.pcb_mid.add_attr(self.pcb_mid_trans)
self.viewer.add_geom(self.pcb_mid)
if self.state is None: return None
x = self.state
body_x = x[0]+screen_width/2
body_y = x[1]+screen_height/2+200
self.bodytrans.set_translation(body_x, body_y)
#self.lead_1_trans.set_translation(0,-lead_length/2)
#self.lead_1_trans.set_rotation(np.pi/2)
return self.viewer.render(return_rgb_array= mode == 'rgb_array')
def close(self):
if self.viewer:
self.viewer.close()
self.viewer = None
The rotation on the right image results in the circumstance the the pole(lead) is no longer connected to the body. What I expect is that the Anchor lies in the lower part of the body.
We were thinking the wrong wayfrom start of. Before we tryed to work with gym we build something with pyglet and learned that pyglet change anchor of images. When building our own gym we centered our leads and thought that we also could change the anchors. After a short pause I realized that the pole of cartpole is centred on the bottom with l,r,t,b.
So the simple solution in our situation was to change this line
l, r, t, b = -lead_width/2, lead_width/2, lead_length/2, -lead_length/2
into this line
l, r, t, b = -lead_width/2, lead_width/2, 0, -lead_length
So the result of this:
Anchor points of gyms pollygons are controlled by vertexes.