Skip to content

Phyrenos/GraphixModule

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Graphix Module - Collision Detection Extension

A collision detection extension for the University of Portsmouth's graphix module, adding axis-aligned bounding box (AABB) collision detection with normal vector calculations.

Overview

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.

Features

  • 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

Installation

  1. Replace your existing graphix.py with the modified version
  2. No additional dependencies required - works with standard Python and Tkinter

Usage

Basic Collision Detection

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}")

Methods

move(dx: int, dy: int) -> tuple[bool, tuple[int, int]]

Moves the object and checks for collisions.

Parameters:

  • dx: Units to move in x direction
  • dy: Units to move in y direction

Returns:

  • tuple[bool, tuple[int, int]]: (collision, normal_vector)
    • collision: False if no collision occurred, True if collision detected
    • normal_vector: (nx, ny) - Normal vector at collision point (for physics calculations)

Examples

See sample/ball.py for a complete bouncing ball simulation demonstrating collision detection with normal vectors.

Understanding 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

Recommended Reading

Limitations

Not Supported

  • 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)

Supported Objects

  • Rectangle
  • Oval
  • Circle

Roadmap

  • Collision groups (objects only collide with specific groups)
  • Polygon collision support
  • Rotated object support (OBB - Oriented Bounding Boxes)

Contributing

This is an open-source contribution! Feel free to:

  • Fork and extend functionality
  • Add new collision mechanics
  • Improve performance
  • Fix bugs
  • Add tests

Credits

Please maintain attribution to original contributors when forking or extending this work.

License

This extension follows the same license as the original graphix module (GPL).

Acknowledgments

  • Original graphix module by John Zelle
  • Modified for University of Portsmouth M30299 Programming module
  • Collision detection extension contributed to the community

Support

For issues, questions, or suggestions:

  • Open an issue on the repository
  • Contact me on the University Discord

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%