def __init__(self, b2world_interface, init_pos, base, init_angle, hand_shape='rectangle', hand_size=(0.3, 1)): world = b2world_interface.world self.hand = world.CreateDynamicBody(position=init_pos, angle=init_angle) self.hand_shape = hand_shape self.hand_size = hand_size # forceunit for circle and rect if hand_shape == 'rectangle': rshape = b2PolygonShape(box=hand_size) self.forceunit = 30.0 elif hand_shape == 'circle': rshape = b2CircleShape(radius=hand_size) self.forceunit = 100.0 elif hand_shape == 'polygon': rshape = b2PolygonShape(vertices=hand_size) else: raise Exception("%s is not a correct shape" % hand_shape) self.hand.CreateFixture( shape=rshape, density=.1, friction=.1 ) self.hand.userData = "hand" friction_joint = world.CreateFrictionJoint( bodyA=base, bodyB=self.hand, maxForce=2, maxTorque=2, ) b2world_interface.add_bodies(self.hand)
def replace_body(old_body, new_position, scale, shape='box', category_bits=0x0001, mask_bits=(0x0001 | 0x0002 | 0x0003)): if shape == 'box': new_shape = b2PolygonShape( box=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO, scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) elif shape == 'circle': new_shape = b2CircleShape( radius=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) else: new_shape = b2PolygonShape( box=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO, scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) new_body = defines.world.CreateDynamicBody( position=new_position, angle=old_body.angle, angularDamping=old_body.angularDamping, linearDamping=old_body.linearDamping, shapes=new_shape, bullet=old_body.bullet, shapeFixture=b2FixtureDef(density=2.0, categoryBits=category_bits, maskBits=mask_bits)) return new_body
def __init__(self): super(BoxCutter, self).__init__() # The ground self.ground = self.world.CreateStaticBody( userData='ground', shapes=[ b2EdgeShape(vertices=[(-50, 0), (50, 0)]), b2EdgeShape(vertices=[(-50, 0), (-50, 10)]), b2EdgeShape(vertices=[(50, 0), (50, 10)]), ] ) self.laser_body = self.world.CreateDynamicBody( userData='laser', position=(0, 2), fixtures=b2FixtureDef( density=4.0, shape=b2PolygonShape(box=(LASER_HALF_WIDTH, 1)) ) ) for i in range(2): self.world.CreateDynamicBody( userData=LASER_SPLIT_TAG, position=(3.0 + i * 6, 8), fixtures=b2FixtureDef( density=5.0, shape=b2PolygonShape(box=(3, 3)) ) )
def createWorld(self): self._isLiving = True self._auto = True self._pid_control = PIDControl(1, 0.05, 0.1) # kp, ki, kd self.ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-25, 0), (25, 0)]) ) self.carBody = self.world.CreateDynamicBody( position=(0, 3), fixtures=b2FixtureDef( shape=b2PolygonShape(box=(5, 1)), density=1) ) self.carLwheel = self.world.CreateDynamicBody( position=(-3, 1), fixtures=b2FixtureDef( shape=b2CircleShape(radius=1), density=2, friction=1) ) self.carRwheel = self.world.CreateDynamicBody( position=(3, 1), fixtures=b2FixtureDef( shape=b2CircleShape(radius=1), density=2, friction=1) ) self.pendulum = self.world.CreateDynamicBody( position=(0, 13), fixtures=b2FixtureDef( shape=b2PolygonShape(box=(0.5, 10)), density=1), ) self.pendelumJoin = self.world.CreateRevoluteJoint( bodyA=self.carBody, bodyB=self.pendulum, anchor=(0, 3), maxMotorTorque=1, enableMotor=True ) self.pendelumRJoin =self.world.CreateRevoluteJoint( bodyA=self.carBody, bodyB=self.carRwheel, anchor=(3, 1), maxMotorTorque=1, enableMotor=True, ) self.pendelumLJoin = self.world.CreateRevoluteJoint( bodyA=self.carBody, bodyB=self.carLwheel, anchor=(-3, 1), maxMotorTorque=1, enableMotor=True, )
def __init__(self): Framework.__init__(self) self.using_contacts = True ground = self.world.CreateStaticBody( shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])], ) # Platform self.platform = self.world.CreateStaticBody( position=(-5, 5), allowSleep=False, fixtures=b2FixtureDef(friction=0.8, shape=b2PolygonShape(box=(10.0, 5.0)),), ) self.platform_fixture = self.platform.fixtures[0] # Boxes for i in range(5): self.platform = self.world.CreateDynamicBody( position=(-10.0 + 2.0 * i, 7.0), fixtures=b2FixtureDef(density=20.0, shape=b2PolygonShape(box=(0.5, 0.5)),), )
def __init__(self): Framework.__init__(self) self.using_contacts = True ground = self.world.CreateStaticBody( shapes=[b2EdgeShape(vertices=[(-20, 0), (20, 0)])], ) # Platform self.platform = self.world.CreateStaticBody( position=(-5, 5), allowSleep=False, fixtures=b2FixtureDef( friction=0.8, shape=b2PolygonShape(box=(10.0, 5.0)), ), ) self.platform_fixture = self.platform.fixtures[0] # Boxes for i in range(5): self.platform = self.world.CreateDynamicBody( position=(-10.0 + 2.0 * i, 7.0), fixtures=b2FixtureDef( density=20.0, shape=b2PolygonShape(box=(0.5, 0.5)), ), )
def __init__(self): super(Raycast, self).__init__() self.world.gravity = (0, 0) # The ground ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])) # The various shapes w = 1.0 b = w / (2.0 + sqrt(2.0)) s = sqrt(2.0) * b self.shapes = [ b2PolygonShape(vertices=[(-0.5, 0), (0.5, 0), (0, 1.5)]), b2PolygonShape(vertices=[(-0.1, 0), (0.1, 0), (0, 1.5)]), b2PolygonShape( vertices=[ (0.5 * s, 0), (0.5 * w, b), (0.5 * w, b + s), (0.5 * s, w), (-0.5 * s, w), (-0.5 * w, b + s), (-0.5 * w, b), (-0.5 * s, 0.0), ] ), b2PolygonShape(box=(0.5, 0.5)), b2CircleShape(radius=0.5), ] self.angle = 0 self.callbacks = [RayCastClosestCallback, RayCastAnyCallback, RayCastMultipleCallback] self.callback_class = self.callbacks[0]
def create_body(base, b2world_interface, body_shape, body_size, body_friction, body_density, obj_loc): world = b2world_interface.world link = world.CreateDynamicBody(position=obj_loc) if body_shape == 'rectangle': linkshape = b2PolygonShape(box=body_size) elif body_shape == 'circle': linkshape = b2CircleShape(radius=body_size) elif body_shape == 'polygon': linkshape = b2PolygonShape(vertices=body_size) else: raise Exception("%s is not a correct shape" % body_shape) link.CreateFixture( shape=linkshape, density=body_density, friction=body_friction, ) friction_joint = world.CreateFrictionJoint( bodyA=base, bodyB=link, maxForce=5, maxTorque=2, ) b2world_interface.add_bodies([link]) return link
def __init__(self, robot_config, b2_world, ground): self.robot_config = robot_config self._robot_state = DefaultRobotState(self.robot_config) # adding the rods rod_fixture = b2FixtureDef( shape=b2PolygonShape(box=(0.5 * robot_config.rod_diameter, 0.5 * robot_config.rod_length)), density=robot_config.rod_density) rod_fixture.filter.groupIndex = -1 self.rods = [] y_pos = 0.5 * robot_config.rod_length for _ in range(2): b2_rod = b2_world.CreateDynamicBody( position=(robot_config.position, y_pos), fixtures=rod_fixture) self.rods.append(b2_rod) y_pos += robot_config.rod_length # adding the rackets racket_fixture = b2FixtureDef( shape=b2PolygonShape(box=(0.5 * robot_config.racket_diameter, 0.5 * robot_config.racket_thickness)), density=robot_config.racket_density, restitution=robot_config.racket_restitution) self.racket = b2_world.CreateDynamicBody( position=(robot_config.position, 2. * robot_config.rod_length + 0.5 * robot_config.racket_thickness), fixtures=racket_fixture) # adding joints self.joints = [None, None, None] # items : rods + racket dyn_items = self.rods + [self.racket] previous_dyn_item = ground previous_length = 0 for index, (robot_item, dyn_item) in enumerate(zip(robot_config.items, dyn_items)): self.joints[index] = b2_world.CreateRevoluteJoint( bodyA=previous_dyn_item, bodyB=dyn_item, anchor=(robot_config.position, previous_length), lowerAngle=-robot_item.joint_limit, upperAngle=+robot_item.joint_limit, enableLimit=True, enableMotor=True) previous_length = previous_length + robot_item.length previous_dyn_item = dyn_item
def addShape(self, world, shape): if self._body is not None: # shortcuts createShape = self._body.CreateFixturesFromShapes # add shapes for s in shape: if isinstance(s, Rect): createShape(b2PolygonShape(vertices=[s.topleft, s.bottomleft, s.bottomright, s.topright])) else: createShape(b2PolygonShape(vertices=s))
def __init__(self): Framework.__init__(self) ground = self.world.CreateStaticBody( position=(0,0), shapes=b2PolygonShape(box=(1000,1)), ) a=14.5 b=12.2 vertices=[] for i in range(16): ell_x=a*math.cos(22.5*i*math.pi/180) ell_y=b*math.sin(22.5*i*math.pi/180) vertices.append((ell_x,ell_y)) self.qross = self.world.CreateDynamicBody( position=(0, 20), allowSleep=True, fixtures=b2FixtureDef(density=2.0, friction=0.6, shape=b2PolygonShape(vertices=vertices),), ) self.ball = self.world.CreateStaticBody( fixtures=b2FixtureDef( shape=b2CircleShape(radius=2.2), density=1.0), bullet=True, position=(0, 1000)) leg = self.world.CreateDynamicBody( position=(0, 20), allowSleep=True, fixtures=b2FixtureDef(density=2.0, friction=0.0, shape=b2PolygonShape(vertices=[(20,1.3), (0,1.3), (0,0), (20,0)]),), ) self.world.CreateRevoluteJoint( bodyA=leg, bodyB=self.qross, anchor=(0, 20), ) self.joint = self.world.CreateMotorJoint(bodyA=leg, bodyB=self.qross, maxForce=0, maxTorque=500000) self.go = False self.time = 0.0
def __init__(self): super(BodyTypes, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)]) ) # The attachment self.attachment = self.world.CreateDynamicBody( position=(0, 3), fixtures=b2FixtureDef( shape=b2PolygonShape(box=(0.5, 2)), density=2.0), ) # The platform fixture = b2FixtureDef( shape=b2PolygonShape(box=(4, 0.5)), density=2, friction=0.6, ) self.platform = self.world.CreateDynamicBody( position=(0, 5), fixtures=fixture, ) # The joints joining the attachment/platform and ground/platform self.world.CreateRevoluteJoint( bodyA=self.attachment, bodyB=self.platform, anchor=(0, 5), maxMotorTorque=50, enableMotor=True ) self.world.CreatePrismaticJoint( bodyA=ground, bodyB=self.platform, anchor=(0, 5), axis=(1, 0), maxMotorForce=1000, enableMotor=True, lowerTranslation=-10, upperTranslation=10, enableLimit=True ) # And the payload that initially sits upon the platform # Reusing the fixture we previously defined above fixture.shape.box = (0.75, 0.75) self.payload = self.world.CreateDynamicBody( position=(0, 8), fixtures=fixture, )
def __init__(self): super(Tower, self).__init__() #Ground ground = self.world.CreateStaticBody( position=(0, 0), shapes=[b2EdgeShape(vertices=[(-30, 0), (30, 0)])] #, ## b2PolygonShape(box=(0.2, 5, (0.5, 5),0))] ) ## self.world.CreateStaticBody( ## shapes=[b2PolygonShape(box=[1, 1, (5, 15), 0]), ## b2PolygonShape(box=[1, 1, (-2, 12), 0]), ## ] ## ) self._x = 0 ## self.body = self.world.CreateDynamicBody( ## position=(0.5, 10), ## fixtures=b2FixtureDef( ## shape=b2PolygonShape(box=(4, 0.1)), density=1.0), ## ) # Small circle #circle = b2FixtureDef( #shape=b2CircleShape(radius=0.25), #density=100.0, #) # Generate bullet (circle) #self.bullet = self.world.CreateBody( #position=(self._x, 30), #bullet=True, #type=b2_dynamicBody, #fixtures=circle, #linearVelocity=(0, -50) #) self.stack1 = self.world.CreateDynamicBody( position=(self._x, 0), ## bullet=True, type=b2_dynamicBody, fixtures=b2FixtureDef(shape=b2PolygonShape(box=(5, 7)), density=100.0), linearVelocity=(0, -50)) self.stack2 = self.world.CreateDynamicBody( position=(self._x, 0), ## bullet=True, type=b2_dynamicBody, fixtures=b2FixtureDef(shape=b2PolygonShape(box=(4, 4)), density=100.0), linearVelocity=(0, -50))
def __init__(self): super(ApplyForce, self).__init__() self.world.gravity = (0.0, 0.0) # The boundaries ground = self.world.CreateBody(position=(0, 20)) ground.CreateEdgeChain( [(-20, -20), (-20, 20), (20, 20), (20, -20), (-20, -20)] ) # TODO: make note of transform.R.set() -> transform.angle = xf1 = b2Transform() xf1.angle = 0.3524 * b2_pi xf1.position = xf1.R * (1.0, 0.0) xf2 = b2Transform() xf2.angle = -0.3524 * b2_pi xf2.position = xf2.R * (-1.0, 0.0) self.body = self.world.CreateDynamicBody( position=(0, 2), angle=b2_pi, angularDamping=5, linearDamping=0.1, shapes=[b2PolygonShape(vertices=[xf1 * (-1, 0), xf1 * (1, 0), xf1 * (0, .5)]), b2PolygonShape(vertices=[xf2 * (-1, 0), xf2 * (1, 0), xf2 * (0, .5)])], shapeFixture=b2FixtureDef(density=2.0), ) gravity = 10.0 fixtures = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 0.5)), density=1, friction=0.3) for i in range(10): body = self.world.CreateDynamicBody( position=(0, 5 + 1.54 * i), fixtures=fixtures) # For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) r = sqrt(2.0 * body.inertia / body.mass) self.world.CreateFrictionJoint( bodyA=ground, bodyB=body, localAnchorA=(0, 0), localAnchorB=(0, 0), collideConnected=True, maxForce=body.mass * gravity, maxTorque=body.mass * r * gravity )
def create_jar(world, properties, scene_width, scene_height, shape, color, diameter, x, y, angle, isDynamic): # constants from PHYRE BASE_RATIO = Constant.JAR_BASE_RATIO WIDTH_RATIO = Constant.JAR_WIDTH_RATIO jar_height = scene_width * Constant._jar_diameter_to_default_scale( diameter) jar_width = jar_height * WIDTH_RATIO jar_base_width = jar_width * BASE_RATIO jar_thickness = Constant._jar_thickness_from_height( scene_width, jar_height) vertices_list, _ = Constant._build_jar_vertices(height=jar_height, width=jar_width, base_width=jar_base_width, thickness=jar_thickness) jar_center = _get_jar_center(scene_width, scene_height, x, y, angle, jar_height, jar_thickness) literal1_shape = b2PolygonShape() literal2_shape = b2PolygonShape() bottom_shape = b2PolygonShape() _set_vertices(literal1_shape, vertices_list[0]) _set_vertices(literal2_shape, vertices_list[1]) _set_vertices(bottom_shape, vertices_list[2]) literal1_fixture = b2FixtureDef(shape=literal1_shape, density=properties.densities["jar"], friction=properties.frictions["jar"], restitution=properties.restitutions["jar"]) literal2_fixture = b2FixtureDef(shape=literal2_shape, density=properties.densities["jar"], friction=properties.frictions["jar"], restitution=properties.restitutions["jar"]) bottom_fixture = b2FixtureDef(shape=bottom_shape, density=properties.densities["jar"], friction=properties.frictions["jar"], restitution=properties.restitutions["jar"]) if isDynamic: body = world.CreateDynamicBody( position=jar_center, fixtures=[literal1_fixture, literal2_fixture, bottom_fixture], angle=2 * b2_pi * angle) else: body = world.CreateStaticBody( position=jar_center, fixtures=[literal1_fixture, literal2_fixture, bottom_fixture], angle=2 * b2_pi * angle) return body
def CreateBody(self, world, TILE_SIZE): pos = b2Vec2(self.pos.x * TILE_SIZE, -self.pos.y * TILE_SIZE) dim = b2Vec2(self.dim.w * TILE_SIZE, self.dim.h * TILE_SIZE) bodyDef = b2BodyDef() bodyDef.type = b2_staticBody bodyDef.userData = self bodyDef.position = pos body = world.CreateBody(bodyDef) fixture = None if (self.name == "building"): dim.x /= 2.0 dim.y /= 2.0 pos.x += self.dim.w * TILE_SIZE / 2.0 pos.y -= self.dim.h * TILE_SIZE / 2.0 body.position = pos polygonShape = b2PolygonShape() polygonShape.SetAsBox(dim.x, dim.y) fixture = body.CreateFixture(shape=polygonShape) elif (self.name == "pickup"): circleShape = b2CircleShape() circleShape.radius = dim.x fixtureDef = b2FixtureDef() fixtureDef.shape = circleShape fixtureDef.isSensor = True fixture = body.CreateFixture(fixtureDef) self.done = False fixture.userData = self body.userData = self self.m_body = body return body
def __init__(self): super(EdgeShapes, self).__init__() self.ground = self.world.CreateStaticBody( shapes=[b2EdgeShape(vertices=v) for v in get_sinusoid_vertices(-20.0, VERTEX_COUNT)]) self.shapes = [ b2PolygonShape(vertices=[(-0.5, 0), (0.5, 0), (0, 1.5)]), b2PolygonShape(vertices=[(-0.1, 0), (0.1, 0), (0, 1.5)]), b2PolygonShape(vertices=get_octagon_vertices(1.0)), b2PolygonShape(box=(0.5, 0.5)), b2CircleShape(radius=0.5), ] self.angle = 0 self.callback = RayCastCallback()
def __init__(self): super(Distance, self).__init__() # Transform A -- a simple translation/offset of (0,-0.2) self.transformA = b2Transform() self.transformA.SetIdentity() self.transformA.position = (0, -0.2) # Transform B -- a translation and a rotation self.transformB = b2Transform() self.positionB = b2Vec2(12.017401, 0.13678508) self.angleB = -0.0109265 self.transformB.Set(self.positionB, self.angleB) # The two shapes, transformed by the respective transform[A,B] self.polygonA = b2PolygonShape(box=(10, 0.2)) self.polygonB = b2PolygonShape(box=(2, 0.1))
def __init__(self): super(EdgeTest, self).__init__() v1 = (-10.0, 0.0) v2 = (-7.0, -1.0) v3 = (-4.0, 0.0) v4 = (0.0, 0.0) v5 = (4.0, 0.0) v6 = (7.0, 1.0) v7 = (10.0, 0.0) shapes = [b2EdgeShape(vertices=[None, v1, v2, v3]), b2EdgeShape(vertices=[v1, v2, v3, v4]), b2EdgeShape(vertices=[v2, v3, v4, v5]), b2EdgeShape(vertices=[v3, v4, v5, v6]), b2EdgeShape(vertices=[v4, v5, v6, v7]), b2EdgeShape(vertices=[v5, v6, v7]) ] ground = self.world.CreateStaticBody(shapes=shapes) box = self.world.CreateDynamicBody( position=(0.5, 0.6), allowSleep=False, shapes=b2PolygonShape(box=(0.5, 0.5)) )
class TileMap(Entity): boxShape = b2PolygonShape() boxShape.SetAsBox(1, 1) boxFixtureDef = b2FixtureDef(shape=boxShape, density=1) def __init__(self, pos=(0, 0), size=(10, 10)): super().__init__() self.pos = pos #self.size = size raw = Image.open('q4_map.png') pixels = raw.load() self.size = raw.width, raw.height self.blocks = dict() for x in range(self.size[0]): for y in range(self.size[1]): if pixels[x, y][3] == 0: self.blocks[x, y] = None else: self.blocks[x, y] = SolidBlock(self, (x, y)) self.blocks[x, y].add() kitten = image.load('q4_map.png') self.texture = kitten.get_texture() def render(self): #glBindTexture(self.texture.target, self.texture.id) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) self.texture.blit(self.pos[0], self.pos[1])
def __init__(self): super(Spring, self).__init__() ground = self.world.CreateStaticBody( position=(0, 0), shapes=[b2EdgeShape(vertices=[(-50, 0), (50, 0)])], ) self.bodyA = self.world.CreateDynamicBody( position=(0, 10), fixtures=b2FixtureDef( shape=b2PolygonShape(box=(1, 1)), density=1.0) ) self.bodyB = self.world.CreateDynamicBody( position=(0, 5), fixtures=b2FixtureDef( shape=b2CircleShape(radius = 0.5), density=1.0, restitution=0) ) dfn = b2DistanceJointDef( frequencyHz=2.5, dampingRatio=0, bodyA=self.bodyA, bodyB=self.bodyB, anchorA=(0, 10), anchorB=(0, 5), collideConnected=True ) self.world.CreateJoint(dfn)
def create_bar(world, properties, scene_width, scene_height, shape, color, diameter, x, y, angle, isDynamic): # constant from PHYRE BAR_HEIGHT = scene_width * Constant.BAR_HEIGHT_RATIO center = (Transform.width_percent_to_x(scene_width, x), Transform.height_percent_to_y(scene_height, y)) bar_shape = b2PolygonShape( box=(Transform.diameter_percent_to_length(scene_width, diameter) / 2, BAR_HEIGHT / 2)) bar_fixture = b2FixtureDef(shape=bar_shape, density=properties.densities["bar"], friction=properties.frictions["bar"], restitution=properties.restitutions["bar"]) if isDynamic: body = world.CreateDynamicBody(position=center, fixtures=bar_fixture, angle=2 * b2_pi * angle) else: body = world.CreateStaticBody(position=center, fixtures=bar_fixture, angle=2 * b2_pi * angle) return body
def add_node(self, parent, local_anchor, depth, offset, a): density = 20.0 h = (0, a) p = parent.position + local_anchor - h fixture = b2FixtureDef(shape=b2PolygonShape(box=(0.25 * a, a)), density=density) body = self.world.CreateDynamicBody(position=p, fixtures=fixture) if depth == self.max_depth: return body a1 = (offset, -a) a2 = (-offset, -a) body1 = self.add_node(body, a1, depth + 1, 0.5 * offset, a) body2 = self.add_node(body, a2, depth + 1, 0.5 * offset, a) self.world.CreateRevoluteJoint(bodyA=body, bodyB=body1, localAnchorA=a1, localAnchorB=h) self.world.CreateRevoluteJoint(bodyA=body, bodyB=body2, localAnchorA=a2, localAnchorB=h) return body
def __init__(self): super(OneSidedPlatform, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)]) ) # The platform half_height = 0.5 ypos = 10 body = self.world.CreateBody( position=(0, ypos), shapes=b2PolygonShape(box=(3, half_height)) ) self.platform = body.fixtures[0] # The circular character self.character_radius = 0.5 body = self.world.CreateDynamicBody( position=(0, 12), fixtures=b2FixtureDef(shape=b2CircleShape( radius=self.character_radius), density=1.0), ) self.character = body.fixtures[0] body.linearVelocity = (0, -50) self.bottom = ypos - half_height # The bottom of the platform self.top = ypos + half_height # The top of the platform self.state = 'unknown'
def add_agent(self, y, x): self.agent_init_position = (x, y) agent_vertices = [(0.5 - self.agent_radius, 0.5 - self.agent_radius), (0.5 - self.agent_radius, 0.5 + self.agent_radius), (0.5 + self.agent_radius, 0.5 + self.agent_radius), (0.5 + self.agent_radius, 0.5 - self.agent_radius)] agent_fixture = b2FixtureDef( shape=b2PolygonShape(vertices=agent_vertices), friction=0.4, restitution=0.3, ) self.agent_body = self.world.CreateDynamicBody( position=(x, y), fixtures=agent_fixture, fixedRotation=True, ) visual_radius = 1.5 * self.agent_radius agent_vertices_visual = [(0.5 - visual_radius, 0.5 - visual_radius), (0.5 - visual_radius, 0.5 + visual_radius), (0.5 + visual_radius, 0.5 + visual_radius), (0.5 + visual_radius, 0.5 - visual_radius)] drawing_util.add_polygon(self.display_objects, self.agent_body, agent_vertices_visual, 'agent', drawing_layer=5, color=self.agent_color) self.position_indicator = drawing_util.DummyBody(b2Vec2(x, y), angle=0) self.target_indicator = drawing_util.DummyBody(b2Vec2(x, y), angle=0)
def __init__(self): super(Pyramid, self).__init__() # The ground ground = self.world.CreateStaticBody(shapes=b2EdgeShape( vertices=[(-40, 0), (40, 0)])) box_half_size = (0.5, 0.5) box_density = 5.0 box_rows = 20 x = b2Vec2(-7, 0.75) deltaX = (0.5625, 1.25) deltaY = (1.125, 0) for i in range(box_rows): y = x.copy() for j in range(i, box_rows): self.world.CreateDynamicBody( position=y, fixtures=b2FixtureDef( shape=b2PolygonShape(box=box_half_size), density=box_density)) y += deltaY x += deltaX
def __init__(self): super(Pyramid, self).__init__() # The ground ground = self.world.CreateStaticBody( shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]) ) box_half_size = (0.5, 0.5) box_density = 5.0 box_rows = 20 x = b2Vec2(-7, 0.75) deltaX = (0.5625, 1.25) deltaY = (1.125, 0) for i in range(box_rows): y = x.copy() for j in range(i, box_rows): self.world.CreateDynamicBody( position=y, fixtures=b2FixtureDef( shape=b2PolygonShape(box=box_half_size), density=box_density) ) y += deltaY x += deltaX
def __init__(self, position, world, gravity): self.mWorld = world self.mGravity = gravity pos = b2Vec2(position[0] + self.BOX_WIDTH/2, position[1] + self.BOX_HEIGHT/2) #create box physicsbody body = world.CreateDynamicBody(position = pos) shape = b2PolygonShape() fd = b2FixtureDef() fd.shape = shape fd.isSensor = True #gravitysensor shape.SetAsBox(0.1,0.1) fd.userData = Sensor.GRAVITYZONESENSOR body.CreateFixture(fd) #collisionbody body.CreatePolygonFixture(box=(self.BOX_WIDTH/2, self.BOX_HEIGHT/2), density=6, friction=0) body.fixedRotation = True body.bullet = True body.allowSleep = False body.mass = 2 body.userData = self super(Box, self).__init__(pos, b2Vec2(self.BOX_WIDTH, self.BOX_HEIGHT), body, b2Vec2(0,0), 0, b2Vec2(0,0))
def __init__(self, im, x, y): im_w2 = im.width // 2 im_h2 = im.height // 2 coll_shape = b2PolygonShape(box=(im_w2, im_h2)) super(Actor1, self).__init__(sf.Sprite(im), shape=coll_shape) self.object.origin = (im_w2, im_h2) self.object.position = (x, y)
def create_creature_bodies(world, creatures): ''' Creates a list of creature bodies and returns reference bodies ''' reference_body = {} for creature in creatures: body = {} for edge in creature.edges: vertex = creature.vertices[edge[0]], creature.vertices[edge[1]] point = line_to_rectangle(*vertex, THICKNESS) fixture = b2FixtureDef( shape=b2PolygonShape(vertices=point), density=DENSITY, friction=FRICTION, ) fixture.filter.groupIndex = -1 body[tuple(edge)] = world.CreateDynamicBody( fixtures=fixture, ) for edge in creature.edges: adjacent = find_adjacent_edges(edge, creature.edges) for a_edge in adjacent: anchor = int(creature.vertices[edge[0]][0]), int(creature.vertices[edge[0]][1]) world.CreateRevoluteJoint( bodyA=body[tuple(edge)], bodyB=body[tuple(a_edge)], anchor=anchor, collideConnected=True, motorSpeed=MOTOR_SPEED, maxMotorTorque=MAX_MOTOR_TORQUE, enableMotor=True, ) reference_body[creature.identity] = body[tuple(edge)] return reference_body
def __init__(self): super(VerticalStack, self).__init__() columns = 5 rows = 16 ground = self.world.CreateStaticBody(shapes=[ b2EdgeShape(vertices=[(-40, 0), (40, 0)]), b2EdgeShape(vertices=[(20, 0), (20, 20)]), ]) box = b2FixtureDef(shape=b2PolygonShape(box=(0.5, 0.5)), density=1, friction=0.3) circle = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1, friction=0.3) box_start = -10 box_space = 2.5 circle_start = 8 circle_space = 2.5 for j in range(columns): for i in range(rows): self.world.CreateDynamicBody( fixtures=box, position=(box_start + box_space * j, 0.752 + 1.54 * i)) self.world.CreateDynamicBody( fixtures=circle, position=(circle_start + circle_space * j, 0.752 + 1.54 * i))
def __init__(self): super(Bridge, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)]) ) create_bridge(self.world, ground, (1.0, 0.25), (-14.5, 5), self.numPlanks, 0.2, 20) fixture = b2FixtureDef( shape=b2PolygonShape(vertices=[(-0.5, 0.0), (0.5, 0.0), (0.0, 1.5), ]), density=1.0 ) for i in range(2): self.world.CreateDynamicBody( position=(-8 + 8 * i, 12), fixtures=fixture, ) fixture = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1) for i in range(3): self.world.CreateDynamicBody( position=(-6 + 6 * i, 10), fixtures=fixture, )
def __init__(self): super(Bridge, self).__init__() # The ground ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])) create_bridge(self.world, ground, (1.0, 0.25), (-14.5, 5), self.numPlanks, 0.2, 20) fixture = b2FixtureDef(shape=b2PolygonShape(vertices=[ (-0.5, 0.0), (0.5, 0.0), (0.0, 1.5), ]), density=1.0) for i in range(2): self.world.CreateDynamicBody( position=(-8 + 8 * i, 12), fixtures=fixture, ) fixture = b2FixtureDef(shape=b2CircleShape(radius=0.5), density=1) for i in range(3): self.world.CreateDynamicBody( position=(-6 + 6 * i, 10), fixtures=fixture, )
def _reset(self): self._destroy() membrane_base.reset_helper(self) # Creating the object to manipulate object_fixture = b2FixtureDef(shape=b2PolygonShape(box=(OBJ_SIZE / 2, OBJ_SIZE / 2)), density=1, friction=0.6, restitution=0.0) object_position = (membrane_base.BOX_WIDTH / 2, OBJ_SIZE / 2 + membrane_base.LINK_HEIGHT) self.object = self.world.CreateDynamicBody( position=object_position, fixtures=object_fixture, linearDamping=0.6 # Control this parameter for surface friction ) self.object.color1 = (1, 1, 0) self.object.color2 = (0, 0, 0) self.drawlist = self.drawlist + [self.object] return self._step(np.array([0, 0, 0, 0, 0]))[0] # action: zero motor speed
def __init__(self, im, x, y): im_w2 = im.width//2 im_h2 = im.height//2 coll_shape = b2PolygonShape(box=(im_w2, im_h2)) super(Actor1, self).__init__(sf.Sprite(im), shape=coll_shape) self.object.origin = (im_w2, im_h2) self.object.position = (x, y)
def __init__(self): super(EdgeShapes, self).__init__() self.ground = self.world.CreateStaticBody( shapes=[b2EdgeShape(vertices=v) for v in get_sinusoid_vertices(-20.0, VERTEX_COUNT)] ) self.shapes = [ b2PolygonShape(vertices=[(-0.5, 0), (0.5, 0), (0, 1.5)]), b2PolygonShape(vertices=[(-0.1, 0), (0.1, 0), (0, 1.5)]), b2PolygonShape(vertices=get_octagon_vertices(1.0)), b2PolygonShape(box=(0.5, 0.5)), b2CircleShape(radius=0.5), ] self.angle = 0 self.callback = RayCastCallback()
def __init__(self): super(OneSidedPlatform, self).__init__() # The ground ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)])) # The platform half_height = 0.5 ypos = 10 body = self.world.CreateBody(position=(0, ypos), shapes=b2PolygonShape(box=(3, half_height))) self.platform = body.fixtures[0] # The circular character self.character_radius = 0.5 body = self.world.CreateDynamicBody( position=(0, 12), fixtures=b2FixtureDef( shape=b2CircleShape(radius=self.character_radius), density=1.0), ) self.character = body.fixtures[0] body.linearVelocity = (0, -50) self.bottom = ypos - half_height # The bottom of the platform self.top = ypos + half_height # The top of the platform self.state = 'unknown'
def create_car(world, offset, wheel_radius, wheel_separation, density=1.0, wheel_friction=0.9, scale=(1.0, 1.0), chassis_vertices=None, wheel_axis=(0.0, 1.0), wheel_torques=[20.0, 10.0], wheel_drives=[True, False], hz=4.0, zeta=0.7, **kwargs): """ """ x_offset, y_offset = offset scale_x, scale_y = scale if chassis_vertices is None: chassis_vertices = [ (-1.5, -0.5), (1.5, -0.5), (1.5, 0.0), (0.0, 0.9), (-1.15, 0.9), (-1.5, 0.2), ] chassis_vertices = [(scale_x * x, scale_y * y) for x, y in chassis_vertices] radius_scale = sqrt(scale_x ** 2 + scale_y ** 2) wheel_radius *= radius_scale chassis = world.CreateDynamicBody( position=(x_offset, y_offset), fixtures=b2FixtureDef( shape=b2PolygonShape(vertices=chassis_vertices), density=density, ) ) wheels, springs = [], [] wheel_xs = [-wheel_separation * scale_x / 2.0, wheel_separation * scale_x / 2.0] for x, torque, drive in zip(wheel_xs, wheel_torques, wheel_drives): wheel = world.CreateDynamicBody( position=(x_offset + x, y_offset - wheel_radius), fixtures=b2FixtureDef( shape=b2CircleShape(radius=wheel_radius), density=density, ) ) spring = world.CreateWheelJoint( bodyA=chassis, bodyB=wheel, anchor=wheel.position, axis=wheel_axis, motorSpeed=0.0, maxMotorTorque=torque, enableMotor=drive, frequencyHz=hz, dampingRatio=zeta ) wheels.append(wheel) springs.append(spring) return chassis, wheels, springs
def __init__(self): super(Breakable, self).__init__() # The ground self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])) # The breakable body self.shapes = (b2PolygonShape(box=(0.5, 0.5, (-0.5, 0), 0)), b2PolygonShape(box=(0.5, 0.5, (0.5, 0), 0))) self.body = self.world.CreateDynamicBody( position=(0, 40), angle=0.25 * b2_pi, shapes=self.shapes, shapeFixture=b2FixtureDef(density=1), ) self.fixtures = self.body.fixtures
def __init__(self,new_display): self.b2_game_world = b2World() # default gravity is (0,-10) and doSleep is True self.groundBody = self.b2_game_world.CreateStaticBody(position=(0, -48), shapes=b2PolygonShape(box=(80, 10)), ) # Create a dynamic body at (0, 4) self.game_display = new_display
def make_edges(world): '''Create static bodies to represent the edges of the table.''' positions = [(TABLE_WIDTH / 2.0, EDGE_WIDTH), (EDGE_WIDTH,TABLE_HEIGHT / 2), (TABLE_WIDTH - EDGE_WIDTH,TABLE_HEIGHT / 2), (TABLE_WIDTH / 2.0, TABLE_HEIGHT - EDGE_WIDTH)] dimensions = [(TABLE_WIDTH / 2,EDGE_WIDTH), (EDGE_WIDTH, TABLE_HEIGHT / 2), (EDGE_WIDTH, TABLE_HEIGHT / 2), (TABLE_WIDTH / 2, EDGE_WIDTH)] edge_color = (150, 111, 51) edges = [world.CreateStaticBody(position=pos, shapes=b2PolygonShape(box=dim), restitution=RESTITUTION, userData=edge_color) for pos, dim in zip(positions, dimensions)] for edge in edges: edge.fixtures[0].friction = FRICTION return edges
def __init__(self): super(Breakable, self).__init__() # The ground self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])) # The breakable body self.shapes = (b2PolygonShape(box=(0.5, 0.5, (-0.5, 0), 0)), b2PolygonShape(box=(0.5, 0.5, (0.5, 0), 0)) ) self.body = self.world.CreateDynamicBody( position=(0, 40), angle=0.25 * b2_pi, shapes=self.shapes, shapeFixture=b2FixtureDef(density=1), ) self.fixtures = self.body.fixtures
def __init__(self, world): self.posx = 0 self.posy = 0 self.width = 50 self.height = 50 self.color = (255, 0, 0) self.shape = b2PolygonShape(box=(0.5, 0.125)) self.shape.box = (1.5, 1.5) self.body = world.CreateDynamicBody(position=(self.posx, self.posy))
def __init__(self): super(Pinball, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2LoopShape(vertices=[(0, -2), (8, 6), (8, 20), (-8, 20), (-8, 6)]), ) # Flippers p1, p2 = (-2, 0), (2, 0) fixture = b2FixtureDef(shape=b2PolygonShape(box=(1.75, 0.1)), density=1) flipper = {'fixtures': fixture} self.leftFlipper = self.world.CreateDynamicBody( position=p1, **flipper ) self.rightFlipper = self.world.CreateDynamicBody( position=p2, **flipper ) rjd = b2RevoluteJointDef( bodyA=ground, bodyB=self.leftFlipper, localAnchorA=p1, localAnchorB=(0, 0), enableMotor=True, enableLimit=True, maxMotorTorque=1000, motorSpeed=0, lowerAngle=-30.0 * b2_pi / 180.0, upperAngle=5.0 * b2_pi / 180.0, ) self.leftJoint = self.world.CreateJoint(rjd) rjd.motorSpeed = 0 rjd.localAnchorA = p2 rjd.bodyB = self.rightFlipper rjd.lowerAngle = -5.0 * b2_pi / 180.0 rjd.upperAngle = 30.0 * b2_pi / 180.0 self.rightJoint = self.world.CreateJoint(rjd) # Ball self.ball = self.world.CreateDynamicBody( fixtures=b2FixtureDef( shape=b2CircleShape(radius=0.2), density=1.0), bullet=True, position=(1, 15)) self.pressed = False
def __init__(self): super(Rope, self).__init__() # The ground ground = self.world.CreateBody( shapes=b2EdgeShape(vertices=[(-40, 0), (40, 0)])) shape = b2PolygonShape(box=(0.5, 0.125)) fd = b2FixtureDef( shape=shape, friction=0.2, density=20, categoryBits=0x0001, maskBits=(0xFFFF & ~0x0002), ) N = 10 y = 15.0 prevBody = ground for i in range(N): if i < N - 1: body = self.world.CreateDynamicBody( position=(0.5 + i, y), fixtures=fd, ) else: shape.box = (1.5, 1.5) fd.density = 100 fd.categoryBits = 0x0002 body = self.world.CreateDynamicBody( position=(i, y), fixtures=fd, angularDamping=0.4, ) self.world.CreateRevoluteJoint( bodyA=prevBody, bodyB=body, anchor=(i, y), collideConnected=False, ) prevBody = body extraLength = 0.01 self.rd = rd = b2RopeJointDef( bodyA=ground, bodyB=body, maxLength=N - 1.0 + extraLength, localAnchorA=(0, y), localAnchorB=(0, 0) ) self.rope = self.world.CreateJoint(rd)
def __init__(self, world): self.posx = 0 self.posy = 0 self.width = 50 self.height = 50 self.color = (255,0,0) self.shape = b2PolygonShape(box=(0.5, 0.125)) self.shape.box = (1.5, 1.5) self.body = world.CreateDynamicBody( position = (self.posx, self.posy) )
def __init__(self): super(Tiles, self).__init__() a = 0.5 def ground_positions(): N = 200 M = 10 position = b2Vec2(0, 0) for i in range(M): position.x = -N * a for j in range(N): yield position position.x += 2.0 * a position.y -= 2.0 * a ground = self.world.CreateStaticBody( position=(0, -a), shapes=[b2PolygonShape(box=(a, a, position, 0)) for position in ground_positions()] ) count = 20 def dynamic_positions(): x = b2Vec2(-7.0, 0.75) deltaX = (0.5625, 1.25) deltaY = (1.125, 0.0) for i in range(count): y = x.copy() for j in range(i, count): yield y y += deltaY x += deltaX for pos in dynamic_positions(): self.world.CreateDynamicBody( position=pos, fixtures=b2FixtureDef( shape=b2PolygonShape(box=(a, a)), density=5) )
def make_pockets(world): positions = [(EDGE_WIDTH * 2, EDGE_WIDTH * 2), (TABLE_WIDTH - EDGE_WIDTH * 2, EDGE_WIDTH * 2), (TABLE_WIDTH - EDGE_WIDTH * 2, TABLE_HEIGHT - EDGE_WIDTH * 2), (EDGE_WIDTH * 2, TABLE_HEIGHT - EDGE_WIDTH * 2)] num_vertices = 12 offsets = [0.0, 90.0, 180.0, 270.0] vertices = [make_semicircle(num_vertices, offset) for offset in offsets] shapes = [b2PolygonShape(vertices=verts) for verts in vertices] pocket_color = (5,5,5) pockets = [world.CreateStaticBody(position=pos, shapes=shape, restitution=RESTITUTION, userData=pocket_color) for pos, shape in zip(positions, shapes)] return pockets
def __init__(self): Framework.__init__(self) ground = self.world.CreateBody() body = self.world.CreateDynamicBody( position=(0, 10), allowSleep=False, shapeFixture=b2FixtureDef(density=5.0), shapes=[ b2PolygonShape(box=(0.5, 10, (10, 0), 0)), b2PolygonShape(box=(0.5, 10, (-10, 0), 0)), b2PolygonShape(box=(10, 0.5, (0, 10), 0)), b2PolygonShape(box=(10, 0.5, (0, -10), 0)), ] ) self.joint = self.world.CreateRevoluteJoint(bodyA=ground, bodyB=body, localAnchorA=(0, 10), localAnchorB=(0, 0), referenceAngle=0, motorSpeed=0.05 * b2_pi, enableMotor=True, maxMotorTorque=1.0e8)
def __init__(self): super(Liquid, self).__init__() self.per_particle_mass = self.total_mass / self.num_particles ground = self.world.CreateStaticBody( shapes=[ b2PolygonShape(box=[5.0, 0.5]), b2PolygonShape(box=[1.0, 0.2, (0, 4), -0.2]), b2PolygonShape(box=[1.5, 0.2, (-1.2, 5.2), -1.5]), b2PolygonShape(box=[0.5, 50.0, (5, 0), 0.0]), b2PolygonShape(box=[0.5, 3.0, (-8, 0), 0.0]), b2PolygonShape(box=[2.0, 0.1, (-6, -2.8), 0.1]), b2CircleShape(radius=0.5, pos=(-.5, -4)), ] ) cx = 0 cy = 25 box_width = 2.0 box_height = 20.0 self.liquid = [] for i in range(self.num_particles): self.createDroplet((b2Random(cx - box_width * 0.5, cx + box_width * 0.5), b2Random(cy - box_height * 0.5, cy + box_height * 0.5))) self.createBoxSurfer() if hasattr(self, 'settings'): self.settings.enableSubStepping = False
def Step(self, settings): Framework.Step(self, settings) self.count -= 1 if self.count == 0: return self.world.CreateDynamicBody( position=(0, 10), allowSleep=False, fixtures=b2FixtureDef( density=1.0, shape=b2PolygonShape(box=(0.125, 0.125))), )
def replace_body(old_body, new_position, scale, shape='box', category_bits=0x0001, mask_bits=(0x0001 | 0x0002 | 0x0003)): if shape == 'box': new_shape = b2PolygonShape(box=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO, scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) elif shape == 'circle': new_shape= b2CircleShape(radius=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) else: new_shape= b2PolygonShape(box=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO, scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) new_body = defines.world.CreateDynamicBody( position=new_position, angle=old_body.angle, angularDamping=old_body.angularDamping, linearDamping=old_body.linearDamping, shapes=new_shape, bullet=old_body.bullet, shapeFixture=b2FixtureDef(density=2.0, categoryBits=category_bits, maskBits=mask_bits ) ) return new_body
def __init__(self): super(Bullet, self).__init__() ground = self.world.CreateStaticBody( position=(0, 0), shapes=[b2EdgeShape(vertices=[(-10, 0), (10, 0)]), b2PolygonShape(box=(0.2, 1, (0.5, 1), 0))] ) self._x = 0.20352793 self.body = self.world.CreateDynamicBody( position=(0, 4), fixtures=b2FixtureDef( shape=b2PolygonShape(box=(2, 0.1)), density=1.0), ) self.bullet = self.world.CreateDynamicBody( position=(self._x, 10), bullet=True, fixtures=b2FixtureDef(shape=b2PolygonShape( box=(0.25, 0.25)), density=100.0), linearVelocity=(0, -50) )
def __init__(self): super(BodyTypes, self).__init__() # The ground ground = self.world.CreateBody(shapes=b2EdgeShape(vertices=[(-20, 0), (20, 0)])) # The attachment self.attachment = self.world.CreateDynamicBody( position=(0, 3), fixtures=b2FixtureDef(shape=b2PolygonShape(box=(0.5, 2)), density=2.0) ) # The platform fixture = b2FixtureDef(shape=b2PolygonShape(box=(4, 0.5)), density=2, friction=0.6) self.platform = self.world.CreateDynamicBody(position=(0, 5), fixtures=fixture) # The joints joining the attachment/platform and ground/platform self.world.CreateRevoluteJoint( bodyA=self.attachment, bodyB=self.platform, anchor=(0, 5), maxMotorTorque=50, enableMotor=True ) self.world.CreatePrismaticJoint( bodyA=ground, bodyB=self.platform, anchor=(0, 5), axis=(1, 0), maxMotorForce=1000, enableMotor=True, lowerTranslation=-10, upperTranslation=10, enableLimit=True, ) # And the payload that initially sits upon the platform # Reusing the fixture we previously defined above fixture.shape.box = (0.75, 0.75) self.payload = self.world.CreateDynamicBody(position=(0, 8), fixtures=fixture)
def new_physic_object(shape='box', scale=1, is_a_bullet=False, pos=(0, 0), angular_dampning=0, linear_dampning=0, category_bits=0x0001, mask_bits=(0x0001 | 0x0002 | 0x0003), den=2.0): debug_graphic = loader.loadModel("models/physic_square") debug_graphic.reparentTo(camera) if shape == 'circle': tex = loader.loadTexture("textures/round.png") else: tex = loader.loadTexture("textures/physic_texture.png") debug_graphic.setTexture(tex, 1) debug_graphic.setBin("unsorted", 0) debug_graphic.setDepthTest(False) if shape == 'box': new_shape = b2PolygonShape(box=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO, scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) elif shape == 'circle': new_shape= b2CircleShape(radius=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) else: new_shape = b2PolygonShape(box=(scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO, scale * defines.GRAPHIC_TO_PHYSIC_SCALE_RATIO)) body = defines.world.CreateDynamicBody( position=pos, angle=0, angularDamping=angular_dampning, linearDamping=linear_dampning, shapes=new_shape, bullet=is_a_bullet, shapeFixture=b2FixtureDef(density=den, categoryBits=category_bits, maskBits=mask_bits) ) return debug_graphic, body
def __init__(self, physworld, gravity, size, lifetime, pos): self.mWorld = physworld self.mGravity = gravity self.mSize = size self.mLifetime = lifetime self.mIsAlive = True #create body self.mBody = self.mWorld.CreateDynamicBody(position = pos) shape = b2PolygonShape() shape.SetAsBox(self.mSize.x/2.0,self.mSize.y/2.0) fd = b2FixtureDef() fd.shape = shape fd.categoryBits = Filter.CATEGORY_FX fd.maskBits = Filter.MASK_FX self.mBody.CreateFixture(fd) self.mBody.userData = self
def __init__(self, boxes, boxes_density, boxes_restitution, boxes_friction, boxes_pos, boxes_angle, grip_strength, gravity = (0, -9.81), timestep = 1.0/10, vel_iters = 10, pos_iters = 8): # init box2d world self.world = b2World(gravity = gravity, doSleep = True) # gripper max force self.grip_strength = grip_strength # set simulation parameters self.timestep = timestep self.vel_iters = vel_iters self.pos_iters = pos_iters # initialize all the boxes self.box_bodies = [] self.box_fixtures = [] for b, d, r, f, p, a in zip(boxes, boxes_density, boxes_restitution, boxes_friction, boxes_pos, boxes_angle): self.box_bodies.append(self.world.CreateDynamicBody(position = p, angle = a)) fixture = self.box_bodies[-1].CreatePolygonFixture(box=b, density = d, friction = f, restitution = r) self.box_fixtures.append(fixture) # initialize the floor self.floor_body = self.world.CreateStaticBody( position = (0,0), shape = b2PolygonShape(box = (50,2)) )