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.



.setSensor(is_sensor)

When a collider is set as senor it will not collide with any other game object but will trigger onCollisionEnter, Stay and exit events

collider.setSensor(True)

Arguments

  • is_sensor (bool) - Sets the sensor value



.destroy()

Destroys the collider and removes it from the world

collider.destroy()

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.5.tar.gz (11.3 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.5-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stormfield-1.2.5.tar.gz
  • Upload date:
  • Size: 11.3 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.5.tar.gz
Algorithm Hash digest
SHA256 f3e7ca1f57777cd03b31024eef9cd9d8c6ed764246ebcbba14f1a5ea80f4288c
MD5 e53f84b730d948000331b7ea326922f5
BLAKE2b-256 aa82801b7918de9169e63465db30ba64f5f7dd85ba86a6a41032f3926f817f5c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stormfield-1.2.5-py3-none-any.whl
  • Upload date:
  • Size: 8.6 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 99ddce9c9f30c0a836e5a492e2c76e5cf98f23e12e9951679522d590f66ef8d2
MD5 0aee4aa253434f9ec6367b71e5da7776
BLAKE2b-256 c66fc6b34d2760d3ab773115a46dc2d49c54f9308a4bb70b47680225f1385bac

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