World > PhysicsWorld#
PhysicsWorld extends the regular world with a pymunk space. Use it when actors should fall, collide, bounce, or be connected with joints.
Install miniworlds_physics alongside miniworlds and import it from the separate package:
pip install miniworlds miniworlds_physics
from miniworlds import Circle, Rectangle
from miniworlds_physics import PhysicsWorld
world = PhysicsWorld(400, 300)
world.gravity = (0, 500)
floor = Rectangle((0, 260), 400, 40)
floor.physics.simulation = "static"
ball = Circle((80, 40), 20)
ball.physics.elasticity = 0.4
ball.physics.shape_type = "circle"
world.run()
Typical Physics Settings#
world.gravitycontrols global acceleration.world.dampingslows bodies over time.actor.physics.simulationchooses how an actor participates:simulated,manual,static, orNone.actor.physicsalso exposesdensity,friction,elasticity,shape_type, and velocity helpers.
Collision Hooks#
Physics worlds support dedicated actor callbacks such as on_touching_circle and on_separation_from_circle. These receive the other actor and contact information from the physics engine.
See also the Flappy Bird tutorial for a larger example.
Runtime Notes#
Physics support is intentionally separate from the main miniworlds package.
Desktop Python environments are the recommended target. Browser-based runtimes
such as Pyodide/H5P may not provide the same physics package support.
API Reference#
- class miniworlds_physics.physics_world.PhysicsWorld(columns=40, rows=40)[source]#
A PhysicsWorld is a playing field on which objects follow physical laws.
The PhysicsWorld itself defines some values with which the physics engine can be influenced, e.g. the gravity in the world.
All actors on a PhysicsWorld have an attribute
actor.physics, with which you can change the physical properties of the object.- property accuracy#
Sets number of physics-steps performed in each frame.
Default: 1
- property damping#
Amount of simple damping to apply to the space.
A value of 0.9 means that each body will lose 10% of its velocity per second. Defaults to 1.
- dialog#
- get_physics_collision_type(actor_class)[source]#
Return a stable pymunk collision type for an actor class.