ActorPhysics (actor.physics)#
Every actor in a PhysicsWorld gains an actor.physics attribute
of type ActorPhysics. Use it to control how the physics engine treats that actor.
Access it as:
my_actor.physics.simulation = "simulated"
my_actor.physics.elasticity = 0.8
my_actor.physics.velocity_x = 200
Simulation mode#
The simulation property is the primary switch. It must be set before the world starts running, or after a geometry change.
Value |
Description |
|---|---|
|
Fully simulated: affected by gravity, collisions, impulses. |
|
Kinematic: not affected by gravity, but can be moved manually and collides with others. |
|
Not moved by the engine; other actors can collide with it (e.g. floors, walls). |
|
Completely removed from the physics space; no collisions possible. |
Physical properties#
Property |
Default |
Description |
|---|---|---|
|
|
Mass density. Higher value = heavier object. Re-adds actor to physics space when changed. |
|
|
Surface friction (0 = frictionless, 1 = very rough). Re-adds actor to physics space. |
|
|
Bounciness (0 = no bounce, 1 = perfect bounce). Re-adds actor to physics space. |
|
|
Collision shape: |
|
|
Scale factor for the physics collision box relative to the actor’s visual size. |
|
depends on mode |
Whether the physics engine may rotate the actor. |
|
|
Per-actor damping multiplied with the world’s damping. |
|
|
Clamp the horizontal velocity to this maximum. |
Velocity control (manual / simulated)#
Property |
Description |
|---|---|
|
Horizontal velocity in pixels per second. Positive = right. |
|
Vertical velocity in pixels per second. Positive = down. |
Impulses and forces#
Method |
Description |
|---|---|
|
Apply an instantaneous impulse in the given direction. |
|
Apply a continuous force in the given direction. |
Joints#
Method |
Description |
|---|---|
|
Create a pin joint between this actor and |
|
Remove the joint to |
Example: Platformer character#
from miniworlds import Rectangle
from miniworlds_physics import PhysicsWorld
world = PhysicsWorld(400, 300)
floor = Rectangle((0, 270), 400, 30)
floor.physics.simulation = "static"
floor.physics.friction = 0.8
player = Rectangle((50, 200), 30, 50)
player.physics.simulation = "manual"
player.physics.shape_type = "rect"
@player.register
def on_key_pressed(self, keys):
if "a" in keys:
self.physics.velocity_x = -150
elif "d" in keys:
self.physics.velocity_x = 150
else:
self.physics.velocity_x = 0
@player.register
def on_key_down(self, keys):
if " " in keys:
self.physics.velocity_y = -400
world.run()
API Reference#
- class miniworlds_physics.actor_physics.ActorPhysics(actor, world)[Quellcode]#
Defines physics-properties of a actor, used as my_actor.physics.attribute or my_actor.physics.method
Can only be used for actors on a PhysicsWorld.
Examples
from miniworlds import * world = PhysicsWorld((800, 600)) a = Circle() a.position = (75, 200) a.color = (255,0,0) a.physics.simulation = "simulated" a.direction = 180 a.physics.shape_type = "circle" a.impulse(45, 1500) world.run()
- property body#
- property body_type#
Returns body type of actor
Must not be used from outside - Use property simulation instead.
- property density#
Sets density of actor
Warnung
Actor is re-added to physics space after this operation - Velocity and impulses are lost.
- property elasticity#
Sets elasticity of actor
Warnung
Actor is re-added to physics space after this operation - Velocity and impulses are lost.
- force_in_direction(direction, power)[Quellcode]#
Adds a force in given direction
- Parameter:
power – The power-value of the force.
direction – pymunk direction
- property friction#
Sets friction of actor
Warnung
Actor is re-added to physics space after this operation - Velocity and impulses are lost.
- impulse_in_direction(direction, power)[Quellcode]#
Adds an impulse in actor-direction
Examples
from miniworlds import * world = PhysicsWorld(300, 200) rect = Rectangle((280,120), 20, 80) rect.physics.simulation = "manual" ball = Circle((50,50),20) @rect.register def act(self): rect.x -= 1 if rect.x == 0: rect.x = 280 @ball.register def on_key_down(self, key): self.physics.impulse_in_direction(0, 5000) world.run()
- Parameter:
power – The power-value of the impulse.
direction – pymunk direction
- property is_rotatable#
defines, if actor will be rotated by physics-engine.
- join(other, type='pin')[Quellcode]#
joins two actors at their center points
- reload()[Quellcode]#
Removes actor from space and reloads physics_model
- remove()[Quellcode]#
Removes an object from physics-space
- remove_join(other)[Quellcode]#
Remove a joint between two actors.
Removes a joint between two actors, if a joint exists.
Examples
Add and remove a joint on key_down:
import random from miniworlds import * world = PhysicsWorld((400, 200)) connected = False line = None anchor = Rectangle() anchor.size = (20,20) anchor.center = (100, 20) anchor.physics.simulation = "manual" other_side = Line((250,100),(500,200)) def add_line(obj1, obj2): l = Line(obj1.center, obj2.center) l.physics.simulation = None @l.register def act(self): self.start_position = obj1.center self.end_position = obj2.center return l c = Circle() @c.register def on_key_down(self, key): global connected global line if not connected: print("not connected") self.physics.join(anchor) line = add_line(self, anchor) connected = True else: print("connected") self.physics.remove_join(anchor) line.remove() world.run()
- property shape_type#
Sets shape type of object:
- Shape Types:
„rect“: Rectangle
„circle“: Circle
Warnung
Actor is re-added to physics space after this operation - Velocity and impulses are lost.
Examples
Demonstrate different shape types:
from miniworlds import * world = PhysicsWorld(600,300) Line((0,100),(100,150)) t = Actor((0,50)) t.physics.shape_type = "rect" Line((200,100),(300,150)) t = Actor((200,50)) t.physics.shape_type = "circle" world.run()
- property simulation#
Sets simulation type for actor (static, manual, simulated or None)
Sets simulation type for actor:
simulated: Actor is fully simulated by physics engine.
manual: Actor is not affected by gravity.
static: Actor is not moved by physics engine, but actors can collide with actor.
None: Actor is not moved by physics engine and other actors can’t collide with actor.
- property size#
Sets size of physics_object in relation to object
1: Physics object size equals actor size
< 1: Physics object is smaller than actor
> 1: Physics object is larger than actor.
Warnung
Actor is re-added to physics space after this operation - Velocity and impulses are lost.
- static velocity_function(body, gravity, damping, dt)[Quellcode]#
- property velocity_x#
Sets velocity in x-direction. Can be positive or negative.
Examples
Move a actor left or right.
def on_key_pressed_d(self): self.physics.velocity_x = 50 def on_key_pressed_a(self): self.physics.velocity_x = - 50
- property velocity_y#
Sets velocity in y-direction