PhysicsWorld#

PhysicsWorld is a World subclass backed by a pymunk space. Every actor placed in this world automatically gets an actor.physics object (see ActorPhysics).

World-level properties#

Property

Default

Description

gravity

(0, 900)

Global gravity as (x, y) acceleration tuple.

damping

0.9

Velocity decay factor applied each second. 0.9 = lose 10 % per second.

accuracy

1

Number of physics sub-steps per frame. Higher values improve simulation accuracy at the cost of performance.

debug

False

When True, pymunk debug shapes are drawn on top of the world.

Methods#

Method

Description

connect(actor1, actor2)

Draw a dynamic line between two actors that updates its endpoints every frame. Returns the Line actor.

on_new_actor(actor)

Called automatically when an actor is added; removes non-simulated actors from the physics space.

get_physics_collision_type(actor_class)

Return a stable pymunk collision-type integer for an actor class. Used internally for collision dispatch.

Collision callbacks#

Define methods on any actor to react to physics collisions. The method name determines which actor class triggers it:

@my_actor.register
def on_touching_Circle(self, other, collision):
    """Called when my_actor touches any Circle."""
    other.remove()

@my_actor.register
def on_separation_from_Circle(self, other, collision):
    """Called when my_actor separates from a Circle."""
    pass

The other parameter is the colliding actor; collision is a dict with collision metadata.

Example: Ball with gravity#

from miniworlds import Circle, Rectangle, Line
from miniworlds_physics import PhysicsWorld

world = PhysicsWorld(400, 400)
world.gravity = (0, 900)
world.damping = 0.99

floor = Rectangle((0, 370), 400, 30)
floor.physics.simulation = "static"
floor.physics.friction = 1.0

ball = Circle((200, 50), 25)
ball.physics.simulation = "simulated"
ball.physics.elasticity = 0.6

@ball.register
def on_key_down(self, keys):
    if " " in keys:
        self.physics.velocity_y = -400

world.run()

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)
gravity_x: float
gravity_y: float
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.