def setup_world(): world_bounds = b2AABB() world_bounds.lowerBound = (-200, -1000) world_bounds.upperBound = (200, 200) world = b2World( world_bounds, b2Vec2(0, -30), # Gravity vector True # Use "sleep" optimisation ) wallsdef = b2BodyDef() walls = world.CreateBody(wallsdef) walls.userData = 'Blocks' WALLS = [ (W, FLOOR * 0.5, (W / 2, FLOOR * 0.5), 0), # floor (W / 2, 1, (W / 2, H + 1), 0), # ceiling (1, 600, (-1, -500), 0), # left wall (1, 600, (W + 1, -500), 0), # right wall ] for wall in WALLS: shape = b2PolygonDef() shape.SetAsBox(*wall) walls.CreateShape(shape) return world
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 create_body(self, position, *shapes): body_def = b2BodyDef() body_def.position = position body = self.world.CreateBody(body_def) for shape in shapes: body.CreateShape(shape.shape) if shape.shape.density is not None: body.SetMassFromShapes() self.render_list.append((body, shapes)) return body
def append_to_world(self, world): width, height = self._size x, y = self._position box = b2PolygonDef() hitbox_width = (self._hitbox.left * width / 100) * 2 hitbox_height = (self._hitbox.top * height / 100) * 2 box.SetAsBox((width / 2) - hitbox_width, (height / 2) - hitbox_height) body_def = b2BodyDef() body_def.position.Set(x, y) self._body = world.CreateBody(body_def) self._body.CreateShape(box)
def create_body(self, pos): if self.SHAPEDEF is None: return bodydef = b2BodyDef() bodydef.position = b2Vec2(*pos) body = world.CreateBody(bodydef) body.CreateShape(self.SHAPEDEF) body.SetBullet(self.BULLET) body.SetMassFromShapes() self.body = body body.userData = self
def create_body(self, pos): bodydef = b2BodyDef() bodydef.position = b2Vec2(*pos) body = world.CreateBody(bodydef) shape = b2PolygonDef() shape.SetAsBox(self.W * 0.5, self.H * 0.5, (0, 0), 0) shape.density = 0.5 shape.restitution = 0.1 shape.friction = 0.5 body.CreateShape(shape) body.SetMassFromShapes() self.body = body
def create_body(world: b2World, body_type, fixture_def, position=b2Vec2(0, 0)) -> b2Body: body_def = b2BodyDef() body_def.type = body_type body_def.position = position body = world.CreateBody(body_def) body.CreateFixture(fixture_def) return body
def setup_ball(): bodydef = b2BodyDef() bodydef.position = b2Vec2(W * 0.33, H * 0.7) body = world.CreateBody(bodydef) cdef = b2CircleDef() cdef.radius = BOX_SIZE cdef.density = 0.01 cdef.restitution = 0.3 cdef.friction = 0.3 body.CreateShape(cdef) body.SetMassFromShapes() return body
def create_body(self, pos): bodydef = b2BodyDef() bodydef.position = b2Vec2(*pos) body = world.CreateBody(bodydef) cdef = b2CircleDef() cdef.radius = self.RADIUS cdef.density = 1.0 cdef.restitution = 0.1 cdef.friction = 0.5 body.CreateShape(cdef) self.body = body body.SetBullet(True) body.SetMassFromShapes()
def create_block(pos): bodydef = b2BodyDef() bodydef.position = pos body = world.CreateBody(bodydef) shape = b2PolygonDef() shape.SetAsBox(BOX_SIZE, BOX_SIZE, (0, 0), 0) shape.density = 0.1 shape.restitution = 0.2 shape.friction = 0.5 body.CreateShape(shape) body.SetMassFromShapes() return body
def setup_bear(): bodydef = b2BodyDef() bodydef.position = (W * 0.66, H * 0.7) body = world.CreateBody(bodydef) shape = b2PolygonDef() shape.SetAsBox(BOX_SIZE, BOX_SIZE, (0, 0), 0) shape.density = 0.3 shape.restitution = 0.5 shape.friction = 0.5 body.CreateShape(shape) body.SetMassFromShapes() return body
def create_body(self, pos): if not self.SHAPEDEFS: return bodydef = b2BodyDef() bodydef.position = b2Vec2(*pos) body = world.CreateBody(bodydef) for shape in self.SHAPEDEFS: body.CreateShape(shape) body.SetBullet(self.BULLET) body.SetMassFromShapes() self.body = body #buoyancy.AddBody(body) body.userData = self
def create_body(self, pos): bodydef = b2BodyDef() bodydef.position = b2Vec2(*pos) body = world.CreateBody(bodydef) cdef = b2CircleDef() cdef.radius = self.RADIUS cdef.density = 1.0 #вес тела cdef.restitution = 0.1 # прыгучесть, отскок cdef.friction = 0.5 # сила трения body.CreateShape(cdef) self.body = body body.SetBullet(True) body.SetMassFromShapes() self.body.userData = self
def setupWorld(self): # circle shape shape = b2CircleShape(radius=50 / settings.B2D_PPM) # fixture fixture = b2FixtureDef() fixture.density = 1 fixture.friction = 0.3 fixture.shape = shape #fixture.userData = new UserDataInfo(name, bRadius * 2, bRadius * 2) # body definition bodyDef = b2BodyDef() bodyDef.position.Set((300) / settings.B2D_PPM, (300) / settings.B2D_PPM) bodyDef.type = b2_dynamicBody bodyDef.fixedRotation = False circle = self.addActor(ActorB2D((300, 300), (50, 50)), bodyDef=bodyDef, fixture=fixture) box = self.addActor(ActorB2D((100, 400), (40, 20)), False) #j = self.m_b2dWorld.CreateRevoluteJoint(bodyA=box.m_body, # bodyB=circle.m_body, # localAnchorA=(0, 5), # localAnchorB=(0, 0), # enableMotor=False, # maxMotorTorque=1000, # enableLimit=True, # lowerAngle=0, # upperAngle=1) j = self.m_b2dWorld.CreateRevoluteJoint( bodyA=box.m_body, bodyB=circle.m_body, localAnchorA=(0, 5), localAnchorB=(0, 0), enableMotor=True, maxMotorTorque=1000, motorSpeed=1, #rad/s enableLimit=False, lowerAngle=0, upperAngle=1) self.m_joints.append(j)
def CreateShape(self, shapeindex): try: shape = self.shapes[shapeindex] except IndexError: return pos = (10.0 * (2.0 * random() - 1.0), 10.0 * (2.0 * random())) defn = b2BodyDef( type=b2_dynamicBody, fixtures=b2FixtureDef(shape=shape, friction=0.3), position=pos, angle=(b2_pi * (2.0 * random() - 1.0)), ) if isinstance(shape, b2CircleShape): defn.angularDamping = 0.02 self.world.CreateBody(defn)
def CreateShape(self, shapeindex): try: shape = self.shapes[shapeindex] except IndexError: return pos = (10.0 * (2.0 * random() - 1.0), 10.0 * (2.0 * random() + 1.0)) defn = b2BodyDef( type=b2_dynamicBody, fixtures=b2FixtureDef(shape=shape, friction=0.3), position=pos, angle=(b2_pi * (2.0 * random() - 1.0)), ) if isinstance(shape, b2CircleShape): defn.angularDamping = 0.02 self.world.CreateBody(defn)
def append_to_world(self, world): self.world = world width, height = self._size x, y = self._position box = b2PolygonDef() hitbox_width = (self._hitbox.left * width / 100) * 2 hitbox_height = (self._hitbox.top * height / 100) * 2 box.SetAsBox((width / 2) - hitbox_width, (height / 2) - hitbox_height) box.density = self._density box.friction = 0 box.angle = 0 box.restitution = 0 body_def = b2BodyDef() body_def.fixedRotation = True body_def.position.Set(x, y) self._body = world.CreateBody(body_def) self._body.CreateShape(box) self._body.SetMassFromShapes()
def create_body(self, pos): x, y = pos self.lend = self.create_left_end(pos) self.segments = [] self.ends = [] if self.lend: prevBody = self.lend.body self.ends.append(self.lend) else: prevBody = None for i in range(self.length): sd = b2PolygonDef() sd.SetAsBox(self.PIECE_LENGTH, self.radius) sd.density = self.density sd.friction = 1 sd.restitution = 0 sd.filter.groupIndex = -1 bd = b2BodyDef() bd.linearDamping = 0 bd.angularDamping = 0.2 bd.position = (x + self.PIECE_LENGTH * 2 * i - self.PIECE_LENGTH, y) body = world.CreateBody(bd) body.CreateShape(sd) body.SetMassFromShapes() self.segments.append(body) if prevBody: jd = b2RevoluteJointDef() anchor = (x + self.PIECE_LENGTH * (2 * i - 1), y) jd.Initialize(prevBody, body, anchor) world.CreateJoint(jd).getAsType() prevBody = body self.rend = self.create_right_end((x + self.PIECE_LENGTH * (self.length - 1) * 2, y)) if self.rend: jd = b2RevoluteJointDef() anchor = (x + self.PIECE_LENGTH * (self.length - 1) * 2, y) jd.Initialize(prevBody, self.rend.body, anchor) world.CreateJoint(jd).getAsType() self.ends.append(self.rend)
def __init__(self, world, car): self.m_maxForwardSpeed = 0 self.m_maxBackwardSpeed = 0 self.m_maxDriveForce = 0 self.m_maxLateralImpulse = 0 self.m_groundAreas = [] bodyDef = b2BodyDef() bodyDef.type = b2_dynamicBody bodyDef.restitution = 0.1 bodyDef.position = car.m_body.position self.m_body = world.CreateBody(bodyDef) polygonShape = b2PolygonShape() polygonShape.SetAsBox(0.5, 1.25) fixture = self.m_body.CreateFixture(shape=polygonShape, density=5, isSensor=True) fixture.userData = CarTireFUD() fixture.userData.car = car self.m_body.userData = self self.m_currentTraction = 1
def setup_world(): global buoyancy world_bounds = b2AABB() world_bounds.lowerBound = (-200, -100) world_bounds.upperBound = (1000, 1000) world = b2World( world_bounds, b2Vec2(0, -30), # Gravity vector True # Use "sleep" optimisation ) wallsdef = b2BodyDef() walls = world.CreateBody(wallsdef) walls.userData = 'Blocks' WALLS = [ #(W, FLOOR * 0.5, (W / 2, FLOOR * 0.5), 0), # floor #(W / 2, 1, (W / 2, H + 1), 0), # ceiling (1, 600, (-1, -500), 0), # left wall #(1, 600, (W + 1, -500), 0), # right wall ] for wall in WALLS: shape = b2PolygonDef() shape.SetAsBox(*wall) walls.CreateShape(shape) for shape in read_shapes_from_svg('shapes/ground.svg'): walls.CreateShape(shape) buoyancydef = b2BuoyancyControllerDef() buoyancydef.normal = b2Vec2(0, 1) buoyancydef.offset = WATER_LEVEL buoyancydef.density = 2.5 buoyancydef.angularDrag = 0.5 buoyancydef.linearDrag = 3 buoyancy = world.CreateController(buoyancydef) return world
def createSimpleBox(self, screenBoxPosition, screenBoxSize, categoryBits, maskBits=settings.B2D_CAT_BITS_GROUND): screenBoxWidth = screenBoxSize[0] screenBoxHeight = screenBoxSize[1] boxWidth = screenBoxWidth / settings.B2D_PPM boxHeight = screenBoxHeight / settings.B2D_PPM boxPosition = self.convertScreenToWorld(screenBoxPosition) shape = b2PolygonShape() shape.SetAsBox(boxWidth / 2, boxHeight / 2) fixture = b2FixtureDef() fixture.density = 1 fixture.friction = 0.0 fixture.shape = shape fixture.filter = b2Filter( groupIndex=0, categoryBits=categoryBits, # I am... maskBits=maskBits # I collide with... ) # body definition bodyDef = b2BodyDef() bodyDef.position.Set(boxPosition[0], boxPosition[1]) bodyDef.type = b2_dynamicBody bodyDef.fixedRotation = False return self.addActor(ActorB2D(screenBoxPosition, (screenBoxWidth, screenBoxHeight)), bodyDef=bodyDef, fixture=fixture)
def __init__(self, world, pos=None): if (pos is None): pos = b2Vec2(0, 0) self.m_tires = [] # create car body bodyDef = b2BodyDef() bodyDef.type = b2_dynamicBody bodyDef.restitution = 0.1 bodyDef.position = pos self.m_body = world.CreateBody(bodyDef) self.m_body.angularDamping = 5 self.m_body.linearDamping = 1 vertices = [] for i in xrange(4): vertices.append(b2Vec2()) vertices[0].Set(-5.6, 0) vertices[1].Set(-5.6, 20) vertices[2].Set(5.6, 20) vertices[3].Set(5.6, 0) ''' vertices[0].Set(3, 0) vertices[1].Set(6, 5) vertices[2].Set(5.6, 11) vertices[3].Set(2, 20) vertices[4].Set(-2, 20) vertices[5].Set(-5.6, 11) vertices[6].Set(-6, 5) vertices[7].Set(-3, 0) ''' polygonShape = b2PolygonShape(vertices=vertices) self.m_body.CreateFixture( shape=polygonShape, density=100.0, ) # prepare common joint parameters jointDef = b2RevoluteJointDef() jointDef.bodyA = self.m_body jointDef.enableLimit = True jointDef.lowerAngle = 0 jointDef.upperAngle = 0 jointDef.localAnchorB.SetZero() # center of tire maxForwardSpeed = 150 maxBackwardSpeed = -40 backTireMaxDriveForce = 50 frontTireMaxDriveForce = 100 backTireMaxLateralImpulse = 1.5 frontTireMaxLateralImpulse = 0.5 # back left tire tire = TDTire(world, self) tire.setCharacteristics( maxForwardSpeed, maxBackwardSpeed, backTireMaxDriveForce, backTireMaxLateralImpulse) jointDef.bodyB = tire.m_body jointDef.localAnchorA.Set(-6, 1.5) world.CreateJoint(jointDef) self.m_tires.append(tire) # back right tire tire = TDTire(world, self) tire.setCharacteristics( maxForwardSpeed, maxBackwardSpeed, backTireMaxDriveForce, backTireMaxLateralImpulse) jointDef.bodyB = tire.m_body jointDef.localAnchorA.Set(6, 1.5) world.CreateJoint(jointDef) self.m_tires.append(tire) # front left tire tire = TDTire(world, self) tire.setCharacteristics( maxForwardSpeed, maxBackwardSpeed, frontTireMaxDriveForce, frontTireMaxLateralImpulse) jointDef.bodyB = tire.m_body jointDef.localAnchorA.Set(-6, 17) self.flJoint = world.CreateJoint(jointDef) self.m_tires.append(tire) # front right tire tire = TDTire(world, self) tire.setCharacteristics( maxForwardSpeed, maxBackwardSpeed, frontTireMaxDriveForce, frontTireMaxLateralImpulse) jointDef.bodyB = tire.m_body jointDef.localAnchorA.Set(6, 17) self.frJoint = world.CreateJoint(jointDef) self.m_tires.append(tire)