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.0.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.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stormfield-1.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 b9c723c7f104e3dfc6f4b8578c0def2fbd9977e69304a6db116151760845738d
MD5 c91fa8262c4a995e0192103302701a3a
BLAKE2b-256 2eea99d2f7fe4a48a24a7dbadf7a271186861f7fe56efdd8fc73a91f5f2d13c4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stormfield-1.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e873138b6467fa0308dbaffacd4f9ffb6216dfbc1007de876e1cc8e43f22118
MD5 f8c206ad0c6faa59518195c9528fd4e3
BLAKE2b-256 f577636f7164a0f29fac2a0b7e5c98d889c075e3a22bc9042aa2e65dce4971bf

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