Board -> PhysicsBoard : Token.physics#

class miniworldmaker.boards.board_templates.physics_board.token_physics.TokenPhysics(token, board)[source]#

Defines pyhsics-properties of a token, used as my_token.pyhsics.attribute or my_token.physics.method

Can only be used for tokens on a PhysicsBoard.

Examples

from miniworldmaker import *

board = PhysicsBoard((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)

board.run()

Public Data Attributes:

simulation

Sets simulation type for token (static, manual, simulated or None)

body

body_type

Returns body type of token

size

Sets size of physics_object in relation to object

shape_type

Sets shape type of object:

friction

Sets friction of token

elasticity

Sets elasticity of token

density

Sets density of token

velocity_x

Sets velocity in x-direction.

velocity_y

Sets velocity in y-direction

is_rotatable

defines, if token will be rotated by physics-engine.

Public Methods:

__init__(token, board)

velocity_function(body, gravity, damping, dt)

join(other[, type])

joins two tokens at their center points

remove_join(other)

Remove a joint between two tokens.

reload()

Removes token from space and reloads physics_model

remove()

Removes an object from physics-space

force_in_direction(direction, power)

Adds a force in given direction

impulse_in_direction(direction, power)

Adds an impulse in token-direction

Private Methods:

_start()

Starts the physics-simulation

_get_pymunk_shape()

_setup_physics_model()

_set_pymunk_position()

_set_pymunk_direction()

_set_mwm_token_position()

_set_mwm_token_direction()

_remove_from_space()

_simulation_preprocess_token()

Updates the physics model in every frame

_set_update_mode()

_unset_update_mode()

_is_in_update_mode()

_simulation_postprocess_token()

Reloads physics model from pygame data


property body#
property body_type#

Returns body type of token

Must not be used from outside - Use property simulation instead.

property density#

Sets density of token

Warning

Token is re-added to physics space after this operation - Velocity and impulses are lost.

property elasticity#

Sets elasticity of token

Warning

Token is re-added to physics space after this operation - Velocity and impulses are lost.

force_in_direction(direction, power)[source]#

Adds a force in given direction

Parameters
  • power – The power-value of the force.

  • direction – pymunk direction

property friction#

Sets friction of token

Warning

Token is re-added to physics space after this operation - Velocity and impulses are lost.

impulse_in_direction(direction, power)[source]#

Adds an impulse in token-direction

Examples

from miniworldmaker import *

board = PhysicsBoard(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)
board.run()
Parameters
  • power – The power-value of the impulse.

  • direction – pymunk direction

property is_rotatable#

defines, if token will be rotated by physics-engine.

join(other, type='pin')[source]#

joins two tokens at their center points

reload()[source]#

Removes token from space and reloads physics_model

remove()[source]#

Removes an object from physics-space

remove_join(other)[source]#

Remove a joint between two tokens.

Removes a joint between two tokens, if a joint exists.

Examples

Add and remove a joint on key_down:

import random
from miniworldmaker import *
board = PhysicsBoard((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()

board.run()
property shape_type#

Sets shape type of object:

Shape Types:
  • “rect”: Rectangle

  • “circle”: Circle

(Planned for future relases: autogeometry)

Warning

Token is re-added to physics space after this operation - Velocity and impulses are lost.

Examples

Demonstrate different shape types:

from miniworldmaker import *

board = PhysicsBoard(600,300)
Line((0,100),(100,150))
t = Token((0,50))
t.physics.shape_type = "rect"
Line((200,100),(300,150))
t = Token((200,50))
t.physics.shape_type = "circle"
board.run()
property simulation#

Sets simulation type for token (static, manual, simulated or None)

Sets simulation type for token:

  • simulated: Token is fully simulated by physics engine.

  • manual: Token is not affected by gravity.

  • static: Token is not moved by physics engine, but tokens can collide with token.

  • None: Token is not moved by physics engine and other tokens can’t collige with token.

property size#

Sets size of physics_object in relation to object

  • 1: Physics object size equals token size

  • < 1: Physics object is smaller than token

  • > 1: Physics object is larger than token.

Warning

Token is re-added to physics space after this operation - Velocity and impulses are lost.

static velocity_function(body, gravity, damping, dt)[source]#
property velocity_x#

Sets velocity in x-direction. Can be positive or negative.

Examples

Move a token 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