A collision detection extension for the University of Portsmouth's graphix module, adding axis-aligned bounding box (AABB) collision detection with normal vector calculations.
This contribution integrates collision detection directly into the move() method of graphical objects, enabling real-time collision detection and physics-based responses for games and simulations.
- AABB Collision Detection: Efficient axis-aligned bounding box collision detection for rectangles, ovals, and circles
- Normal Vector Calculation: Returns collision normal vectors for physics-based responses (bouncing, sliding, etc.)
- Integrated Move Method: Collision detection happens automatically during movement
- Simple API: Enable collision detection with a single property
- Replace your existing
graphix.pywith the modified version - No additional dependencies required - works with standard Python and Tkinter
from graphix import Window, Rectangle, Point
win = Window("Collision Demo", 400, 400)
a = Rectangle(Point(50, 50), Point(150, 150))
b = Rectangle(Point(200, 200), Point(300, 300))
a.collision = True
b.collision = True
a.draw(win)
b.draw(win)
collision, normal = a.move(10, 10)
if collision:
print(f"Collision detected! Normal vector: {normal}")Moves the object and checks for collisions.
Parameters:
dx: Units to move in x directiondy: Units to move in y direction
Returns:
tuple[bool, tuple[int, int]]:(collision, normal_vector)collision:Falseif no collision occurred,Trueif collision detectednormal_vector:(nx, ny)- Normal vector at collision point (for physics calculations)
See sample/ball.py for a complete bouncing ball simulation demonstrating collision detection with normal vectors.
Normal vectors indicate the direction perpendicular to the collision surface. They are essential for:
- Bouncing: Reflect velocity using the normal vector
- Sliding: Project movement along surfaces
- Physics simulations: Calculate collision responses
- Rotated objects: Collision detection only works for axis-aligned shapes (no rotation)
- Concave polygons: Requires convex collision detection (not yet implemented)
- Collision groups: Not yet implemented (planned for future release)
- Rectangle
- Oval
- Circle
- Collision groups (objects only collide with specific groups)
- Polygon collision support
- Rotated object support (OBB - Oriented Bounding Boxes)
This is an open-source contribution! Feel free to:
- Fork and extend functionality
- Add new collision mechanics
- Improve performance
- Fix bugs
- Add tests
Please maintain attribution to original contributors when forking or extending this work.
This extension follows the same license as the original graphix module (GPL).
- Original graphix module by John Zelle
- Modified for University of Portsmouth M30299 Programming module
- Collision detection extension contributed to the community
For issues, questions, or suggestions:
- Open an issue on the repository
- Contact me on the University Discord