World > PhysicsWorld#

PhysicsWorld erweitert die normale Welt um einen pymunk-Raum. Verwende sie, wenn Actors fallen, kollidieren, abprallen oder über Gelenke verbunden werden sollen.

Installiere miniworlds_physics zusammen mit miniworlds und importiere es aus dem separaten Paket:

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()

Typische Physics-Einstellungen#

  • world.gravity steuert die globale Beschleunigung.

  • world.damping bremst Körper mit der Zeit ab.

  • actor.physics.simulation legt fest, wie ein Actor an der Physik teilnimmt: simulated, manual, static oder None.

  • actor.physics stellt außerdem density, friction, elasticity, shape_type und Hilfen für Geschwindigkeiten bereit.

Kollisions-Hooks#

Physics-Welten unterstützen spezielle Actor-Callbacks wie on_touching_circle und on_separation_from_circle. Diese erhalten den anderen Actor und Kontaktinformationen aus der Physik-Engine.

Siehe auch das Flappy-Bird-Tutorial für ein größeres Beispiel.

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)[Quellcode]#

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)[Quellcode]#
Rückgabetyp:

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)[Quellcode]#

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)[Quellcode]#

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.

Parameter:

actor – The newly added actor.