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.gravity controls global acceleration.

  • world.damping slows bodies over time.

  • actor.physics.simulation chooses how an actor participates: simulated, manual, static, or None.

  • actor.physics also exposes density, 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

connect(actor1, actor2)[source]#
Return type:

Line

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.

property gravity: tuple#

Defines gravity in physics world.

Gravity is a 2-tuple with gravity in x-direction and y-direction.

Default gravity: x=0, y=500

Examples

Get all actors at mouse position:

world = PhysicsWorld(400,400)
world.gravity = (0, 0)
on_new_actor(actor)[source]#

Synchronizes newly added actors with the physics simulation.

Actors that have physics disabled are removed from pymunk so they stay as non-simulated world objects.

Parameters:

actor – The newly added actor.