Skip to main content

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

Project description

Alt text

Licence Python

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(200)

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.
from stormfield import CollisionType

collider.setType(CollisionType.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.2.1.tar.gz (10.7 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.2.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for stormfield-1.2.1.tar.gz
Algorithm Hash digest
SHA256 0091251e74dd3c0efecc5d77898ed4f7dcd84c079b4ae0c0cc095dc8a39a0a37
MD5 4162dca0d35ddb811c301eda79b5a6cb
BLAKE2b-256 05021151a9ea4080f66dcf6313d3f8331b990485df76e44b72d6f22776b1f396

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stormfield-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8fdbe1bdc2b61af3674aa79b21e472704ac1d75d550237928c1b8f1af0d73a3
MD5 376a066d72bd1f1abf1bf9e0d7b73bf4
BLAKE2b-256 c18903aa4279214953817bfabf18af74f3ef87514fcd33a71111547d4d948035

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