Shape

class pecrs.shape.Shape(x, y)
Parameters
  • x (Int) – Position of the shape in the x-axis

  • y (Int) – Position of the shape in the y-axis

Abstract Base Shape that other shapes extend from. Shapes are simple data structures that describe the physical properties of an body to a Collider.

position

Tuple(Int, Int), position of the shape

area

Tuple(Int, Int) or None, collision area of the shape

class pecrs.shape.Rect(x, y, width, height)
Parameters
  • x (Int) – Position of the shape in the x-axis

  • y (Int) – Position of the shape in the y-axis

  • width (int) – Width of the Rect

  • height (int) – Height of the Rect

A Rectangle Treated as Axis-Aligned Bounding Boxes by the Collider.

position

Tuple(Int, Int), position of the shape

width

Int, Width, extending up

height

Int, Height, extending to the right

class pecrs.shape.Circle(x, y, radius)
Parameters
  • x (Int) – Position of the shape in the x-axis

  • y (Int) – Position of the shape in the y-axis

  • radius (int) – Width of the Rect

A Circle

position

Tuple(Int, Int), position of the shape

radius

Int, Radius, extending from the center.

Collider

class pecrs.collider.Collider

Collider handles collisions between two objects with physical dimensions. Note that the collider is abstract and low-level, dealing with any set of positions and dimensions.

check(shape, other)
Parameters
  • shape (Shape) – Shape of the first object

  • other (Shape) – Shape of the second object

Returns

True if there is a collision, False is not

Return type

bool

Checks for a collision between any two shapes. Determines what the shapes are and sends them to the appropriate collision method. If you already know the shape of your objects, use a more direct method.

check_rects(shape, other)
Parameters
  • shape (Rect) – Shape of the first Rect

  • other (Rect) – Shape of the second Rect

Returns

True if there is a collision, False is not

Return type

bool

Checks for a collision between any two Rects. This is a higher level interface for Collider.rect_rect(). Note that Pyglet sprites are effectively Rects and can be used interchangably here.

check_circles(shape, other)
Parameters
  • shape (Circle) – Shape of the first Circle

  • other (Circle) – Shape of the second Circle

Returns

True if there is a collision, False is not

Return type

bool

Checks for a collision between any two Circles. This is a higher level interface for Collider.circle_circle().

rect_rect(w, h, x, y, wo, ho, xo, yo)
Parameters
  • w (Int) – Width of the first Rect

  • h (Int) – Height of the first Rect

  • x (Int) – x position of the first Rect

  • y (Int) – y position of the first Rect

  • wo (Int) – Width of the second Rect

  • ho (Int) – Height of the second Rect

  • xo (Int) – x position of the second Rect

  • yo (Int) – y position of the second Rect

Checks for a collision between two Rects in abstract.

rect_circle(w, h, x, y, ro, xo, yo)
Parameters
  • w (Int) – Width of the first Rect

  • h (Int) – Height of the first Rect

  • x (Int) – x position of the first Rect

  • y (Int) – y position of the first Rect

  • ro (Int) – Radius of the second Circle

  • xo (Int) – x position of the second Circle

  • yo (Int) – y position of the second Circle

Checks for a collision between a Rect and a Circle in abstract.

circle_rect(r, x, y, wo, ho, xo, yo)
Parameters
  • r (Int) – Radius of the first Circle

  • x (Int) – x position of the first Circle

  • y (Int) – y position of the first Circle

  • wo (Int) – Width of the second Rect

  • ho (Int) – Height of the second Rect

  • xo (Int) – x position of the second Rect

  • yo (Int) – y position of the second Rect

Checks for a collision between a Circle and a Rect in abstract. Interface for Collider.rect_circle

circle_circle(r, x, y, ro, xo, yo)
Parameters
  • r (Int) – Radius of the first Circle

  • x (Int) – x position of the first Circle

  • y (Int) – y position of the first Circle

  • ro – Radius of the second Circle

  • xo (Int) – x position of the second Circle

  • yo (Int) – y position of the second Circle

Checks for a collision between any two Circles abstract.

Space

class pecrs.space.Space(size=256)
Parameters

size (int) – Size of the collision areas

Space handles the movement and collision detection between shapes. You can change the values here to work with different spatial partioning system and collision systems.

grid

SpatialHash, spatial partitioning system. Think about combining these into one class

collider

Collider, collision detection system

add(shape, update_area=True)
Parameters

shape (Shape) – Shape to add to the space

Adds a shape to the space. Bodies are added to their collision area as well as the 8 nearest neighboring cells to handle overlap and fast moving obejcts.

delete(shape)
Parameters

shape (Shape) – Shape to removed from the space

Removes a shape from the space.

has(shape)
Parameters

shape (Shape) – Shape to check for

Returns

True if the space contains the shape, False if not

Return type

bool

Checks if a space has a shape in that shape’s area Note that the shape’s must be set correctly or errors can occur

collisions_at(x, y, width=1, height=1)
Parameters
  • x (Int) – Posistion to search for on the x-axis

  • y (Int) – Posistion to search for on the y-axis

  • width (Int) – Width of the search area

  • height (Int) – Height of the search area

Returns

List of shapes colliding at the position

Return type

list(Shapes)

Gets a list of all shapes colliding at x, y

collisions_with(shape)
Parameters

shape (Shape) – Shape to find collisions with

Returns

List of shapes colliding at the position

Return type

list(Shape)

Get a list of all shapes colliding with shape

check_at(x, y, width=1, height=1)
Parameters
  • x (Int) – Posistion to search for on the x-axis

  • y (Int) – Posistion to search for on the y-axis

  • width (Int) – Width of the search area

  • height (Int) – Height of the search area

Returns

True if there is a collision at x, y, False if not

Return type

bool

Check to see if there is a collision at x, y in space.

check(shape)
Parameters

shape (Shape) – Shape to check for collisions

Returns

True if the shape is colliding with anything in space, False if not

Return type

bool

Check to see if something is colliding with shape in the space.

check_two(shape, other)
Parameters
  • shape (Shape) – First Shape to check for collision

  • other (Shape) – Second Shape to check for collision

Returns

True if the two shapes are colliding, False if not

Return type

Bool

Check to see if two shapes are colliding

update_area(shape)
Parameters
  • shape (AbsBody) – Body to be moved from the space

  • area (Tuple(Int, Int)) – Area of the space to move the shape to

Returns

Original area if updating, None if not

Return type

Tuple(Int, Int) or None

Moves a shape from one collision area to another

move(shape, x, y, distance)
Parameters
  • shape (Shape) – Shape to move

  • x (int) – Direction to move in on horizontal plane

  • y (iny) – Direction to move in on the vertical plane

  • distance (float) – Distance to move for

Returns

Distance moved

Return type

tuple(int, int)

Advances a shape in the direction x, y for distance units.

push(shape, x, y)
Parameters
  • shape (Shape) – Shape to push

  • x (int) – Direction on the horizontal plane to push the shape

  • y (int) – Direction on the vertical plane to push the shape

Pushes a shape in the direction of x, y in space

place(shape, x, y)
Parameters
  • x (int) – The position in space to place the shape on the horizontal plane

  • y (int) – The position in space to place the shape on the vertical plane

Directly places a shape at x, y in space

Body

class pecrs.body.Body(id, x, y, width, height)
Parameters
  • id – Identifer of the body, should be unique to an Index

  • x (Int) – Position of the Body on the x-axis

  • y (Int) – Position of the Body on the y-axis

  • width (Int) – Width of the Body

  • height (Int) – Height of the Body

The abstract base class for all bodies. Bodies are physical entities that can interact with other bodies in space.

id

Identifer of the body, should be unique

direction

Tuple(int, int), holds the direction the body moves in

speed

Number, how fast the body moves

name

string, used to communicate body type in a reader friendly way

moving

bool, If the body should be moved during processing

Controller

class pecrs.controller.Controller(size=256)
Parameters

size (int) – Size of each collision area, in pixels. A large area means less area changes while moving but more collision checks

High-level manager of the physics system.

index

Index, Indexing system used to track bodies.

actives

list, stores Body for processing.

space

Space partionining system.

make(kind, x, y, width, height, id=None, dx=0, dy=0, static=False)
Parameters
  • kind (Uninstanced Body) – Class to be created

  • x (int) – Position in space to create the body on the horizontal plane

  • y (int) – Position in space to create the body on the vertical plane

  • width (int) – Width of the new body

  • height (int) – Height of the new body

  • id (int) – Unique identifer to assign to the body, generated by Index if None

  • dx (int) – Directional facing of the body

  • dy (int) – Directional facing of the body

Returns

Newly created body

Return type

kind

Creates a Body at position x, y with a height and width, then add it to the system.

on_make(body)
Parameters

body (Body) – Body that was made by the system

Callback when bodies are made by the system. Override this when extending your Controller.

add(body, id=None, static=False)
Parameters
  • body (Body) – Body to be added to the system

  • id (Int) – Unique identifer to assign to the body, generated by Index if None

  • static (Bool) – If the Body is Static or Dynamic

Adds a body to the system and space.

delete(body)
Parameters

body (Body) – Body to be removed from the system

Removes a body from the system. Triggers on_delete(body) callback

delete_id(id)
Parameters

id (int) – id of the body to be removed from the system

Removes a body from the system by id. Triggers on_delete(body) callback

on_delete(body)
Parameters

body (Body) – Body removed from the system

Callback when bodies are deleted by the system. Override this when extending your Controller

move(body, delta)
Parameters
  • body (Body) – Body to be moved by the system

  • delta (float) – Units of time to move the body

Move a body by it’s speed and direction for delta. Triggers the on_move(body, distance) and on_motion(body) callbacks

move_to(body, x, y, delta)
Parameters
  • body (Body) – Body to be moved by the system

  • x (int) – Direction to move the body in on the horitzontal plane

  • y (int) – Direction to move the body in on the vertical plane

  • delta (float) – Units of time to advance the simulation by

Move a body by it’s speed in the direction of x, y for delta. Triggers the on_move(body, distance) and on_motion(body) callbacks

on_move(body, distance)
Parameters
  • body (Body) – Body that was moved by the system

  • distance (tuple(int, int)) – The distance moved by the body

Callback when bodies are moved by the system. Override this when extending your Controller

push(body, x, y)
Parameters
  • body (Body) – Body to be pushed by the system

  • x (int) – Direction to push the body in on the horitzontal plane

  • y (int) – Direction to push the body in on the vertical plane

Pushes a body in the direction of x, y. Triggers callbacks for on_push(body, x, y) and on_motion(body)

on_push(body, x, y)
Parameters
  • body (Body) – Body that was pushed in space by the system

  • x (int) – Direction the body was pushed in on the horitzontal plane

  • y (int) – Direction the body was pushed in on the vertical plane

Callback when bodies are pushed by the system. Override this when extending your Controller

place(body, x, y)
Parameters
  • body (Body) – Body to be placed in space by the system

  • x (int) – Position to place the body in on horitzontal plane

  • y (int) – Position to place the body in on the vertical plane

Directly place a body at x, y Triggers the callbacks on_place(body, start) and on_motion(body) Use this instead of body.place(x, y) to keep track of collision area

on_place(body, start)
Parameters
  • body (Body) – Body that was placed by the system

  • start (Tuple(Int, Int)) – The location of the body before being placed

Callback triggered when bodies are placed by the system. Override this when extending your Controller

on_motion(body)
Parameters

body (Body) – Body that had its position changed by the system.

Callback when bodies change positions. This is triggered by both move and place. Override this when extending your Controller

on_area(body, start)
Parameters
  • body (Body) – Body that had its area changed by the system.

  • start (Tuple(Int, Int)) – Area the Body was in before it was changed.

Callback when bodies change collision areas. This is triggered by move(), move_to(), place(), and push(). Override this when extending your Controller.

turn(body, x, y)
Parameters
  • body (Body()) – Body to be rotated from the system

  • x (int) – Direction to move to in the horitzontal plane

  • y (int) – Direction to move to in the vertical plane

Change the direction of a body

on_turn(body)
Parameters

body (Body()) – Body that was turned

Callback when a body is turned by the system. Override this when extending your Controller

start_moving(body)
Parameters

body (Body()) – Body to active for movement during processing by the system

Tells a body to move when the system is processed.

on_start_moving(body)
Parameters

body (Body()) – Body that was actived for movement

Callback when bodies are told to move by the system. Override this when extending your Controller

stop_moving(body)
Parameters

body (Body()) – Body to deactived system movement processing for

Tells a body to not move when the system is proccessed.

on_stop_moving(body)
Parameters

body (Body()) – Body that was deactived for movement

Callback when bodies are told to stop by the system. Override this when extending your Controller

get(id)
Parameters

id (int) – id of the body to get

Returns

Body or None

Return type

Body or None

Gets an body by its id.

collisions_at(x, y, width=1, height=1)
Parameters
  • x (int) – Position to seach for on in the horitzontal plane

  • y (int) – Position to seach for on in the vertical plane

Returns

A list of bodies located at x, y

Return type

List(Body)

Finds all of the bodies at x, y.

collisions_with(body)
Parameters

body (Body) – Body to check for collisions

Returns

A list of bodies colliding with body

Return type

List(Body)

Finds all bodies colliding with body.

check(body)
Parameters

body (Body) – Body to check for collisions with

Returns

True if Collisions, False is not

Return type

Bool

Check to see if any shapes are colliding with the Body in space

check_at(x, y, width=1, height=1)
Parameters
  • x (Int) – Posistion to search for on the x-axis

  • y (Int) – Posistion to search for on the y-axis

  • width (Int) – Width of the search area

  • height (Int) – Height of the search area

Returns

True if there is a collision at x, y, False if not

Return type

bool

Check to see if there is a collision at x, y in space.

check_two(body, other)
Parameters
  • body (Body) – First Body to check for collision

  • other (Body) – Second Body to check for collision

Returns

True if the two bodies are colliding, False is not

Return type

Bool

Check to see if the two bodies are colliding.

step(delta)
Parameters

delta (float) – Units of time to advance the simulation

Process the system for delta steps. Triggers the on_step_start(delta), on_step(body, delta), on_collision(body, collisions), and on_step_end(delta) callbacks

on_step_start(delta)
Parameters

delta (float) – Units of time to advance the simulation

Callback before the simulation takes a step. Override this when extending your Controller

on_step(body, delta)
Parameters
  • body (Body) – Body being processed by the step

  • delta (float) – Units of time to advance the simulation

Callback when the simulation takes a step over a body. Override this when extending your Controller

on_collision(body, collisions)
Parameters
  • body (Body) – Body involved in a collision

  • collision – List of bodies colliding with body

Callback when bodies collide with another body. Override this when extending your Controller

on_step_end(delta)
Parameters

delta (float) – Units of time to advance the simulation

Callback after the simulation takes a step. Override this when extending your Controller

Spatialhash

class pecrs.spatialhash.SpatialHash(size)
Parameters

size (int) – Size of each grid cell

A generic spatialhash Keeps a bucket of objects at a given scaled position

grid

dict, Grid of buckets

size

int, Size of each grid cell

scale(x, y)
Parameters
  • x (int) – A position in to be scaled on the horizontal plane

  • y (int) – A position in to be scaled on the horizontal plane

Returns

Grid cell key for the position at x, y

Return type

tuple(int, int)

Scales an x, y value to a grid cell position

add(item, area)
Parameters
  • item (Object) – Item to be added to the grid

  • area (tuple(int, int)) – Area to add the item to

Add an item to the grid at an area

add_pos(item, x, y)
Parameters
  • item (Object) – Item to be added to the grid

  • x (int) – Area position

  • y (int) – Area position

Add an item to the grid at x, y

has(item, area=None)
Parameters
  • item (Object) – Item to be added to the grid

  • area (tuple(int, int) or None) – Area to check, checks all if None

Checks to see if an item is in the grid

get(area)
Parameters

area (tuple(int, int)) – Grid area

Returns

List of objects at position at x, y

Return type

list(Object)

Get a bucket at area from the grid

get_pos(x, y)
Parameters
  • x (int) – Area position

  • y (int) – Area position

Returns

List of objects at position at x, y

Return type

list(Object)

Get a bucket at x, y from the grid

delete(item, area)
Parameters
  • item (Object) – Item to be removed to the grid

  • area (tuple(int, int)) – Area to remove the item from

Remove an item from the grid at an area

delete_pos(item, x, y)
Parameters
  • item (Object) – Item to be removed to the grid

  • x (int) – Area position

  • y (int) – Area position

Remove an item to the grid at x, y

Index

class pecrs.index.Index

Indexing system for assigning unique IDs to bodies.

Index keeps track what number to assign to new entities.

TODO move object list keeping and retrival; update docs

count

The ID to be used for the next new body

free

A list of free IDs for reuse

list

List of objects by thier id

next()
Returns

A unique id

Return type

int

Gets a unique identifer for this index. Freed ids are assigned first, in first-in-last-out order.

delete(id)
Parameters

id (int) – Previously assigned id to be returned to the system

Frees an identifer to be reused.

Clock

class pecrs.clock.Clock(rate)
Parameters

rate (float) – Minimum amount of time before the next tick can occur

Keeps track of time through polling tick

rate

float, Minimum amount of time before the next tick can occur

time

ctypes.c_ulong, Number of ticks. Using ctypes for free rollover

last

time.time, Time of the last tick

get_time()

Get the number of ticks that have elapsed :return: Number of ticks that have elapsed :rtype: int

tick()
Returns

delta or None

Return type

float or None

Attemps to advance the system clock. Delta is the ratio of time that has passed since the tick. For example if your tick rate is 100ms, and your tick happens 150ms after your last tick, your delta will be 1.5 You probably are going to want to use this one for timing in games

tick_time()
Returns

Current time

Return type

int

Attemps to advance the system clock. Always returns the time.

tick_check()
Returns

True if rate time has elapsed since the last tick, False if not

Return type

bool

Attemps to advance the system clock.

class pecrs.clock.SyncClock

A Clock for syncing values from a master Clock.

get_time()
Returns

The current time

Return type

int

Gets the current time

Indices and tables