def draw_hit(hit_point, hit_normal): head = hit_point + hit_normal * 0.5 p1_color = (100, 230, 100) s1_color = (200, 200, 200) s2_color = (230, 230, 100) renderer.draw_point(hit_point, p1_color) renderer.draw_line_segment(point1, hit_point, s1_color) renderer.draw_line_segment(hit_point, head, s2_color) renderer.draw_line_segment(point1, point2, (0, 255, 0, 127)) if mode in ('closest', 'any'): if mode == 'closest': item = world.raycast_closest(point1, point2) else: item = world.raycast_any(point1, point2) if item is not None: renderer.draw_line_segment(point1, item.point, (255, 0, 0, 255)) draw_hit(item.point, item.normal) else: for item in world.raycast_all(point1, point2): renderer.draw_line_segment(point1, item.point, (255, 0, 0, 255)) draw_hit(item.point, item.normal) if __name__ == '__main__': from main_simple import main main(setup, keyboard_hook=keyboard, pre_render_hook=raycast_render)
plank_count=30, friction=0.2, density=20) fixture = FixtureDef(shape=PolygonShape(vertices=[ (-0.5, 0.0), (0.5, 0.0), (0.0, 1.5), ]), density=1.0) for i in range(2): world.create_dynamic_body( position=(-8 + 8 * i, 12), fixtures=fixture, ) fixture = FixtureDef(shape=CircleShape(radius=0.5), density=1) for i in range(3): world.create_dynamic_body( position=(-6 + 6 * i, 10), fixtures=fixture, ) world.state['bridge_bodies'] = bodies if __name__ == '__main__': from main_simple import main main(setup)
M = 10 position = Vec2(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 = world.create_static_body(position=(0, -a)) for position in ground_positions(): ground.create_polygon_fixture(box=(a, a, position, 0)) create_pyramid(world) def pre_render_hook(world, renderer): status = world.status print(status) height = status.tree_height leaf_count = status.proxy_count min_node_count = 2 * leaf_count - 1 min_height = ceil(log(float(min_node_count)) / log(2)) print('Dynamic tree height=%d, min=%d' % (height, min_height)) if __name__ == '__main__': from main_simple import main world = main(setup, pre_render_hook=pre_render_hook)
# Set the fixture as a sensor so that the car doesn't collide ground_area_2.create_polygon_fixture( box=(9, 5, (5, 20), math.radians(-40)), sensor=True) def pre_step(world, renderer): for car in world.state['cars']: car.update(keys=world.state['key_status'], hz=renderer.target_fps) for i, tire in enumerate(car.tires): # for contact in tire.monitor_contacts: # print('tire monitor contacts', i, # [body.__class__.__name__ for body in contact.bodies]) tire.update_traction() def pre_render(world, renderer): pass def keyboard(world, key, pressed): key_status = world.state['key_status'] key_status[key] = pressed if __name__ == '__main__': from main_simple import main main(setup, keyboard_hook=keyboard, pre_step_hook=pre_step, pre_render_hook=pre_render)
density=1) # Create the balls on the ground for i in range(ball_count): world.create_dynamic_body( fixtures=circle, position=(-40 + 2.0 * i, 0.5), ) info = create_thing(world) world.state.update(info) def keyboard(world, key, pressed): if pressed: joint = world.state['motor_joint'] if key == 'a': joint.motor_speed = -2 elif key == 'd': joint.motor_speed = 2 elif key == 's': joint.motor_speed = 0 elif key == 'm': joint.motor_enabled = not joint.motor_enabled print('Motor enabled:', joint.motor_enabled) if __name__ == '__main__': from main_simple import main main(setup, keyboard_hook=keyboard)
plank_pos = Vec2(x + i * plank_size[0], y) body = world.create_dynamic_body(position=plank_pos, fixtures=plank) if prev_body is not None: anchor = plank_pos - (plank_size[0] / 2, 0) if joint_type == "revolute": joint = world.create_revolute_joint((prev_body, body), anchor=anchor) else: # You can try a WeldJoint for a slightly different effect. joint = world.create_weld_joint((prev_body, body), anchor=anchor) joints.append(joint) bodies.append(body) prev_body = body return bodies, joints def setup(world): """Chain example""" ground = world.create_static_body() ground.create_edge_fixture(vertices=[(-40, 0), (40, 0)]) create_chain(world, position=(0.5, 25), attach_to=ground) if __name__ == "__main__": from main_simple import main main(setup)
world.create_friction_joint( bodies=(ground, body), local_anchors=((0, 0), (0, 0)), collide_connected=True, max_force=body.mass * joint_gravity, max_torque=body.mass * r * joint_gravity ) def keyboard(world, key, pressed): body = world.state['ship'] if not body: return if not pressed: return if key == 'w': f = body.get_world_vector((0.0, -200.0)) p = body.get_world_point((0.0, 2.0)) body.apply_force(f, p, True) elif key == 'a': body.apply_torque(50.0, True) elif key == 'd': body.apply_torque(-50.0, True) if __name__ == '__main__': from main_simple import main main(setup, keyboard_hook=keyboard)