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 __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_cloth(world, cloth, wind, body_size, segment_count, distance_joints, wind_dir=(1, 1), wind_rand=0.0, distance_factor=1.45): segment_w, segment_h = segment_count body_spacing_w = body_size * 2 if wind: for x in range(segment_w): w = (b2Random(wind_dir[0] - wind_rand / 2.0, wind_dir[0] + wind_rand / 2.0), b2Random(wind_dir[1] - wind_rand / 2.0, wind_dir[1] + wind_rand / 2.0)) cloth[x][-1].linearVelocity += w # If any two points are too far from one another, find the joint connecting # them and destroy it. check_segments = [] for y in range(segment_h): for x in range(segment_w): if y > 0: check_segments.append((cloth[x][y], cloth[x][y - 1])) if x <= segment_w - 2: check_segments.append((cloth[x][y], cloth[x + 1][y])) thresh = body_spacing_w * distance_factor for c1, c2 in check_segments: if (c1.worldCenter - c2.worldCenter).length <= thresh: continue for joint in distance_joints: if ((joint.bodyA == c1 and joint.bodyB == c2) or (joint.bodyA == c2 and joint.bodyB == c1)): world.DestroyJoint(joint) distance_joints.remove(joint) break
def create_cloth(world, segment_count, body_size, position=(0, 30), group_index=-1, bar_height=0.5, base_hz=15, base_damping=0.11, density=0.2): segment_w, segment_h = segment_count body_spacing_w = body_size * 2 total_w = body_spacing_w * segment_w position = b2Vec2(*position) # The static bar at the top which holds the cloth bar = world.CreateStaticBody(position=position) bar.CreatePolygonFixture(box=(total_w / 2.0 + body_spacing_w, bar_height / 2.0), groupIndex=group_index) box_fixture = b2FixtureDef(shape=b2CircleShape(radius=body_size), groupIndex=group_index, density=density) weld_joints = [] distance_joints = [] cloth = [[None] * segment_h for x in range(segment_w)] for y in range(segment_h): pos = position - (total_w / 2.0, y * body_spacing_w) for x in range(segment_w): pos += (body_spacing_w, 0.0) body = world.CreateDynamicBody(position=pos, fixtures=box_fixture) cloth[x][y] = body if y == 0: joint = world.CreateWeldJoint(bodyA=body, bodyB=bar, anchor=body.position) weld_joints.append(joint) connect_bodies = [] for y in range(segment_h): for x in range(segment_w): if x <= segment_w - 2: left_body = cloth[x][y] right_body = cloth[x + 1][y] connect_bodies.append((left_body, right_body)) if y > 0: left_body = cloth[x][y] right_body = cloth[x][y - 1] connect_bodies.append((left_body, right_body)) for bodyA, bodyB in connect_bodies: joint = world.CreateDistanceJoint( bodyA=bodyA, bodyB=bodyB, anchorA=bodyA.position, anchorB=bodyB.position, frequencyHz=base_hz + b2Random(0, base_hz / 2.0), dampingRatio=base_damping + b2Random(0.01, base_damping), ) distance_joints.append(joint) return cloth, weld_joints, distance_joints
def generate(self): lower = (-8, -8) upper = (8, 8) self.verts = verts = [] for i in range(b2_maxPolygonVertices): x = 10.0 * b2Random(0.0, 10.0) y = 10.0 * b2Random(0.0, 10.0) # Clamp onto a square to help create collinearities. # This will stress the convex hull algorithm. verts.append(b2Clamp((x, y), lower, upper))
def checkBounds(self): self.hash = None to_remove = [ drop for drop in self.liquid if drop.position.y < self.fluid_miny] for drop in to_remove: self.liquid.remove(drop) self.world.DestroyBody(drop) self.createDroplet( (0.0 + b2Random(-0.6, 0.6), 15.0 + b2Random(-2.3, 2.0))) if self.surfer.position.y < -15: self.world.DestroyBody(self.surfer) self.createBoxSurfer()
def LaunchRandomBomb(self): """ Create a new bomb and launch it at the testbed. """ p = b2Vec2(b2Random(-15.0, 15.0), 30.0) v = -5.0 * p self.LaunchBomb(p, v)
def checkBounds(self): self.hash = None to_remove = [ drop for drop in self.liquid if drop.position.y < self.fluid_miny ] for drop in to_remove: self.liquid.remove(drop) self.world.DestroyBody(drop) self.createDroplet( (0.0 + b2Random(-0.6, 0.6), 15.0 + b2Random(-2.3, 2.0))) if self.surfer.position.y < -15: self.world.DestroyBody(self.surfer) self.createBoxSurfer()
def Launch(self): self.body.transform = [(0, 4), 0] self.body.linearVelocity = (0, 0) self.body.angularVelocity = 0 self.x = b2Random() self.bullet.transform = [(self.x, 10), 0] self.bullet.linearVelocity = (0, -50) self.bullet.angularVelocity = 0
def __init__(self): super(CollisionProcessing, self).__init__() # Tell the framework we're going to use contacts, so keep track of them # every Step. self.using_contacts = True # Ground body world = self.world ground = world.CreateBody( shapes=b2EdgeShape(vertices=[(-50, 0), (50, 0)],) ) xlow, xhi = -5, 5 ylow, yhi = 2, 35 random_vector = lambda: b2Vec2( b2Random(xlow, xhi), b2Random(ylow, yhi)) # Small triangle triangle = b2FixtureDef( shape=b2PolygonShape(vertices=[(-1, 0), (1, 0), (0, 2)]), density=1, ) world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=triangle, ) # Large triangle (recycle definitions) triangle.shape.vertices = [ 2.0 * b2Vec2(v) for v in triangle.shape.vertices] tri_body = world.CreateBody(type=b2_dynamicBody, position=random_vector(), fixtures=triangle, fixedRotation=True, # <-- ) # note that the large triangle will not rotate # Small box box = b2FixtureDef( shape=b2PolygonShape(box=(1, 0.5)), density=1, restitution=0.1, ) world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=box, ) # Large box box.shape.box = (2, 1) world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=box, ) # Small circle circle = b2FixtureDef( shape=b2CircleShape(radius=1), density=1, ) world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=circle, ) # Large circle circle.shape.radius *= 2 world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=circle, )
def createBoxSurfer(self): self.surfer = self.world.CreateDynamicBody(position=(0, 25)) self.surfer.CreatePolygonFixture( density=1, box=(b2Random(0.3, 0.7), b2Random(0.3, 0.7)), )
def __init__(self): super(CollisionProcessing, self).__init__() # Tell the framework we're going to use contacts, so keep track of them # every Step. self.using_contacts = True # Ground body world = self.world ground = world.CreateBody(shapes=b2EdgeShape(vertices=[(-50, 0), (50, 0)], )) xlow, xhi = -5, 5 ylow, yhi = 2, 35 random_vector = lambda: b2Vec2(b2Random(xlow, xhi), b2Random( ylow, yhi)) # Small triangle triangle = b2FixtureDef( shape=b2PolygonShape(vertices=[(-1, 0), (1, 0), (0, 2)]), density=1, ) world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=triangle, ) # Large triangle (recycle definitions) triangle.shape.vertices = [ 2.0 * b2Vec2(v) for v in triangle.shape.vertices ] tri_body = world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=triangle, fixedRotation=True, # <-- ) # note that the large triangle will not rotate # Small box box = b2FixtureDef( shape=b2PolygonShape(box=(1, 0.5)), density=1, restitution=0.1, ) world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=box, ) # Large box box.shape.box = (2, 1) world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=box, ) # Small circle circle = b2FixtureDef( shape=b2CircleShape(radius=1), density=1, ) world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=circle, ) # Large circle circle.shape.radius *= 2 world.CreateBody( type=b2_dynamicBody, position=random_vector(), fixtures=circle, )