예제 #1
0
def do_test(iters):
    start = time.time()
    world = b2.World((0, -10), True)

    world.create_static_body(shapes=b2.Edge((-40, 0), (40, 0)))

    box_half_size = (0.5, 0.5)
    box_density = 5.0
    box_rows = 20

    x = Vec2(-7, 0.75)
    delta_x = (0.5625, 1.25)
    delta_y = (1.125, 0)

    fixture = b2.Fixture(b2.Polygon(box=box_half_size), density=box_density)
    for i in range(box_rows):
        y = copy(x)

        for j in range(i, box_rows):
            world.create_dynamic_body(position=y, fixtures=fixture)
            y += delta_y

        x += delta_x
    init_time = time.time() - start

    range_ = range(iters)

    TIMESTEP = 1.0 / 60
    start = time.time()
    for i in range_:
        world.step(TIMESTEP, 10, 10)
        world.clear_forces()

    step_time = time.time() - start
    return init_time, step_time
예제 #2
0
    def __init__(self):
        Framework.__init__(self)
        
        world = self.world

        ground = world.create_static_body(shapes=b2.Edge((-20, 0),( 20, 0))) 

        controller = world.create_buoyancy_controller(
                                        offset=15, normal=(0, 1), density=2, 
                                        linear_drag=2, angular_drag=1)
       
        # Create a few spheres to bob around
        for i in range(7):
            body = self.world.create_dynamic_body(
                    position=(-10+4.0*i, 20), 
                    fixtures=b2.Fixture(shape=b2.Circle(radius=1.0), 
                                density=1.0)
                    )

            controller.add_body(body)

        # Create a bridge, and add it to the controller
        num_planks = 30
        plank_bodies = create_bridge(self.world, ground, (1.0, 0.25), (-14.5, 5), num_planks, 0.2, 1)
        for body in plank_bodies:
            controller.add_body(body)

        # Create a circle underneath the bridge
        body = self.world.create_dynamic_body(
                position=(-10.0, 0), 
                fixtures=b2.Fixture(shape=b2.Circle(radius=1.0), 
                            density=1.0)
                )

        controller.add_body(body)

        # And finally, some triangles
        for i in range(5):
            body = self.world.create_dynamic_body(
                    position=(-10+3.0*i, 20), 
                    fixtures=b2.Fixture(
                                shape=b2.Polygon(
                                    vertices=[(-0.5,0),(0,-0.5),(0.5, 0.0)]),
                                density=1.0)
                    )

            controller.add_body(body)

        # And (really) finally this time, just something so we can be sure 
        # edges work, too.
        edge = world.create_dynamic_body(
                    fixtures=b2.Fixture(shape=b2.Edge((5, 0),(5, 3)),
                                density=1.0)
                    )
        controller.add_body(edge)
예제 #3
0
def do_test(iters):
    start = time.time()

    world = b2.World()
    columns=5
    rows=16
    ground = world.create_static_body(
            shapes=[ 
                    b2.Edge((-40,0),(40,0)),
                    b2.Edge((20,0),(20,20)),
                ]
            ) 

    box=b2.Fixture(
            shape=b2.Polygon(box=(0.5,0.5)),
            density=1,
            friction=0.3)
    circle=b2.Fixture(
            shape=b2.Circle(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):
            world.create_dynamic_body(
                    fixtures=box,
                    position=(box_start+box_space*j, 0.752 + 1.54 * i)
                    )
            world.create_dynamic_body(
                    fixtures=circle,
                    position=(circle_start+circle_space*j, 0.752 + 1.54 * i)
                    )

    init_time = time.time() - start

    range_ = range(iters)

    TIMESTEP = 1.0 / 60
    start = time.time()
    for i in range_:
        world.step(TIMESTEP, 10, 10)
        world.clear_forces()

    step_time = time.time() - start
    return init_time, step_time
예제 #4
0
    def __init__(self):
        Framework.__init__(self)
        
        world = self.world

        # Turn off normal gravity
        world.gravity = (0, 0)

        ground = world.create_static_body(shapes=b2.Edge((-20, 0),( 20, 0))) 

        controller = world.create_gravity_controller(G=0.8, inv_sqr=True)
       
        # Create a few spheres to bob around
        for i in range(1,4):
            body = self.world.create_dynamic_body(
                    position=(0.25 * i, 2.0 + 7.5 * i),
                    fixtures=b2.Fixture(shape=b2.Circle(radius=0.25 * i), 
                                density=1.0),
                    bullet=True,
                    )

            controller.add_body(body)

        # Create a bridge, and add it to the controller
        num_planks = 30
        plank_bodies = create_bridge(self.world, ground, (1.0, 0.25), (-14.5, 5), num_planks, 0.2, 10)
        for body in plank_bodies:
            controller.add_body(body)

        # Create a circle underneath the bridge
        body = self.world.create_dynamic_body(
                position=(-10.0, 0), 
                fixtures=b2.Fixture(shape=b2.Circle(radius=1.0), 
                            density=10.0)
                )

        controller.add_body(body)

        # And finally, some triangles
        for i in range(5):
            body = self.world.create_dynamic_body(
                    position=(-10+3.0*i, 20), 
                    fixtures=b2.Fixture(shape=b2.Polygon(vertices=[(-0.5,0),(0,-0.5),(0.5, 0.0)]),
                                density=1.0)
                    )

            controller.add_body(body)
예제 #5
0
# PYPY version
pypy_world = b2.World((gravity_X, gravity_Y), True)
pypy_ground = pypy_world.create_static_body(position=(0, -10))
pypy_ground.create_polygon_fixture(box=(50, 10))

if amountOfBlocks > 0:
  pypy_middle_block = pypy_world.create_static_body(position=(xMiddleBlock, yMiddleBlock))
  pypy_middle_block.create_polygon_fixture(box=(blockWidth, blockHeight))
if amountOfBlocks > 1:
  pypy_middle_block = pypy_world.create_static_body(position=(-xMiddleBlock, yMiddleBlock + blockHeight))
  pypy_middle_block.create_polygon_fixture(box=(blockWidth, blockHeight))


pypy_box=b2.Fixture(
	shape=b2.Polygon(box=(8,8)),
    density=density,
    friction=friction)
pypy_body = pypy_world.create_dynamic_body(
    fixtures=pypy_box,
    position=(x0, y0))

iterationNumber = 80;

pypy_start = time.time()
for i in range(iterationNumber):
   pypy_world.step(timeStep, velocityIterations, positionIterations)
   pypy_world.clear_forces()
pypy_finish = time.time()
print "Result:", (pypy_finish - pypy_start) * 1000
예제 #6
0
파일: part2.py 프로젝트: neikila/Practice
blockWidth = 5
blockHeight = 10

timeStep = 1.0 / 10.0
velocityIterations = 10
positionIterations = 10

world = b2.World((gravity_X, gravity_Y), True)
ground = world.create_static_body(position=(0, -10))
ground.create_polygon_fixture(box=(50, 10))

middle_block = world.create_static_body(position=(xMiddleBlock, yMiddleBlock))
middle_block.create_polygon_fixture(box=(blockWidth, blockHeight))

box = b2.Fixture(shape=b2.Polygon(box=(8, 8)),
                 density=density,
                 friction=friction)
body = world.create_dynamic_body(fixtures=box, position=(x0, y0))

print "   py", "      pypy", "     differ", "  Theoretical"

# print dir(pypy_body)

# This is our little game loop.
for i in range(80):
    world.step(timeStep, velocityIterations, positionIterations)
    world.clear_forces()

    y = body.position.y