Skip to main content

A simple collision library for pygame inspired by LÖVE2D's windfield

Project description

Alt text

Stormfield - A Pygame Collision Library

Stormfield aims to partially recreate Love2D's windfield physics library.
The main idea behind it is to easily intergrate collisions in your game

[!NOTE] Stormfield is still in development, some expected features may not be present.

[!IMPORTANT] The main reason i created stormfield was to easily manage collisions and collision classes, NOT to handle full physics — although some physics features are present.


Contents




Instalation

git clone https://github.com/clxakz/stormfield/

or

pip install stormfield



Quick Start

place the stormfield folder inside your project and import it

from stormfield import World

Create a world

A physics World can be created similarly to windfield.
Create a World with a vertical gravity of 500.

world = World(pygame.Vector2(0, 500))

Create colliders

Create a box collider and a ground collider, apply a linear impulse to the box. The box collides with the ground and bounces.

box = world.newRectangleCollider(300, 350, 100, 100)
box.setRestitution(0.8)
box.applyLinearImpulse(pygame.Vector2(0, 1000))

ground = world.newRectangleCollider(100, 600, 600, 100)
ground.setType("static") # <- Types can be 'static', 'dynamic' or 'kinematic'. Defaults to 'dynamic'

# In your main loop
world.update(dt)
world.draw(screen) # <- The world can be drawn for debugging purposes

And that looks like this:

Alt text


Documentation


World

World(gravity)

The World class is the core of the Stormfield collision system. It manages all colliders, updates their positions, resolves collisions, and handles collision class logic.

Stormfield’s World object is inspired by the windfield library in LÖVE2D, focusing on ease of use and flexibility for collision handling in Pygame.

world = World(pygame.Vector2(0, 500))

Arguments:

  • gravity (pygame.Vector2) - The gravity for all of your collider objects



.addCollisionClass(name, ignores)

A collision class is a way to group colliders and control which objects should interact with each other. You can assign a collision class to each collider, and configure which classes should ignore collisions with others. This allows you to easily manage complex collision relationships without writing custom logic for every case.

world.addCollisionClass("Player")
world.addCollisionClass("Enemies", ["Walls"]) # <- The enemy class will ignore all objects in the 'Walls' class
world.addCollisionClass("Walls")

collider.setCollisionClass("Player") # <- Assigns a collision class to your object

Arguments:

  • name (str) - The name of a collision class
  • ignores (list[str]) - A list of other classes a class should ignore



.draw(screen, color, width)

The draw() function allows you to quickly visualize all colliders in the world by drawing their shapes on the screen using pygame.draw.rect(). This is mainly useful for debugging and development purposes.

world.draw(screen, (255,255,255), 1)

Arguments:

  • screen (pygame.Surface) - The surface where all colliders should be drawn
  • color (tuple) - RGB color of the outline
  • width (int) - The width of the outline



.newRectangleCollider(x, y, width, height)

Creates a new rectangle collider and automatically adds it to the world.

box = world.newRectangleCollider(0, 0, 100, 100)

Arguments:

  • x (int) - The X position of the collider
  • y (int) - The Y position of the collider
  • width (int) - The width of the collider
  • height (int) - The height of the collider



.destroy()

The destroy() function will clear all colliders, collision classes and resets the gravity.

world.destroy()



Rectangle Collider

A collider is an object that handles collision detection and can be attached to your game entities or sprites.

.setLinearVelocity(velocity)

Sets the linear velocity vector of the collider. This controls how fast and in what direction the collider moves each frame.

collider.setLinearVelocity(pygame.Vector2(100, 0))

Arguments:

  • velocity (pygame.Vector2) — The velocity vector to apply.



.applyLinearImpulse(impulse)

Applies an instantaneous impulse to the collider, changing its velocity immediately based on the impulse and the collider’s mass. Only affects colliders of type "dynamic".

collider.applyLinearImpulse(pygame.Vector2(0, 500))

Arguments:

  • impulse (pygame.Vector2) — The impulse vector to apply.



.setFriction(friction)

Sets the friction coefficient for the collider, which slows it down over time when moving.

collider.setFriction(0.5)

Arguments:

  • friction (float) — The friction value.



.setMass(mass)

Sets the mass of the collider, which affects how impulses change its velocity.

collider.setMass(2.0)

Arguments:

  • mass (float) — The mass value (must be positive).



.setRestitution(restitution)

Sets the restitution (bounciness) of the collider. Determines how much velocity is retained after collisions.

collider.setRestitution(0.8)

Arguments:

  • restitution (float) — A value between 0 (no bounce) and 1 (perfect bounce).



.setType(type)

Sets the physics type of the collider. Types control whether the collider moves and how:

Type Affected by Gravity Affected by Velocity Moves? Use Case
dynamic ✅Yes ✅Yes ✅Yes Fully simulated objects like players, enemies, projectiles.
kinematic 🚫No ✅Yes (manual) ✅Yes Moving platforms, doors, scripted movement (you set velocity manually).
static 🚫No 🚫No 🚫No Walls, floors, anything that doesn't move.
collider.setType("static")

Arguments:

  • type (str) — One of 'static', 'dynamic', or 'kinematic'. Defaults to dynamic



.setObject(obj)

Associates a custom object with the collider, usually the game entity or sprite it belongs to. Useful for accessing your entity during collision callbacks. Basically this is what gets returned when retrieving collision data which usually you want to be self.

collider.setObject(player)

Arguments:

  • obj (any) — Your custom object reference. Defaults to self



.setCollisionClass(name)

Assigns a collision class to the collider to manage which other colliders it can interact with.

collider.setCollisionClass("Player")

Arguments:

  • name (str) — The name of the collision class.



.setOnCollisionEnterFunc(function)

Assings a function to the onCollisionEnter event.

def on_enter(obj):
  print(obj)

collider.setOnCollisionEnterFunc(on_enter)

Arguments:

  • function function - The function that should be assigned.



.setOnCollisionExitFunc(function)

Assings a function to the onCollisionExit event.

def on_exit(obj):
  print(obj)

collider.setOnCollisionExitFunc(on_exit)

Arguments:

  • function function - The function that should be assigned.



.setOnCollisionStayFunc(function)

Assings a function to the onCollisionStay event.

def on_stay(obj):
  print(obj)

collider.setOnCollisionStayFunc(on_stay)

Arguments:

  • function function - The function that should be assigned.

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

stormfield-1.1.1.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stormfield-1.1.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file stormfield-1.1.1.tar.gz.

File metadata

  • Download URL: stormfield-1.1.1.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for stormfield-1.1.1.tar.gz
Algorithm Hash digest
SHA256 7901c35c64608c631746b1f6267744c94f90f12eb61ee28cf4b65c53ad7a382c
MD5 f10dd058b6ca1fc82a137c2635cff522
BLAKE2b-256 79b4b451510a8aa834c1f9136165aec2f9469ae13f33914e1af3a5c2f074bcac

See more details on using hashes here.

File details

Details for the file stormfield-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: stormfield-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for stormfield-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 94462db2727b5ee4b73d2b8dfb445a7b1958f800aaa162a052de9c153666d912
MD5 de4ccb3022f4300a3feae8a95e496f24
BLAKE2b-256 27ce74e93599fbeedf7423d079e612267a5ec9a31d8d081cb97fd8015a628f27

See more details on using hashes here.

Supported by

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