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 |
|---|---|---|
|
|
Global gravity as |
|
|
Velocity decay factor applied each second. |
|
|
Number of physics sub-steps per frame. Higher values improve simulation accuracy at the cost of performance. |
|
|
When |
Methods#
Method |
Description |
|---|---|
|
Draw a dynamic line between two actors that updates its endpoints every frame. Returns the |
|
Called automatically when an actor is added; removes non-simulated actors from the physics space. |
|
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)[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.
- 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)[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.