Skip to main content

Pythonic Entity Collision Resolution System

Project description

logo

Pythonic Entity Collision Resolution System

pecrs is a pure Python 2D physics system with a focus on top-down games and simple platformers.

Pure Python makes pecrs portable and easy to modify to suit your own needs.

Focused use-case makes pecrs simple to learn and use.

Seamless integration with Pyglet

Installation

Via pip

python3 -m pip install pecrs

Quickstart

from pecrs import *

space = Space()
rectA = Rect(0, 0, 32, 32)
rectB = Rect(10, 0, 32, 32)

space.add(rectA)
space.add(rectB)

collision = space.check(rectA)
print(f"Is something colliding with rectA? {collision}")

collisions = space.collisions_with(rectB)
print(f"Who is colliding with rectB? {collisions}")

space.place(rectB, 100, 0)

collision = space.check_two(rectA, rectB)
print(f"Are rectA and rectB still colliding? {collision}")

Structual Overview

Base types of the system are Shapes and Bodies. Shapes have a position and dimensions which describe its physical properties. Bodies are Shapes with an id, direction, speed, and movement state.

Core functionality is providied by the Collider, which detects collisions between Shapes or Shape-like Objects.

The Space handles positioning of Shapes and optimizes collision handling.

The Controller provides high-level object oriented control over Bodies in a Space.

Real-world Usage

demo

from pecrs import *
import pyglet


class World(Space):
   def __init__(self):
      super().__init__()
      self.window = pyglet.window.Window(400, 300)
      self.batch = pyglet.graphics.Batch()
      
      self.red_image = pyglet.resource.image("red_rect.png")
      self.blue_image = pyglet.resource.image("blue_rect.png")

      spriteA = pyglet.sprite.Sprite(self.blue_image, x=0, y=150, batch=self.batch)
      spriteB = pyglet.sprite.Sprite(self.blue_image, x=300, y=150, batch=self.batch)

      self.add(spriteA, moving=True)
      self.turn(spriteA, (150, 0))

      self.add(spriteB)
      
      pyglet.clock.schedule_interval(self.run, 1.0/60)
      pyglet.app.run()

   def on_collision(self, shape, collisions):
      shape.image = self.red_image

   def run(self, delta):
      self.step(delta)
      self.window.clear()
      self.batch.draw()

world = World()

Documentation

https://solidsmokesoftware.github.io/pecrs-py/

Demonstration

https://github.com/solidsmokesoftware/solconomy

solconomy

Requirements

Tested with Python3.6.9

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pecrs-0.35.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

pecrs-0.35-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file pecrs-0.35.tar.gz.

File metadata

  • Download URL: pecrs-0.35.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.9 Linux/5.3.0-40-generic

File hashes

Hashes for pecrs-0.35.tar.gz
Algorithm Hash digest
SHA256 083c69adae35f4f40c0ab78fcd643da9653221de8b2f992e3dec922948c10f5a
MD5 805526d9bc7874b7d78d798a6360fe8b
BLAKE2b-256 630f7c9878ba9a8c2b6e98b31f111d8a0b2e8344f48d024a8278a20d9a07aece

See more details on using hashes here.

File details

Details for the file pecrs-0.35-py3-none-any.whl.

File metadata

  • Download URL: pecrs-0.35-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.9 Linux/5.3.0-40-generic

File hashes

Hashes for pecrs-0.35-py3-none-any.whl
Algorithm Hash digest
SHA256 0e8eca9884f0844ef2f03dc4f58c83d9945423c4f77797e3ea91da9bdde2b4f2
MD5 363c724e7d02be7ce0d840b95aa776e1
BLAKE2b-256 0109e28792e2eb44e7927b8ea40589928702508367a9526a7fdb900822ee63c0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page