0
\$\begingroup\$

I would like to use pybox2d's Framework class to visualize the simulation. However, the default screen / windows size is 640 x 480 pixels and I don't find a way to change it. I would like to know how I can change the screen size. The code below is a minimal working example:

enter image description here

from Box2D.examples.framework import Framework, main
from Box2D import b2EdgeShape, b2FixtureDef, b2PolygonShape, b2Vec2, b2Color


class TestObject(Framework):

    def __init__(self):
        super(TestObject, self).__init__()

        # World
        self.world.gravity = (0.0, -10.0)
        self.world.CreateStaticBody(
            shapes=[
                    b2EdgeShape(vertices=[(-10, 0), (10, 0)]),
                    b2EdgeShape(vertices=[(-10, 0), (-20, 20)]),
                    b2EdgeShape(vertices=[(10, 0), (20, 20)]),
                ]
        )

        obj_vertices = [(0.5, 0.5), (-0.5, 0.5), (-0.5, -0.5), (0.5, -0.5)]

        # Create object
        position = b2Vec2(12, 20)
        linear_velocity = (0.0, -10.0)
        self.obj = self.world.CreateDynamicBody(position=position, linearVelocity=linear_velocity, allowSleep=False)

        density = 1
        height, width = 6, 4
        vertices = [(width * item[0], height * item[1]) for item in obj_vertices]
        shape = b2PolygonShape(vertices=vertices)
        fixture_def = b2FixtureDef(shape=shape, density=density)
        self.obj.CreateFixture(fixture_def)

    def Step(self, settings):
        super(TestObject, self).Step(settings)


if __name__ == "__main__":
    main(TestObject)
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I have not used this framework before, so this is untested, but looking at the code, I would try this first:

One way to go appears to by modify the file pybox2d/library/Box2D/examples/backends/pygame_framework.py, where it calls pygame.display.set_mode (or pybox2d/library/Box2D/examples/backends/opencv_framework.py where it defines the ctor def __init__(self, w=640, h=480, resizable=False)) and set the size you want, but I presume this is a hackish way to do it.

It could get you going for the tests you're doing.

If you need a "cleaner" way, maybe what you need to do it is to clone the pybox2d/library/Box2D/examples/backends/pygame_framework.py file and implement the w=640, h=480 in the ctor like it's being done in the other backend, then you would pass the values at object construction in your own test code.

It seems the way to integrate a Framework with a Backend is described at the end of framework.py,

# Your framework classes should follow this format. If it is the 'foobar'
# framework, then your file should be 'backends/foobar_framework.py' and you
# should have a class 'FoobarFramework' that subclasses FrameworkBase. Ensure
# proper capitalization for portability.
from Box2D.examples import backends

try:
    framework_name = '%s_framework' % (fwSettings.backend.lower())
    __import__('backends', globals(), fromlist=[framework_name], level=1)
    framework_module = getattr(backends, framework_name)
    Framework = getattr(framework_module,
                        '%sFramework' % fwSettings.backend.capitalize())
except Exception as ex:
    print('Unable to import the back-end %s: %s' % (fwSettings.backend, ex))
    print('Attempting to fall back on the pygame back-end.')

    from Box2D.examples.backends.pygame_framework import PygameFramework as Framework

This may not be the complete/right solution but I hope it will help you get started finding it. And whenever you find it, please don't hesitate to write an answer to this question and mark it as accepted :)

\$\endgroup\$
1
  • \$\begingroup\$ I agree though that this is a "bug" that could be worth an update to pygame_framework.py and a pull request to the repository. \$\endgroup\$
    – Vaillancourt
    Commented Sep 25, 2022 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .