Token Physics

Token Physics

class miniworldmaker.tokens.physics.token_physics.TokenPhysics(token, board)[source]

The PhysicsProperty class describes all properties necessary to physically simulate an object using the pymunk engine.

If you use a physics board, you can access token-physics properties with token_name.physics.property

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_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 :return:

velocity_y

Sets velocity in y-direction :return:

is_rotatable

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

Public Methods:

__init__(token, board)

join(other)

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

impulse_in_direction(direction, power)

Adds an impulse in token-direction

force_in_direction(direction, power)

Adds an force 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 Returns:


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 an force in token-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)[source]

joins two tokens at their center points

reload()[source]

Removes token from space and reloads physics_model

Returns

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.

property velocity_x

Sets velocity in x-direction :return:

property velocity_y

Sets velocity in y-direction :return: