def __init__(self, width, height, immobile_objs, mobile_objs, manipulatable_obj, target_obj, showRender=True): super(Scenario_Generator, self).__init__() self.SCENARIO_WIDTH, self.SCENARIO_HEIGHT = width, height self.SCREEN_WIDTH, self.SCREEN_HEIGHT= self.SCENARIO_WIDTH * PPM, self.SCENARIO_HEIGHT * PPM self.showRender = showRender self.min_d = 9999999 self.solved = False self.impulse = (0,0) self.p = (0,0) self.current_man_id = manipulatable_obj['id'] # Tell the framework we're going to use contacts, so keep track of them # every Step. self.using_contacts=True self.beginContact = False self.current_contacts = set([]) self.contactAID = -1 self.contactBID = -1 #current simulation round self.round = 0 world = self.world self.trajectory = [] self.b2_objects = {} self.game_objects = [] self.target_obj = target_obj self.goal = 0 self.contacts_with_mobile_objs = [] if self.target_obj == None: self.target_obj = target_obj if self.target_obj['goal'] == "RIGHT": self.goal = - pi/2 elif self.target_obj['goal'] == "LEFT": self.goal = pi/2 for obj in immobile_objs: ## create a simulator object gobj = GOBJ(obj['id']) if obj['shape'] == "box": static_body = world.CreateStaticBody(position=obj['position'], shapes=polygonShape(box=obj['size']), angle=obj['angle'],userData=gobj) elif obj['shape'] == "polygon": static_body = world.CreateStaticBody(position=obj['position'], shapes=polygonShape(vertices=obj['vertices']), userData=gobj) # store coordinates info in the screen mass_center = static_body.fixtures[0].massData.center vertices=[(static_body.transform*v) for v in static_body.fixtures[0].shape] #vertices=[(v[0], SCREEN_HEIGHT-v[1]) for v in vertices] gobj.setPosScr(vertices) gobj.setMassCenterSrc(static_body.transform * mass_center) self.game_objects.append(gobj) self.b2_objects[obj['id']] = static_body self.mobile_objs_ids = [] for obj in mobile_objs: obj_id = obj['id'] if obj_id != self.target_obj['id']: gobj = GOBJ(obj_id,role=GOBJ.MOVEABLE) else: gobj = GOBJ(obj_id,role=GOBJ.TARGET) if obj['shape'] == "box": dynamic_body = world.CreateDynamicBody(position=obj['position'], userData=gobj) dynamic_body.CreatePolygonFixture(box=obj['size'],density=obj['density'], friction=obj['friction']) mass_center = dynamic_body.fixtures[0].massData.center vertices=[(dynamic_body.transform*v)for v in dynamic_body.fixtures[0].shape] #vertices=[(v[0], SCREEN_HEIGHT-v[1]) for v in vertices] gobj.setPosScr(vertices) gobj.setMassCenterSrc(dynamic_body.transform * mass_center) self.game_objects.append(gobj) self.b2_objects[obj['id']] = dynamic_body self.mobile_objs_ids.append(obj_id) gobj = GOBJ(manipulatable_obj['id'],role=GOBJ.MAN) man_dynamic_body = world.CreateDynamicBody(position=manipulatable_obj['position'], userData=gobj) if manipulatable_obj['shape'] == "box": man_dynamic_body.CreatePolygonFixture(box=manipulatable_obj['size'],density=manipulatable_obj['density'], friction=manipulatable_obj['friction']) mass_center = man_dynamic_body.fixtures[0].massData.center vertices=[(man_dynamic_body.transform*v)for v in man_dynamic_body.fixtures[0].shape] gobj.setPosScr(vertices) gobj.setMassCenterSrc(man_dynamic_body.transform * mass_center) elif manipulatable_obj['shape'] == "circle": man_dynamic_body.CreateFixture(shape=b2CircleShape(radius=manipulatable_obj['radius']), density=manipulatable_obj['density'], friction=manipulatable_obj['friction']) man_dynamic_body.fixtures[0].restitution = manipulatable_obj['restitution'] mass_center = man_dynamic_body.fixtures[0].massData.center position = man_dynamic_body.transform * man_dynamic_body.fixtures[0].shape.pos gobj.setPosScr([position]) gobj.setMassCenterSrc(man_dynamic_body.transform * mass_center) self.game_objects.append(gobj) self.b2_objects[manipulatable_obj['id']] = man_dynamic_body self.mobile_objs_ids.append(manipulatable_obj['id']) #print dir(man_dynamic_body) #print man_dynamic_body.position '''
def __init__(self, target_region=None, O_i_rect=None, action=None, showRender=False, checksolution=False, scenario_file='s5.json'): super(Scenario_Generator, self).__init__() self.showRender = showRender self.state = -1 self.min_d = 9999999 self.state_impulse = (0,0) # Tell the framework we're going to use contacts, so keep track of them # every Step. self.using_contacts=True self.beginContact = False self.contactAID = -1 self.contactBID = -1 #current simulation round self.round = 0 self.objID = 0 world = self.world self.objects = [] self.game_objects = [] self.target_region = target_region width, height, immobile_objs, mobile_objs, manipulatable_objs, target_obj = sr.loadScenario(scenario_file) for obj in immobile_objs: ## create a simulator object gobj = GOBJ(obj['id']) if obj['shape'] == "box": static_body = world.CreateStaticBody(position=obj['position'], shapes=polygonShape(box=obj['size']), angle=obj['angle'],userData=gobj) elif obj['shape'] == "polygon": static_body = world.CreateStaticBody(position=obj['position'], shapes=polygonShape(vertices=obj['vertices']), userData=gobj) # store coordinates info in the screen mass_center = static_body.fixtures[0].massData.center vertices=[(static_body.transform*v) for v in static_body.fixtures[0].shape] #vertices=[(v[0], SCREEN_HEIGHT-v[1]) for v in vertices] gobj.setPosScr(vertices) gobj.setMassCenterSrc(static_body.transform * mass_center) self.game_objects.append(gobj) target_id = target_obj['id'] for obj in mobile_objs: obj_id = obj['id'] gobj = GOBJ(obj_id,role=GOBJ.MOVEABLE) if obj['shape'] == "box": dynamic_body = world.CreateDynamicBody(position=obj['position'], userData=gobj) dynamic_body.CreatePolygonFixture(box=obj['size'],density=obj['density'], friction=obj['friction']) mass_center = dynamic_body.fixtures[0].massData.center vertices=[(dynamic_body.transform*v)for v in dynamic_body.fixtures[0].shape] #vertices=[(v[0], SCREEN_HEIGHT-v[1]) for v in vertices] gobj.setPosScr(vertices) gobj.setMassCenterSrc(dynamic_body.transform * mass_center) self.game_objects.append(gobj) for obj in manipulatable_objs: gobj = GOBJ(obj['id'],role=GOBJ.MAN) if obj['shape'] == "box": dynamic_body = world.CreateDynamicBody(position=obj['position'], userData=gobj) dynamic_body.CreatePolygonFixture(box=obj['size'],density=obj['density'], friction=obj['friction']) mass_center = dynamic_body.fixtures[0].massData.center vertices=[(dynamic_body.transform*v)for v in dynamic_body.fixtures[0].shape] #vertices=[(v[0], SCREEN_HEIGHT-v[1]) for v in vertices] gobj.setPosScr(vertices) gobj.setMassCenterSrc(dynamic_body.transform * mass_center) self.game_objects.append(gobj) '''