Skip to content

Conversation

Riyarajput053
Copy link

…tc in physics

Describe your change:

The collision detection algorithm implemented in [collision_detection.py] is designed to detect collisions between basic geometric primitives: circles and rectangles. It includes methods for detecting collisions between two circles, two rectangles, and a circle with a rectangle.

  1. Circle-Circle Collision
    This method checks if two circles overlap by calculating the distance between their centers and comparing it to the sum of their radii.

  2. Rectangle-Rectangle Collision
    This method checks if two axis-aligned rectangles overlap by comparing their edges.

  3. Circle-Rectangle Collision
    This method checks if a circle overlaps with a rectangle by finding the closest point on the rectangle to the circle's center and checking if the distance to this point is less than the circle's radius.

Fixes: #12569

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

return distance_squared <= radius_sum**2

@staticmethod
def rectangle_rectangle_collision(rect1, rect2):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to work, e.g. rect1 = (x = 0, y = 0, width = 5, height = 2), rect2 = (x = 1, y = 10, width = 5, height = 2), would return collision.
Would make sense to add tests and do in 3 dimensions or name it 2D.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a collision detection algorithm
2 participants